8protons 8protons - 1 year ago 66
PHP Question

Why is my session variable not changing?

Consider File A, File B, and File X, where both File A and File B include the session instance that is File X.

File X has a variable initialized like so:

$login_order_submitted = false;

File A has a branch of code (that I know is being executed) as follows:

$login_order_submitted = true;
header('Location: FileB.php');

File B has a conditional such that:

if ($login_order_submitted === true) {
alert('Order Successfully Submitted!');
/* now reset the order submitted variable */
$login_order_submitted = false;

Why is my code in File B falling through (the script/alert isn't running) when it's being set to
in the file (File A) that redirects to it?

The code for File X is below.

// Establishing Connection with Server by passing server_name, user_id and password as a parameter
$connection = mysqli_connect(DB_HOST, DB_USER, DB_PASS, DB_NAME);
// Selecting Database

session_start();// Starting Session
// Storing Session
// SQL Query To Fetch Complete Information Of User
$ses_sql=$connection->query("SELECT store_name, store_id FROM Store WHERE store_id='$user_check'");
$row = $ses_sql->fetch_assoc();
$login_user_name =$row['store_name'];
$login_user_ID = $row['store_id'];
$login_order_submitted = false;
mysqli_close($connection); // Closing Connection
header('Location: index.php'); // Redirecting To Home Page

Answer Source

use $_SESSION["login_order_submitted"] instead of $login_order_submitted

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download