Specify a return-path for generated mail
You may want to specify a return-path for your website generated mail. This is the address to which rejected mail will be returned. This is very different from a return address. You can specify the return-path by using the -f parameter in the mail() function or specifying the -f parameter as a default in a php.ini file.You can put the -f parameter in the mail() function, as follows :
mail($toEmailAddress,$subject,$message,$headers,"-f$returnPathAddress");
Is is a fifth parameter, and note there is no space between the -f and the email address.
You can specify a default return-path in your php.ini file using this line:
sendmail_path = /usr/sbin/sendmail -t -i -femail@domain.com
Ensure the sendmail path is correct for your host, and again note there is no space between the -f and the email address.
Clean up your PHP scripts
Scripting in php can get very messy. Typically you build your html as you go, so you have your php logic and your html all mixed up. IMHO, a better way is to separate your html from your php logic. Then you can edit your html page in a standard html editor, and work on your logic independently. This will make your site much easier to maintain. You can build very complex sites in this way, while keeping your scripts easy to understand. Build the variable content of your page in php, put the variable names in your html, and then as the last php line use this:- - Start Script Here - -
<?phpeval("echo(\"".str_replace("\"","\\\"",file_get_contents("page.htm"))."\");");?>- - End Script Here - -
This assumes that the html page is named page.htm. This line of code will take page.htm and replace all the $variable name with the corresponding value from the php script, and output the page. For more complex pages you can put html in the variables and you can add to a variable's value in the php script while you are building the page, for creating lists. This method was used to build this page and the page for www.prettyworthless.com.
If you just want to evaluate the html in the external file (meaning load variable values) and save the result in a variable for later echo or other use then consider this variation
- - Start Script Here - -
<?phpeval("\$variable = \"".str_replace("\"","\\\"",file_get_contents("page.htm"))."\";");?>- - End Script Here - -
Delete session files
Assuming you are using a custom php.ini file and have specified a user directory where session records are stored, this script can be used to delete old session files (the system will not purge them, so the directory will keep growing if you do not delete them). You must change the $sessionDir variable to the path where your session files are stored, and you can adjust the delete time frame as well (the example below uses 3 hours - you would not want to delete an active session). You can run this script directly or set it up to run at specific time intervals using cron.- - Start Script Here - -
<?php
$sessionDir = "/home/user/temp"; // your sessions directory$compareTime = time() - 10800; // 3 hours$dir = opendir("$sessionDir");
while (($file = readdir($dir)) !== false) {
if (substr($file,0,4) == "sess") {
if ($compareTime >= filemtime("$sessionDir/$file")) {
if (unlink ("$sessionDir/$file")) $count++;
}
}
} closedir($dir);
echo "$count session files were deleted";?>- - End Script Here - -
Deleteing php.ini files
You can find another Tip on this page for how to create a custom php.ini file. And another for how to populate directories with your php.ini file in the event your host requires you to have one in each directory with php scripts. But then if you want to delete all the php.ini files, you may need this script. It will delete any php.ini file from your main public directory and all subdirectories.- - Start Script Here - -
<?php// this script will delete all your php.ini files$path = "/home/" . get_current_user() . "/public_html";
function search($dir) {
$dh = opendir($dir);
while (($filename = readdir($dh)) !== false) {
if ( $filename !== '.' AND $filename !== '..' AND $filename !== 'cgi-bin' AND is_dir("$dir/$filename") ) {
$path = $dir."/".$filename;
$target = $path . "/php.ini";
if (file_exists($target)) {
echo "Deleting - $target <br>";
if (!unlink($target)) echo "<b>Delete failed for $target </b><br>";
}
search($path);
}
}
closedir($dh);
}$target = $path . "/php.ini";
if (file_exists($target)) {
echo "Deleting - $target <br>";
if (!unlink($target)) echo "<b>Delete failed for $target </b><br>";
}search($path);
echo "<br>Done.";?> - - End Script Here - -
Comments
Post a Comment