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
https://forums.phpfreaks.com/topic/217000-think-im-losing-the-plot-session-help/
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.

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'];
?>

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.