...

View Full Version : Sessions Working On Lcalhost But Not On Remote Server



Candrias77
10-29-2003, 04:23 AM
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 (http://webhost0.inspire.net.nz/phpinfo.php)).

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:


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


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.

Spookster
10-29-2003, 04:46 AM
Do you have a temp directory set up on the remote server for the sessions?

Candrias77
10-29-2003, 05:11 AM
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.

Spookster
10-29-2003, 05:22 AM
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.

Candrias77
10-29-2003, 05:39 AM
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

Candrias77
10-29-2003, 09:30 PM
No, same error on new server. The code I am using in addItem.php is here:


//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:

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.

Spookster
10-29-2003, 10:32 PM
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);

Candrias77
10-29-2003, 10:42 PM
Aw heck there are a few!


Notice: Undefined index: increase in /home/purplesapphire/public_html/cart/addItem.php on line 6

Notice: Undefined index: decrease in /home/purplesapphire/public_html/cart/addItem.php on line 18

Notice: Undefined index: remove in /home/purplesapphire/public_html/cart/addItem.php on line 30

Notice: Undefined index: prod in /home/purplesapphire/public_html/cart/addItem.php on line 47

Notice: Undefined variable: change 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:6) in /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?

Candrias77
10-30-2003, 04:52 AM
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.

Spookster
10-30-2003, 02:22 PM
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.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum