3

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
}
?>
4
  • Have you checked if they are actually being set? By var dumping the session straight after it was set? It could be empty? Are you working local? Could be some config errors. Maybe your browser does not allow Session? Commented Sep 2, 2013 at 4:21
  • tried it on several other browsers, none work. moved around the code so it was outside of if statements, still doesn't work Commented Sep 2, 2013 at 5:21
  • If that was your real password, you might want to change it, just in case. Commented Sep 2, 2013 at 23:18
  • password and database names are not the real ones Commented Sep 3, 2013 at 2:10

3 Answers 3

4

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?
Sign up to request clarification or add additional context in comments.

3 Comments

echo $name displays the appropriate user name
just did some checking and the session variables are defined properly, and are showing up on the page when I do an echo test. They disappear after a redirect to another page (which also has a session_start() on it)
In your php.ini search for error_reporting and be sure it is set to be E_ALL | E_STRICT
0

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).

1 Comment

setting them on the top of the page makes them work properly, but doesn't help going to other pages
0

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;

1 Comment

happening inside or outside the if condition

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.