I needed to set register_globals = OFF in my server yesterday and had a bit of a hard time finding out how to do it, as Hostgator (where this blog is hosted) does not have an easy way of changing it in the shared servers packages. I found tons of very helpful pages in blogs and forums all over the Web which helped me solve the problem. I feel it’s about time I give something back, besides taking this opportunity to thank all of you out there taking the time to help others.

So I learnt…

  1. to set the register_globals off, one must place a “php.ini” file with the line register_globals = OFF in every folder of the server (thank you bergmann at the joomla forum!)
  2. but this php.ini cannot just contain the register_globals line. The Computer guys at the University of Washington explain it really well in Using a php.ini File, basically saying: if the directory that calls a PHP script contains a php.ini file, that is the one (and only) from where the PHP configuration settings will be read, so you must first obtain the Web server’s php.ini file and add your own particular settings.
  3. now, I don’t know about other Hosting services, but you cannot access the php.ini file in Hostgator through ftp directly. ronniead in a brilliant step by step post in an aMember Professional forum, explains how to use a script to both copy the original php.ini file of the Web server and add the extra user settings. This “Creating a custom php.ini file” script from B&T’s Tips & Scripts is easy to download and use, following ronniead instructions. Only one catch.. it did not work for me.
  4. a bit more research and I found out that the function which appears in the script from B&T’s Tips & Scripts: file_put_contents() works with PHP5 but not with PHP4 which is what I have. Some googling for “file_put_contents() for PHP4″ brings up quite a few implementations of the PHP5 file_put_contents() function for PHP4. The one I settled for can be found in Stoyan Stefanov’s blog phpied.com (thanks Stoyan!)
  5. So a bit of messing around with both the script and Stoyan’s implementation and I finally got the script to create a custom php.ini file for PHP4 working. And here comes the moment to give back to society.
  6. You can either copy and paste the following code in a text editor, and then save as php_ini.php, or download the script from here: php_ini_PHP4.txt (right-click, Save Link As… and as it’s in .txt format, once downloaded, change the ending to .php).


<?php
// Put all the php.ini parameters you want to change below. One per line.
// Follow the example format $parm[] = “parameter = value”;
$parm[] = “register_globals = Off”;
$parm[] = “session.use_trans_sid = 0 “;


// full unix path - location of the default php.ini file at your host
// you can determine the location of the default file using phpinfo()
$defaultPath = ‘/usr/local/lib/php.ini’;


// full unix path - location where you want your custom php.ini file
// if you don't know the full unix path, uncomment the next line and run the script
// echo getcwd() . "\n";
// you will see the full path (and probably an error message)
$customPath = “/home/user/public_html/php.ini”;


// nothing should change below this line.
if (file_exists($defaultPath)) {
$contents = file_get_contents($defaultPath);
$contents .= “\n\n; USER MODIFIED PARAMETERS FOLLOW\n\n”;
foreach ($parm as $value) $contents .= $value . ” \n”;


// code added for the script to work with PHP4
if (!function_exists(‘file_put_contents’)) {
function file_put_contents($customPath,$contents) {
$f = @fopen($customPath, ‘w’);
if (!$f) {
return false;
} else {
$bytes = fwrite($f, $contents);
fclose($f);
return $bytes;
}
}
}
// end of added code for script to work with PHP4


if (file_put_contents($customPath,$contents)) {
if (chmod($customPath,0600)) $message = “The php.ini file has been modified and copied”;
else $message = “Processing error - php.ini chmod failed”;
} else {
$message = “Processing error - php.ini write failed”;
}
} else {
$message = “Processing error - php.ini file not found”;


}
echo $message;
?>

So there it is, the script to create the custom php.ini file for PHP4. Let me know how it goes!


rss feed to post Subscribe to the Computers posts of olivetalks, if you found this article interesting, thank you!



Subscribe to olivetalks to receive an email whenever a new post appears in the category of your choice.
Print This Post Print This Post

Tags: ,

One Response to 'Register_Globals Off and PHP4'

Subscribe to comments with RSS or TrackBack to 'Register_Globals Off and PHP4'.

  1. January 18th, 2008 at 6:05 am
    ZoltarStark said,

    A good example of how sharing what you write benefits all.

Post a comment