Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 10 of 10
  1. #1
    Regular Coder
    Join Date
    Jul 2002
    Location
    New Zealand
    Posts
    127
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Sessions Working On Lcalhost But Not On Remote Server

    Firstly, I have a prety standard php installation on my machine, PHP 4.2.3, my remote server is running PHP 4.2.2 (PHP Info).

    Ok, now to my problem. I have a shopping cart which is stored in a session variable as an array. Each product that is selected is stored in something like $_SESSION['prod'][]['prod_id'] and $_SESSION['prod'][]['quantity'] etc. where the [] in this case is incrimenting as to store various products. I am adding products with a script like this:

    PHP Code:
    $prod_array=array('prod_id'=>$prod_id'option'=>$option'quantity'=>$quantity);
    $_SESSION['prod'][]=$prod_array
    Then for a product list I simply loop out the array to access each product's info with: $_SESSION['prod'][$n]['prod_id'], etc.

    Now this seems to work fine on my localhost but refuses to work on the remote server. When I add a product I see that product alone in the cart but when i return to the cart it has gone. When I count() the array there is always only one result.

    Seems to me like the session variables are simply not being stored. I have this at the top of every page:

    PHP Code:
    session_start();
    header("Cache-control: private"); 
    Any Ideas on what settings could be causing the problem or what changes I could make to get this working?

    Thanks a lot for your help.
    eTheory - the theory of revolution

  • #2
    Supreme Overlord Spookster's Avatar
    Join Date
    May 2002
    Location
    Marion, IA USA
    Posts
    6,273
    Thanks
    4
    Thanked 83 Times in 82 Posts
    Do you have a temp directory set up on the remote server for the sessions?
    Spookster
    CodingForums Supreme Overlord
    All Hail Spookster

  • #3
    Regular Coder
    Join Date
    Jul 2002
    Location
    New Zealand
    Posts
    127
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Yes. Well... I have not set anything up myself but I have other websites on this server that use sessions without a hitch so presume that it is set up ok.
    eTheory - the theory of revolution

  • #4
    Supreme Overlord Spookster's Avatar
    Join Date
    May 2002
    Location
    Marion, IA USA
    Posts
    6,273
    Thanks
    4
    Thanked 83 Times in 82 Posts
    Well if sessions are working fine on your remote server in other applications and it works fine on your testing server then the problem would appear to not be with the sessions.

    Your remote server is running an older version of PHP but I cannot think of anything off the top of my head about the differences between the two versions that could cause a problem like this.

    If you purchased the shopping cart software then I would first probably contact the tech support of the persons or company who wrote the software. They would obviously know best what is in there coding that could cause the problem.
    Spookster
    CodingForums Supreme Overlord
    All Hail Spookster

  • #5
    Regular Coder
    Join Date
    Jul 2002
    Location
    New Zealand
    Posts
    127
    Thanks
    0
    Thanked 0 Times in 0 Posts
    My own coding

    The PHP versions are very similar but I thought perhpas there would be a setting that they have altered that is causing the difference in the way that the sessions, or perhaps the arrays, are handled. I have contacted the webhost and they have set me up on another box with a fresh install of php, apache, mysql so i will post again if the problem persists.

    Thanks
    eTheory - the theory of revolution

  • #6
    Regular Coder
    Join Date
    Jul 2002
    Location
    New Zealand
    Posts
    127
    Thanks
    0
    Thanked 0 Times in 0 Posts
    No, same error on new server. The code I am using in addItem.php is here:

    PHP Code:
    //increase quantity from cart
    if (($prod_id=$_GET['increase']) && ($option=$_GET['option'])) {
        
    $i=count($_SESSION['prod']);
        for (
    $n=0$n<$i$n++) {
            if ((
    $_SESSION['prod'][$n]['prod_id']==$prod_id) && ($_SESSION['prod'][$n]['option']==$option)) {
                
    $_SESSION['prod'][$n]['quantity']=$_SESSION['prod'][$n]['quantity']+1;
            }
        }
        
    header("Location: index.php");
        
    end;
    }

    //decrease quantity from cart
    else if (($prod_id=$_GET['decrease']) && ($option=$_GET['option'])) {
        
    $i=count($_SESSION['prod']);
        for (
    $n=0$n<$i$n++) {
            if ((
    $_SESSION['prod'][$n]['prod_id']==$prod_id) && ($_SESSION['prod'][$n]['option']==$option)) {
                
    $_SESSION['prod'][$n]['quantity']=$_SESSION['prod'][$n]['quantity']-1;
            }
        }
        
    header("Location: index.php");
        
    end;
    }

    //delete from cart
    else if (($prod_id=$_GET['remove']) && ($option=$_GET['option'])) {
        
    $i=count($_SESSION['prod']);
        for (
    $n=0$n<$i$n++) {
            if ((
    $_SESSION['prod'][$n]['prod_id']==$prod_id) && ($_SESSION['prod'][$n]['option']==$option)) {
                
    $_SESSION['prod'][$n]['quantity']=$_SESSION['prod'][$n]['quantity']=0;
            }
        }
        
    header("Location: index.php");
        
    end;
    }

    //add product from products page
    else if (($_POST['quantity']==0) || ($_POST['quantity']=='')) {
        
    header("Location: ".$_POST['return_url']);
        
    end;
    } else if ((
    $prod_id=$_POST['product_id']) && ($option=$_POST['options']) && ($quantity=$_POST['quantity']) && ($return_url=$_POST['return_url'])) {
        
    //if product is already in the cart, increase quantity
        
    $i=count($_SESSION['prod']);
        for (
    $n=0$n<$i$n++) {
            if ((
    $_SESSION['prod'][$n]['prod_id']==$prod_id) && ($_SESSION['prod'][$n]['option']==$option)) {
                
    $_SESSION['prod'][$n]['quantity']=$_SESSION['prod'][$n]['quantity']+$quantity;
                
    $change="made";
            }
        }
        
    //otherwise, add new product info to the array
        
    if ($change!="made") {
            
    $prod_array=array('prod_id'=>$prod_id'option'=>$option'quantity'=>$quantity);
            
    $_SESSION['prod'][]=$prod_array;
        }
        
    header("Location: index.php");
        
    end;
    } else {
        
    header("Location: ".$_REQUEST['return_url']);
        
    end;

    To display the items in the cart I simply loop through the array with something like:
    PHP Code:
    for ($n=0$n<$i$n++) { 
    $sq=$_SESSION['prod'][$n]['quantity'];
    $so=$_SESSION['prod'][$n]['option'];
    $sp=$_SESSION['prod'][$n]['prod_id']; 
    Am I doing something illegal with the php here that for some reason I can get away with on my localhost but not on the remote server?

    Any help is much appreciated.
    eTheory - the theory of revolution

  • #7
    Supreme Overlord Spookster's Avatar
    Join Date
    May 2002
    Location
    Marion, IA USA
    Posts
    6,273
    Thanks
    4
    Thanked 83 Times in 82 Posts
    Try putting this at the top of your page of the one you are running on the remote server and see if you get any warnings or errors:

    error_reporting (E_ALL);
    Spookster
    CodingForums Supreme Overlord
    All Hail Spookster

  • #8
    Regular Coder
    Join Date
    Jul 2002
    Location
    New Zealand
    Posts
    127
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Aw heck there are a few!

    PHP Code:
    NoticeUndefined indexincrease in /home/purplesapphire/public_html/cart/addItem.php on line 6

    Notice
    Undefined indexdecrease in /home/purplesapphire/public_html/cart/addItem.php on line 18

    Notice
    Undefined indexremove in /home/purplesapphire/public_html/cart/addItem.php on line 30

    Notice
    Undefined indexprod in /home/purplesapphire/public_html/cart/addItem.php on line 47

    Notice
    Undefined variablechange in /home/purplesapphire/public_html/cart/addItem.php on line 55

    Warning
    Cannot add header information headers already sent by (output started at /home/purplesapphire/public_html/cart/addItem.php:6in /home/purplesapphire/public_html/cart/addItem.php on line 59

    Notice
    : Use of undefined constant end assumed 'end' in /home/purplesapphire/public_html/cart/addItem.php on line 60 
    Could these be causing issues?
    eTheory - the theory of revolution

  • #9
    Regular Coder
    Join Date
    Jul 2002
    Location
    New Zealand
    Posts
    127
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Problem Solved!

    Obviously, none of those warnings were serious. The problem actually lay in register_globals being off on their system. This meant that $_SESSION[cart][] was effectively called $cart[] and you guessed it, i was using a normal array $cart[] elsewhere on the page and they were overwriting eachother!

    Thanks for your help Spookster.
    eTheory - the theory of revolution

  • #10
    Supreme Overlord Spookster's Avatar
    Join Date
    May 2002
    Location
    Marion, IA USA
    Posts
    6,273
    Thanks
    4
    Thanked 83 Times in 82 Posts

    Re: Problem Solved!

    Originally posted by Candrias77
    Obviously, none of those warnings were serious. The problem actually lay in register_globals being off on their system. This meant that $_SESSION[cart][] was effectively called $cart[] and you guessed it, i was using a normal array $cart[] elsewhere on the page and they were overwriting eachother!

    Thanks for your help Spookster.
    Actually according to your php info register_globals is on. That was one of the first things I looked at. When it is on that problem could happen. This is just one of many reasons why it is always good to have it off. With that kind of error though you should have had problems both locally and remotely.
    Spookster
    CodingForums Supreme Overlord
    All Hail Spookster


  •  

    Posting Permissions

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