Jump to content

Array or session problem in php version 5.2.17


zeekonia2002
 Share

Recommended Posts

Hi Everyone,

 

I'm building a custom shopping cart. I'm having trouble getting my code to work on a production server that is running PHP version 5.2.17. My test machine is running version 5.3.0 and the code works just fine. I do not have the ability to upgrade the version of PHP on the production machine. Here is part of my code. If we can fix this, I can fix the other switch cases:

 


//this code adds the item to the cart and creates an array and session variable for the cart. The array is multidimensional (item id1 => quantity, item id2 => quantity, item id3 => quantity and so on...).

if (isset($_GET["action"]))
{
    $action = $_GET["action"];
    $id = $_GET["id"] ;
    $q = $_GET["quantity"];
    $cart = array();

switch ($action)
{
	case "add":
		if ($_SESSION["cart"])
		{				
			$cart = $_SESSION["cart"];
			if (array_key_exists($id,$cart))
			{
				$cart[$id] = $cart[$id] + $q;
			}
			else
			{	
				$cart[$id] = $q;
			}					
		}
		else
		{
			$cart[$id] = $q;
		}
		$_SESSION["cart"] = $cart;
		break;
}
}

//this code displays the cart array 

if ($_SESSION["cart"])
{
    $cart = $_SESSION["cart"];

foreach($cart as $key => $value)
{	
	$runQ1 = mysql_query("SELECT * FROM products WHERE id='$key'") or die(mysql_error());	
	$results = mysql_fetch_array($runQ1);
	print "<table><tr><td>$results[1]</td><td>$results[2]</td><td>$results[3]</td><td>$results[4]</td></tr></table>";
}
}
else
{
print "<table><tr><td>There are no items in your shopping cart.</td></tr></table>";	
}

 

The cart session variable is being set but it is only holding one item at a time. If I add a second item to the cart, the 1st item disappears and only the second one is present in the session variable and the table. I'm not sure if it is a session problem or an array problem. Please help before I lose all of my hair! I don't understand why this code works on 5.3.0 and not 5.2.17...

 

Thanks in advance!

 

-Isaac

Link to comment
Share on other sites

There's nothing php version specific in your code. It's more likely that your session_start() statement is failing due to a php.ini configuration difference.

 

Are you developing and debugging your code on a system with error_reporting set to E_ALL and display_errors set to ON so that php will report and display all the errors it finds? There would likely be an error that would indicate why your code is not working.

 

What have you done to debug at what point your code is doing what you expect and at what point it is not? You could have missing data in your database so that only specific produce id's work or your code that is producing the get parameters on the end of the URL's could be messing up or about a half-dozen other things could be going wrong.

Link to comment
Share on other sites

Is the old cart data lost even if you don't add another item?  If that's the case, the likely cause is that session variables are not being stored.

 

But if the old data is only lost when you add a new item, it's possible there's code somewhere overwriting the cart.  It may not be the code you have pasted.  If this is the case, you can debug the issue by printing the contents of the cart throughout the script, and see where it changes from non-empty to empty.

Link to comment
Share on other sites

YYYYEEEESSSS!!!!! IT WORKSSSS!!!!!! WOOOOOHHHOOOOOOO!!!!!!

 

I did a comparison between the php.ini files on my machine and the production server. I turned off 'Register Globals' in the php.ini file on the production server and now the cart is working perfectly!

 

Sorry, I'm not a formally trained programmer. When would I ever need register globals to be on?

Link to comment
Share on other sites

You wouldn't ever need them to be on.

 

They were turned off by default in php4.2 in the year 2002 because they open a huge security hole that allows hackers to set your program and session variables simply by putting get parameters on the end of URL's when they visit your site and a lot of web sites were taken over.

Link to comment
Share on other sites

  • 1 year later...

(SOLVED) I had the same problem: only session array were lost. I confirm the solution:

 

in my php.ini I turned Off the register_globals parameter:

 

(php.ini)

 

; You should do your best to write your scripts so that they do not require

; register_globals to be on; Using form variables as globals can easily lead

; to possible security problems, if the code is not very well thought of.

register_globals = Off

 

 

...now it works!

Link to comment
Share on other sites

This thread is more than a year old.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.