Jump to content

Think i'm losing the plot! - session help


Mr Chris

Recommended Posts

Afternoon,

 

Not sure if i've been staring at the screen for too long or not, but have a log in script which saves the password as a session variable and I can pass that variable across my page like so:

 

<?php 
  session_start();
  //Print_r ($_SESSION);
  echo "Erm, I have reached page.php and the password is ".$_SESSION['password'];
?>

 

However, I want to make this secure, so I added a function to it which when you log in says if $_SESSION['password'] does not exist then send me away, like so:

 

<?php 
session_start();

function checklogin()
{
session_start();
  if(!isset($_SESSION['password']))
{
       header("location: /index/");
       exit;
}
}

  checkLogin();
  Print_r ($_SESSION);
  echo "Erm, I have reached page.php and the password is ".$_SESSION['password'];
?>

 

However, using the function and a valid log-in it (as shown above) it chucks me out of the page even though $_SESSION['password'] exists?

 

Am I doing something really stupid here?

 

Thank you

Link to comment
Share on other sites

Are you doing this on a system with error_reporting set to E_ALL and display_errors set to ON so that all the php errors would be reported and displayed? At a minimum, your code should be producing a warning at the second session_start() statement and might in fact be producing errors that would point to the reason why it is not working as expected.

Link to comment
Share on other sites

Thanks, but yes I have error reporting on (did not notice the double session, so thanks for that!) but even if I comment out the redirect and replace it with a "I am not aware of any session" it prints that, even though $_SESSION['password'] exists?

 

<?php 
ini_set('display_errors', 1);
ini_set('log_errors', 1);
ini_set('error_log', dirname(__FILE__) . '/error_log.txt');
error_reporting(E_ALL);

function checklogin()
{
session_start();
  if(!isset($_SESSION['password']))
{
       echo "I aint aware of any session!";
}
}

  checkLogin();
  Print_r ($_SESSION);
  echo "Erm, I have reached page.php and the password is ".$_SESSION['password'];
?>

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.