I have recently posted about this issue, but have done further testing and narrowed down what’s causing the issue. The first php script should log the user in, then set a session variable. It does both, and I am even able to print the $_SESSION with variable inside, but it is missing from subsequent requests.
The second script works perfectly fine, proving I am able to set session variables. I change variable names to test, and on subsequent requests its always there. I’ve never had such a simple problem completely stump me. Its driving me up the wall. What is the issue? Why does one work but the other not?
I have tried including all the things in the first one like taking a post among others, but nothing I do makes it persist.
<?php
session_start();
require_once('preflight.php');
require_once('db_connection.php');
require_once('cors.php');
$json_data = file_get_contents('php://input');
$data = json_decode($json_data, true);
$username = $data['username'];
$password = $data['password'];
$sql = "SELECT * FROM User WHERE username = '$username'";
$result = $conn->query($sql);
if ($result !== False && $result->num_rows === 1) {
$row = $result->fetch_assoc();
$hashed_password = $row['Password'];
if (password_verify($password, $hashed_password)) {
$_SESSION['name'] = $username;
$sql = "SELECT Email, Street, `First Name`, `Last Name`,`Phone Number`, `State`, Country, Username FROM User WHERE username = '$username'";
$result = $conn->query($sql);
if ($result->num_rows === 1) {
$user = $result->fetch_assoc();
echo json_encode($user);
} else {
echo json_encode(array("error" => "User could not be found in database"));
}
} else {
echo json_encode(array("error" => "There was an error in your submission"));
}
} else {
echo json_encode(array("error" => "There was an error in your submission"));
}
$conn->close();
?>
<?php
session_start();
require_once('cors.php');
require_once('preflight.php');
require_once('db_connection.php');
echo json_encode(array("success" => $_SESSION['username']));
$_SESSION['test'] = 'test';
$_SESSION['username'] = 'so done with this';
?>