Session not getting detected, what am I doing wrong?

In test2.php, I have this:

<?php
/*
 * Check if there's already a session. If not, create one.
 */
if(session_id() == '' || !isset($_SESSION)) {
    session_start();
    $_SESSION['name'] = "John Doe";
    echo "Created a session ... saved John Doe's name.<br/>";
} else {
    echo "There's already a session --> name=" . $_SESSION['name'];
}
?>

<html>
    <head>
    </head>
    <body>
        <a href="test3.php">Click to go to test3.php</a>
    </body>
</html>

Open in new window


In test3.php, I have this:

<?php

if(session_id() == '' || !isset($_SESSION)) {
    echo "No session detected";
} else {
    echo "Session detected --> name=" . $_SESSION['name'];
}

?>

Open in new window


When I run test2.php, it will say "Created a session ... saved John Doe's name.". I then click the link on that same page to redirect to test3.php, but the latter says "No session detected".

Can someone tell me what I'm doing wrong, please?

Thanks.
elepilAsked:
Who is Participating?
 
Ray PaseurCommented:
With sessions, there is persistent data and so we cannot reproduce what may have gone before as you tested these scripts.  However I can show you the right way to handle the PHP session.  These are untested code examples, but they are correct in principle.

In test2:
<?php
error_reporting(E_ALL);
session_start();
$_SESSION['name'] = 'John Doe';

Open in new window


In test3:
<?php
error_reporting(E_ALL);
session_start();
var_dump($_SESSION);
echo PHP_EOL . 'name: ' . $_SESSION['name'];

Open in new window

0
 
Dave BaldwinFixer of ProblemsCommented:
There is never a reason to put 'session_start();' in an 'if' statement.  session_start(); will always work correctly.  It will either resume an existing session or it will create a new one.  Ray's example is the right way.

And in the second example, 'test3.php', there is no 'session_start();' which is required to establish a session.

Every time we get this question where 'session_start();' is put in an 'if' statement (and there have been a lot over the years), we have to get people to use it like Ray shows above.
0
 
Ray PaseurCommented:
Please explain the marked-down grade.  What did we do wrong here?
0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

 
elepilAuthor Commented:
I marked down the grade because the answer wasn't to-the-point to my inquiry. I gave two code snippets that demonstrated clearly how I was creating a session on one, and trying to detect its presence/absence on the other. I was using if(session_id() == '' || !isset($_SESSION)) to test the existence of a session, no one remarked on it, so I was left wondering if that was even valid.

I eventually had to decide for MYSELF that I should check it this way:

session_start();
if (isset($_SESSION['name'])) {
}

Open in new window


I assess the grade to give responses based on:

1) How closely and directly it addressed my question;
2) How thorough the response was.

In both of the above criteria, I was left still thinking how I should then approach the testing of the existence of the session. Your response pointed me in the right direction, but it could've been more direct and thorough.
0
 
Ray PaseurCommented:
Speaking as a professional programmer, we don't test for the existence of the session; we assume it's there and working correctly.  This is like testing to see if the computer can add or testing for the existence of electric power before turning on the lights.  Nobody does that.  When the power goes off, you will know it.  Same for the session.

The right way is what we showed you here.  In my experience I have enough work just testing my own code -- I don't need to test PHP's code, too.  Please believe me when I tell you that they already tested it for you!
0
 
elepilAuthor Commented:
Ray, I'm new to PHP. This whole thing started because I created a session variable on one page:

session_start();
$_SESSION['name'] = "John Doe";

Open in new window


and could not access it on the next page. What I didn't know was I had to always issue a session_start(), thinking it's something I have to do only for the first time I use the session. Hence, it led me on the quest to test whether or not a session exists.

Your comparing my inquiry to testing the "existence of power", about having to test PHP code ... is just obnoxiously way off the mark and a bit condescending for my taste.
0
 
Ray PaseurCommented:
Well, that's why I wrote the article -- to help folks new to PHP understand the principles needed to use the PHP session.  You could dig all of this out of the PHP online manual, but it's a lot of reading and it's not very well organized, especially not when it comes to identifying things like the requirement to use session_start() in every script that needs the session.

Not trying to be condescending, just that we don't test for things once we understand the way they work.  PHP sessions really are much easier to use than they might seem at first!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.