Thursday, April 30, 2009

osCommerce & Register Globals

What happens when your web host finally get's around to upgrading to php5 and MySQL 5? Here is what happened to me in order:

1. Rejoice because finally your website is hosted according to industry standards.
2. Panic because you are unprepared.
3. Worked through the night to get the website updated.

In order to migrate an osCommerce website from php4 to php5 there is really only one thing you need to worry about and that's a little "feature" in php called, Register Globals.

Register Globals makes every variable contained in the URL, POST, SESSION and ENVIRONMENT directly available in every script. What this means is that with register globals turned on, you can write a webform that posts a variable to a script and that script can refer to the variable in the POST by name. For example:


<?php
/*
** Example script to show what happens when register globals is turned on.
**
** A form will submit POST data to this website as follows:
** String $_POST['name'];
** Int $_POST['age'];
*/
echo "<h2>Thank you for telling me your name and age.</h2>";
echo "With register globals turned on we can do this:<br/>";
echo "Your name is: " . $name . "</br>";
echo "You are " . $age . " years old.";
?>


Now with register globals turned off, you need to do it this way.

<?php
/*
** Example script to show what happens when register globals is turned on.
**
** A form will submit POST data to this website as follows:
** String $_POST['name'];
** Int $_POST['age'];
*/
echo "<h2>Thank you for telling me your name and age.</h2>";
echo "With register globals turned on we can do this:<br/>";
echo "Your name is: " . $_POST['name'] . "</br>";
echo "You are " . $_POST['age'] . " years old.";
?>


PHP does have an neat function called, "extract" which will take all the variables in and associative array. It works like this:

<?php
/*
** As before we have a post that looks like this:
** String $_POST['name'];
** Int $_POST['age'];
**
** We want to create variables out of the $_POST array keys and make the values the values
** if you catch my drift.
**
*/
echo "<h2>Thank you for telling me your name and age.</h2>";
echo "With register globals turned off we can do this:<br/>";
if( isset ( $_POST ) && !empty( $_POST ) ) {
extract($_POST, EXTR_SKIP);
echo "Your name is: " . $name . "</br>";
echo "You are " . $age . " years old.";
} else {
echo "You did not post any data.";
}
?>


So you can see where I am going with this. The first thing I needed to do was to make sure that all the "registered global" variables were first caught and made available in the above way.

osCommerce Session handling
It quickly became apparent that I was not going to get anywhere until I had the sessions stuff sorted. At this stage I gave up working this out for myself and searched for help. I found an excellent resource called: Magic SEO URL for osCommerce (osCMax)

There were a couple of other pitfalls:

1. root/admin/includes/classes/upload.php has a line that sets "$this = null;" PHP 5 objects to this ( excuse the pun ) so the correction is fairly simple. Just comment it out. I did play with unset( $this ) but found that to cause some other difficulties.

2. The variable $PHP_SELF is referred to many times in osCommerce code. As this is a registered global it will not be available to your code after the upgrade. You must set this variable in application_top.php like this:

$PHP_SELF = $_SERVER['PHP_SELF'];


Other things that did not happen to me but might happen to you.
MySQL upgrade breaks all your queries. This is quite a common problem that I have encountered before but not recently. MySQL 5 requires the correct bracketing of clauses in the from statement of a query. Especially with regards to left joins. Search for this in the contributions section of osCommerce.

References:
http://www.magic-seo-url.com/oscommerce/tips/running-oscommerce-with-register-globals-off.html
http://www.oscommerce.com/community/contributions,2097/category,all/search,globals
http://www.oscommerce.com/community/contributions,4654
Post a Comment