turpentyne Posted September 22, 2010 Share Posted September 22, 2010 I'm building a login page that then redirects to a new page. But it seems the session isn't working. But I don't know much about sessions, so I'm not sure what I might be doing wrong. The login page sets the session with this code: // Fetch the result - This will tell us whether this user exists $userExists = mysql_fetch_assoc($doQuery); // If user exists log them in if($userExists){ mysql_free_result($doQuery); mysql_close(); // Set the SESSION variables $_SESSION['first_name'] = $userExists['first_name']; $_SESSION['id'] = $userExists['id']; The page that it redirects to is this: <?php session_start(); if(isset($_SESSION['id'])) { echo "<html><body><p> You are now logged in,{$_SESSION['ID']}.</p>"; } else { echo 'something went wrong'; exit(); } ob_end_flush(); ?> Link to comment https://forums.phpfreaks.com/topic/214078-session-isnt-passing-to-new-page/ Share on other sites More sharing options...
Oziam Posted September 22, 2010 Share Posted September 22, 2010 I am no expert but have you tried to free and close after setting the sessions! // If user exists log them in if($userExists){ // Set the SESSION variables $_SESSION['first_name'] = $userExists['first_name']; $_SESSION['id'] = $userExists['id']; mysql_free_result($doQuery); mysql_close(); Link to comment https://forums.phpfreaks.com/topic/214078-session-isnt-passing-to-new-page/#findComment-1113980 Share on other sites More sharing options...
turpentyne Posted September 22, 2010 Author Share Posted September 22, 2010 I did that. Just didn't paste that part. Any other ideas? Link to comment https://forums.phpfreaks.com/topic/214078-session-isnt-passing-to-new-page/#findComment-1113989 Share on other sites More sharing options...
turpentyne Posted September 22, 2010 Author Share Posted September 22, 2010 hmmm. I think I figured it out. I opened sessions on the redirect page, but on the login page I hadn't. Seems to work now. Link to comment https://forums.phpfreaks.com/topic/214078-session-isnt-passing-to-new-page/#findComment-1113990 Share on other sites More sharing options...
rwwd Posted September 22, 2010 Share Posted September 22, 2010 On EVERY document you need to access this $_SESSION global you need to declare session_start(); in order for php to access that global; You can check as it is set, because $_SESSION is based on the same logic & behaves the same as a $_COOKIE - Check your cookie log, and you will see something called PHPSESSID, this is your session, as it is instantiated, you just need to instruct php with session_start(); so that access is given.. Rw Link to comment https://forums.phpfreaks.com/topic/214078-session-isnt-passing-to-new-page/#findComment-1113992 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.