You can find another Tip on this page for how to create a custom php.ini file. But after you create it, many hosts will require you to put a copy in each directory. This script makes it easy. It will copy the php.ini file from your main public directory and put one in all the subdirectories. If you do not want it in every subdirectory, after you delete the ones you do not want you can do all future updates with the overwriteOnly flag on.
- - Start Script Here - -
- - End Script Here - -
Note - you may have to change the file/directory paths in the examples below to match your hosting environment.
- - Start Script Here - -
- - End Script Here - -
You may want to change other things in php.ini as well. You can do this by repeating the append statement for as many changes as you wish to make.
For example you may want to use your own temp directory for session files. To do this add the following append line:
$contents .= "session.save_path = /home/user/temp \n"; // user specified temp session file directory
Note the sessions directory is above the public directory for added security.
You may want to use your own temp upload directory for improved security. To do this add the following line:
$contents .= "upload_tmp_dir = /home/user/temp \n"; // user specified temp upload directory
Note the uploads directory is above the public directory for added security.
If you want to change the maximum file upload size to something larger than 2MB, add the following:
$contents .= "upload_max_filesize = 4M \n"; // user specified max file upload size
And if you want to go larger than 8MB, also add the folowing:
$contents .= "post_max_size = 10M \n"; // user specified post max size
The above script does a chmod 600 on the custom php.ini file which should adequately protect it from prying eyes.
An alterative to using a custom php.ini file is to use the php function ini_set() in your scripts to modify the default php parameter values. This is probably the best solution for custom scripts, but not practical if you use applications like phpbb which you do not know how to modify.
- - Start Script Here - -
<?php// set this value to Y if you only want to overwrite old php.ini files
// set this value to N if you want to put a php.ini file in every directory$overwriteOnly = "Y";
if ($overwriteOnly == "Y") echo "Operating in Overwrite Only Mode<br><br>";$path = "/home/" . get_current_user() . "/public_html";$source = $path . "/php.ini";
if (!file_exists($source)) die('Error - no source php.ini file');
function search($dir) {
global $source;
global $overwriteOnly;
$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) AND $overwriteOnly == "Y") {
echo "$path <b>skipped - no php.ini file</b><br>";
} else {
echo "$target <br>";
if (!copy($source,$target)) echo "<b>Write failed for $target </b><br>";
if (file_exists($target)) chmod($target,0600);
}
search($path);
}
}
closedir($dh);
}search($path);
echo "<br>Done.";?>- - End Script Here - -
Using a custom php.ini file
You may want to use a custom php.ini file in order to change some of the host defaults. For example, you may want to turn register_globals = off. If you copy the default host file, make changes, and then put it in your webspace, you may not know if your host makes further changes to the default file which could cause your file to be out-of-date. A good solution is the following script, which you can execute manually or kick-off with cron each night. The script reads the host default file, makes your change, and writes it to your public directory. Now you can always have the current host default script with your changes made. The example below changes register_globals in the default host php.ini file.Note - you may have to change the file/directory paths in the examples below to match your hosting environment.
- - Start Script Here - -
<?php// full unix path - location of the default php.ini file at your host$defaultPath = '/usr/local/lib/php.ini'; // full unix path - location where you want your custom php.ini file$customPath = "/home/user/public_html/php.ini";
if (file_exists($defaultPath)) {
$contents = file_get_contents($defaultPath);
$contents .= "\n\n; USER MODIFIED PARAMETERS FOLLOW\n\n"; // comment line
$contents .= "register_globals = Off \n"; // register globals off
$handle = fopen($customPath, 'w');
if (fwrite($handle, $contents)) {
if (file_exists($customPath)) chmod($customPath,0600);
$message = "The php.ini file has been modified and copied";
} else {
$message = "Processing error - php.ini write failed";
}
fclose($handle);
} else {
$message = "Processing error - php.ini file not found";
}
echo $message;?> - - End Script Here - -
You may want to change other things in php.ini as well. You can do this by repeating the append statement for as many changes as you wish to make.
For example you may want to use your own temp directory for session files. To do this add the following append line:
$contents .= "session.save_path = /home/user/temp \n"; // user specified temp session file directory
Note the sessions directory is above the public directory for added security.
You may want to use your own temp upload directory for improved security. To do this add the following line:
$contents .= "upload_tmp_dir = /home/user/temp \n"; // user specified temp upload directory
Note the uploads directory is above the public directory for added security.
If you want to change the maximum file upload size to something larger than 2MB, add the following:
$contents .= "upload_max_filesize = 4M \n"; // user specified max file upload size
And if you want to go larger than 8MB, also add the folowing:
$contents .= "post_max_size = 10M \n"; // user specified post max size
The above script does a chmod 600 on the custom php.ini file which should adequately protect it from prying eyes.
An alterative to using a custom php.ini file is to use the php function ini_set() in your scripts to modify the default php parameter values. This is probably the best solution for custom scripts, but not practical if you use applications like phpbb which you do not know how to modify.
Comments
Post a Comment