Jump to content
Larry Ullman's Book Forums

Recommended Posts

Hey guys! 

In chapter 12, cookies and sessions, we change the cookies to sessions. I've done this but now, whenever I log in, it redirects me to the index page, logged out again. I've added the


to every page necessary - the login, loggedin, index, other pages, loguit, all of them! And it doesn't work :( I've done it exactly as in the book, checked it several times. Has anyone else had this issue?

Link to comment
Share on other sites

This is a common issue when first starting with sessions. It'll take a bit of debugging work to figure out where the problem is. Often the cause is inconsistent subdomains--www.example.com to example.com--but if you weren't having a problem with cookies, this isn't likely the cause. 

Working this backwards and looking at the code, here's the key bit of loggedin.php:

session_start(); // Start the session.

// If no session value is present, redirect the user:
if (!isset($_SESSION['user_id'])) {

So you're _only_ going to be redirected if there is no stored $_SESSION['user_id'] value. There's just two causes for that:

  • The session has changed (i.e., the value isn't stored in this session).
  • The session has remained the same but the value wasn't properly stored (e.g., used the wrong code). 

To debug the first, print out the session ID or look at the session cookie value on each page. Because of redirects, you'll want to do it like so:

echo session_id();

If the session ID isn't changing, then it's probably just a typo somewhere in the storing or referencing the $_SESSION['user_id']. 

Link to comment
Share on other sites


  • Create New...