SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Enthusiast
    Join Date
    Aug 2009
    Posts
    42
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    $_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:

    PHP Code:
    Fatal errorCall to a member function countItems() on a non-object in (*deleted path*)l/processCart2.php on line 109 
    Code clean of debug lines


    switch statement :
    PHP Code:
     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($itemId1); 
           
    $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() :
    PHP Code:
     /* Returns Cart, new or unserialized */
     
    function initCar(){
     
          if(isset(
    $_SESSION['cart'])){
                
                
    $serializedCart $_SESSION['cart'];
                
    $cart unserialize($serializedCart);
                
           }
           else{
                 
    $cart = new Cart;
           }
           
           return 
    $cart;

     } 
    Results of some tests I ran


    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.
    PHP Code:
    /* localhost */
    ********* END initcar ********

    $_SESSION["cart"TYPEstring

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

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

    $_SESSION["cart"TYPEobject
    $_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.
    PHP Code:
    /* Hostgator */
    ********* initCar() **********

    $_SESSION["cart"TYPEstring
    $_SESSION
    ["cart"] CLASS: 

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

    $_SESSION["cart"TYPEstring

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

  2. #2
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,580
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    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.

  3. #3
    SitePoint Enthusiast
    Join Date
    Aug 2009
    Posts
    42
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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
    PHP Code:
    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
    PHP Code:
    $_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

  4. #4
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,580
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    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.

  5. #5
    SitePoint Enthusiast
    Join Date
    Aug 2009
    Posts
    42
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Disabling register_globals should fix that
    It did. I'm so glad this is over and can move forward. Thanks so much!


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •