简体   繁体   中英

Session variables lost after header redirect

Session variables lost after header redirect Even i Used session_start(); in All Pages 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>

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM