session_name() not working, what am I doing wrong?

I'm trying to use session_name() to give my session a specific name on test2.php:

<?php
session_name('nd'); // Created a session named 'nd'
session_start();

if(!isset($_SESSION['name'])) {
    $_SESSION['name'] = "John Doe";
    echo "Created a session ... saved John Doe's name.<br/>";
} else {
    echo "There's already a session --> name=" . $_SESSION['name'] . '<br/>';
}

print_r($_COOKIE);
?>

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

Open in new window


Then I'm trying to retrieve the value I stored in test3.php:

<?php
session_id('nd');
session_start();

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

print_r($_COOKIE);
?>

Open in new window


For some reason, test3.php is not getting the 'name' variable I stored in test2.php, even though the session ID is identical as evidenced by my print_r of the $_COOKIE array.

Can anyone tell me what I'm doing wrong? Thanks.
elepilAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Chris StanyonWebDevCommented:
You're using session_name() in the first script and session_id() in the second. Try using session_name() in both.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
elepilAuthor Commented:
DOH! I'm getting tired. :( I should take a break and eat dinner. Thanks!
Dave BaldwinFixer of ProblemsCommented:
You need to use session_name('nd'); in both files to get session_start(); to look for the correct session.  session_id('nd'); in the second file is actually changing the 'id' to something different than was generated in the first file.
Dave BaldwinFixer of ProblemsCommented:
Just a note.  If you are ever using PHP on IIS, the Windows web server, you will see that there is always an ASPSESSION id also that is set by the web server in addition to your PHP session id.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.