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

 

 

Link to comment
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.