8protons 8protons - 5 months ago 8
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');
exit();


File B has a conditional such that:

<?php
if ($login_order_submitted === true) {
?>
<script>
alert('Order Successfully Submitted!');
</script>
<?php
/* 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
true
in the file (File A) that redirects to it?

The code for File X is below.

<?php
include('db_const.php');
// 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
$user_check=$_SESSION['login_user'];
// 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;
if(!isset($login_user_name)){
mysqli_close($connection); // Closing Connection
header('Location: index.php'); // Redirecting To Home Page
}
?>

Answer

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