Jump to content

session isn't passing to new page?


turpentyne

Recommended Posts

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

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();       

 

 

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.