Adam Smith
Adam Smith

Reputation: 16

Session variables lost after header redirect

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>

Upvotes: 0

Views: 1574

Answers (2)

riha
riha

Reputation: 2298

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'];
}

Upvotes: 0

Quentin Skousen
Quentin Skousen

Reputation: 1055

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

Upvotes: 1

Related Questions