Marshall Tigerus
Marshall Tigerus

Reputation: 3764

Session Variables not Persisting

I have a few session variables that I am trying to use in my application, however, I am unable to get them to show up on the pages I need them to.

This is the code that sets them (I have manually assigned them values as well, so it isn't the database pull that is the problem):

if ($name != ""){
    $_SESSION['name'] = $name;
    $_SESSION['id'] = $user_id;
}

I start that page with a session_start();, as I do on all the pages that will be using the session variables.

When I try to call the session variables on another page, they no longer exist, even if that is the page the one that assigns the values redirects to.

This is how I am trying to call the session variables:

 $name = $_SESSION['name'];
 $user_id = $_SESSION['id'];

why would it be doing this?

EDIT: To help I'm including the rest of my code for that page. The database connection portions work fine, they are identical to what I use eveyrwhere else.

 <?php
 session_start();

 define('DB_SERVER', '<server>');
 define('DB_USER','<db>');
 define('DB_PASSWORD' , '<password>');
 define('DB_NAME' , '<db-name>');

 $conn = new mysqli(DB_SERVER, DB_USER, DB_PASSWORD, DB_NAME) or 
        die('There was a problem connecting to the database');

 $stmt = "Select User.user_id, User.name from User where User.username = '" .       
 $_POST["username"] . "' AND User.password = '" . $_POST["pwd"] . "';";

 if(!$result = $conn->query($stmt)){
die('there was an error retrieving the information');
 }

$row = $result->fetch_assoc();
$name = $row['name'];
$user_id = $row['user_id'];

$_SESSION["name"] = $name;
$_SESSION["id"] = $user_id;

if ($name != ""){

$conn->close();
?>
<script type="text/javascript">
<!--
window.location = "store.php"
//-->
</script>

<?php
}
else{
?>
<script type="text/javascript">
<!--
window.location = "register.php"
//-->
</script>
<?php
}
?>

Upvotes: 3

Views: 479

Answers (3)

SaidbakR
SaidbakR

Reputation: 13534

There is only two probabilities:

  1. You did not started session before any output.
  2. The $name is already empty or null.

You have to do the following to debug:

  1. echo $name before the if conditional.
  2. error_reporting(E_ALL); or checkout this question: How to get useful error messages in PHP?

Upvotes: 4

user2064468
user2064468

Reputation:

In PHP you must need to start session using session_start() then only you can user session variables.

And also try to debug, Does your if condition satisfied or not. In your case if your if condition does not satisfied then it is not possible your below code will execute.

$_SESSION['name'] = $name;
$_SESSION['id'] = $user_id;

Upvotes: 0

abu
abu

Reputation: 51

As a debugging technique, try setting the session values ON the page that you're trying to call them. For example, set them on the top of the page and then try outputting them somewhere else below on the page and see if that works. If it does, then it's obvious that the variables aren't being set on the previous page(s).

Upvotes: 0

Related Questions