Link to home
Start Free TrialLog in
Avatar of elepil
elepil

asked on

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.
ASKER CERTIFIED SOLUTION
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Dave Baldwin
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.
Please explain the marked-down grade.  What did we do wrong here?
Avatar of elepil
elepil

ASKER

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.
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!
Avatar of elepil

ASKER

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