How to pass session variables when header redirects? loosing session variables during the process of redirection

StackOverflow https://stackoverflow.com/questions/20629972

  •  18-09-2022
  •  | 
  •  

session varibales

<?php session_start(); 
error_reporting(-1);
if (!isset($_SESSION)){
}
$total_amt=$_POST['total_amt'];
$total_seats=$_POST['total_seats'];
$seat=$_POST['seat'];
$boarding_point=$_POST['boarding_point'];
$submit=$_POST['chksbmt'];
$_SESSION['total_amt']=$total_amt;
$_SESSION['total_seats']=$total_seats;
$_SESSION['seat']=$seat;
$_SESSION['boarding_point']=$boarding_point;
$_SESSION['chksbmt']=$submit;
?>

<?php
$serial_no = 1;
$total_seats = $_SESSION['total_seats']; 
$seats=explode(',', rtrim($total_seats, ","));
foreach ($seats as $seat) { ?>
<tr>
<td style="text-align:center;"> <?php echo $serial_no++; ?></td>
  <td style="text-align:center;"><?php echo $seat; ?> </td>

& so on ..

login div

<ul class="top">
         <li><a href="registration.php" class="about">Registration</a>
        </li>
       <?php if(!isset($_SESSION['login_user'])) { ?>
        <li class="hover"><a href="#" onClick="revealModal('modalPage')">Login</a>
        </li>
         <?php } else {?>
        <li class="hover"><a href="logout.php">Logout</a></li>
    <div style="color:#00CC66;">Welcome <?php echo $_SESSION['login_user'];     ?></div>
         <?php } ?>
              </ul>

logout.php

<?php session_start();
session_destroy(); 
header('Location:'.$_SERVER['HTTP_REFERER']);  
exit;  
?>  

When m logging out from my page, it has to be redirected & in this process i m loosing my session variables. Is that any process to keep the variables constant after the redirection .. After the page redirection m getting undefined index error.. Any help .. ?? Thank you in advance

有帮助吗?

解决方案

You're destroying whole session on logout page. While you just need to unset the relevant session key.

Something like,

session_start();
unset($_SESSION['login_user']); //session_destroy(); 
header('Location:'.$_SERVER['HTTP_REFERER']);  
exit;  

其他提示

instead of using session_destroy();
you should use unset($_SESSION['login_user']);

It will change the value of the variable in the session to null and you will not lose your variable. session_destroy() destroys the session completely and then you have no way to access that variable.
You can check the difference between them using var_dump
eg:- session_destroy(); var_dump($_SESSION['login_user']);
and
unset($_SESSION['login_user']); var_dump($_SESSION['login_user']);
AND in your code

if (!isset($_SESSION)){
}

you should not keep it blank. I think you forgot to include the following code inside this block

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top