$_SESSION["cart"] of type string mysteriously becomes an object. works in localhost

I have been going crazy for the past 3 days trying to debug the following cart application. The application works fine when running in localhost in Windows with PHP Version 5.3.1 But, when it runs in the Hostgator Linux server using PHP 5.2.11 it breaks, due to this error:


Fatal error: Call to a member function countItems() on a non-object in (*deleted path*)l/processCart2.php on line 109


[CENTER]Code clean of debug lines[/CENTER]

switch statement :


 switch($action){
    case "add":
	
	   if( !isset($_GET['item']) ) return;
	   
	   $itemId = $_GET['item'];
	   
	   $cart = initCar(); // While executing initCar() $_SESSION['cart'] is of type string, both when running in localhost and in the hostgator server.	

/* Here, right after initCar() $_SESSION['cart'] in localhost is of type string in localhost but of type object in hostgator  */   

	   $prod = new Product($itemId, 1); 
	   $cart->addProduct($prod); 

	   /* $cart->countItems() works here */
	   echo ' cartcounting '.$cart->countItems();
	   
	   $copy = $cart;
	   
	   $copySerial = serialize($copy);
	   
	   $_SESSION['cart'] = $copySerial;
	   
	   /* $cart->countItems() does not work anymore when running in the Hostgator server, works in localhost */
	   echo ' cartcounting is now '.$cart->countItems();
	   
	   
	   include("drawCart.php");
	   
	   return;
/* other cases  for the switch here */

initCar() :


 /* Returns Cart, new or unserialized */
 function initCar(){
 
      if(isset($_SESSION['cart'])){
			
			$serializedCart = $_SESSION['cart'];
			$cart = unserialize($serializedCart);
			
	   }
	   else{
	         $cart = new Cart;
	   }
	   
	   return $cart;

 }

[CENTER]Results of some tests I ran[/CENTER]

For some reason the $cart instance of class Cart becomes a string at some point. This does not happen when running in localhost.

It also happens that after initCar() ends executing, $_SESSION[‘cart’] in localhost is of type string as it should. But, in the hostgator server $_SESSION[‘cart’] is of type object.


/* localhost */
********* END initcar ********

$_SESSION["cart"] TYPE: string

Warning: get_class() expects parameter 1 to be object, string given in (*deleted path*)\\processCart2.php on line 66
$_SESSION["cart"] CLASS: 

/* Hostgator */
********* END initcar ********

$_SESSION["cart"] TYPE: object
$_SESSION["cart"] CLASS: Cart



This is very strange as while initCar() executes $_SESSION[‘cart’] is of type string, both when running in localhost and in the hostgator server.


/* Hostgator */
********* initCar() **********

$_SESSION["cart"] TYPE: string
$_SESSION["cart"] CLASS: 

/* localhost */
********* initCar() **********

$_SESSION["cart"] TYPE: string

Warning: get_class() expects parameter 1 to be object, string given in (*path deleted*)\\processCart2.php on line 34
$_SESSION["cart"] CLASS: 


I think you’re laying out the question wrong

The cart starts as an object (new Cart), becomes a string when you serialize it (to a string), and becomes an object again when you unserialize it (from a string).

It’s mysteriously remaining a string, becoming an object again is what you want, not mysterious. The error is complaining about it not being an object, because strings don’t have methods to call like that.

First thing I’d do is pinpoint when the problem begins. var_dump $_SESSION[‘cart’] and the unserialized version, maybe it’s there.

Thanks a lot for the prompt reply. And sorry for the lack of clarity.

I forgot to mention, the first time you run the script it actually runs fine in both localhost and hostgator. The problem occurs in hostgator when running the 2nd time.

When running processCart2.php the second time initCar() executes the following code and returns $cart


if(isset($_SESSION['cart'])){ 
             
            $serializedCart = $_SESSION['cart']; 
            $cart = unserialize($serializedCart); 
             
       } 


so $cart is an object, and $_SESSION[‘cart’] is a string at this point, as it should be through the whole script.

I want $cart to always be an object to be able to call methods on it. However I keep $_SESSION[‘cart’] as a string. When I want to update $_SESSION[‘cart’] I make a copy of $cart, serialize the copy and assign it to $_SESSION[‘cart’]

The problem in hostgator is that after


$_SESSION['cart'] = $copySerial;

$cart stops being an object and becomes a string while in localhost it remains an object.

Then when calling $cart->countItems() the problem occurs

I bet register_globals is enabled on this server. It does the odd thing with creating references to $_SESSION rather than copies as it does with $_POST and $_GET, when turning each key into a variable in the global scope. So $cart is pointing to the memory $_SESSION[‘cart’] is at and is why you’re having problems.

Disabling register_globals should fix that I think. Hosts really need to stop enabling it, it’s been deprecated for years.

Disabling register_globals should fix that

It did. I’m so glad this is over and can move forward. Thanks so much!