Mr Chris Posted October 27, 2010 Share Posted October 27, 2010 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 Quote Link to comment https://forums.phpfreaks.com/topic/217000-think-im-losing-the-plot-session-help/ Share on other sites More sharing options...
PFMaBiSmAd Posted October 27, 2010 Share Posted October 27, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/217000-think-im-losing-the-plot-session-help/#findComment-1127076 Share on other sites More sharing options...
Mr Chris Posted October 27, 2010 Author Share Posted October 27, 2010 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']; ?> Quote Link to comment https://forums.phpfreaks.com/topic/217000-think-im-losing-the-plot-session-help/#findComment-1127085 Share on other sites More sharing options...
PFMaBiSmAd Posted October 27, 2010 Share Posted October 27, 2010 $_SESSION['password'] exists ^^^ Obviously, not. What does var_dump($_SESSION); show, immediately after the session_start(); statement? Quote Link to comment https://forums.phpfreaks.com/topic/217000-think-im-losing-the-plot-session-help/#findComment-1127087 Share on other sites More sharing options...
Mr Chris Posted October 27, 2010 Author Share Posted October 27, 2010 Agh, worked it out thanks! Quote Link to comment https://forums.phpfreaks.com/topic/217000-think-im-losing-the-plot-session-help/#findComment-1127101 Share on other sites More sharing options...
anselk Posted October 27, 2010 Share Posted October 27, 2010 would you mind posting your code? Helps people figure out what they might be doing wrong if they come across this. - cheers Ansel Quote Link to comment https://forums.phpfreaks.com/topic/217000-think-im-losing-the-plot-session-help/#findComment-1127105 Share on other sites More sharing options...
Mr Chris Posted October 28, 2010 Author Share Posted October 28, 2010 Yep sure. Simply forgot to have session_start() on my opening page - my bad. One of those silly errors. Quote Link to comment https://forums.phpfreaks.com/topic/217000-think-im-losing-the-plot-session-help/#findComment-1127541 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.