0

Session variables lost after header redirect Even i Used session_start(); in All Pages

Here My Code..

<?php 
session_start();
$id=$_REQUEST['id'];
$pid=$_POST['pid'];
$_SESSION['pid']=$_POST['pid'];

Add To Cart Function

include("cart/functions.php");
if($_REQUEST['command']=='add' && $_REQUEST['id']>0){
$id=$_REQUEST['id'];
addtocart($id,1);
header('location:shoppingcart.php');
exit();
}

After Click On This Button $_SESSION['pid']=$_POST['pid'];` Disappear From All Pages?

<input type="button" class="button1" value="Add To Cart" 
 onclick="addtocart(<?php echo $row3['id']?>);" />
</div>
</div></form>
Adam Smith
  • 16
  • 1
  • 13
  • Because you don't POST each page `$_SESSION['pid']=$_POST['pid'];` will probably sometimes not trigger correctly, thus killing your data stored in your session, and thus not validate in your other code-segments. Enable `error_reporting()` and you'll probably see what's going wrong :) –  Mar 06 '13 at 23:42

2 Answers2

1

header('location:shoppingcart.php'); is a forced redirect, there is no POST when this happens, so the line $_SESSION['pid']=$_POST['pid']; is going to have no effect. If you must do cookieless sessions, look into use-trans-sid: http://www.php.net/manual/en/session.configuration.php#ini.session.use-trans-sid

Quentin Skousen
  • 1,035
  • 1
  • 18
  • 30
  • thanks i have done it but can you give me solution for that problem http://stackoverflow.com/questions/15357372/in-php-for-loop-showing-only-one-row-from-mysql-database?noredirect=1#comment21697702_15357372 same problem i'm facing it right now – Adam Smith Mar 12 '13 at 14:23
0

You probably set $_SESSION['pid'] = $_POST['pid'] in every request - even if your POST doesn't even have pid in it.

Try to change this

$_SESSION['pid']=$_POST['pid'];

to this

if (isset($_POST['pid'])) {
  $_SESSION['pid'] = $_POST['pid'];
}
riha
  • 2,270
  • 1
  • 23
  • 40