Tom8001 Posted November 24, 2014 Share Posted November 24, 2014 On my login page i have a set a session when the user logs in called loggedIn and it = true if($count == 1) { $_SESSION['username']; $_SESSION['password']; $_SESSION['loggedIn'] = true; header("Location: index.php"); and on my index page i have session_start(); if(!isset($_SESSION['loggedIn'])) { echo "You are not currently logged in and to view this page you must be logged in to have access."; die(); } but when the user logs in it comes up with the message above echo "You are not currently logged in and to view this page you must be logged in to have access."; which obviously it should not do. Does anyone know how i can fix this? Quote Link to comment https://forums.phpfreaks.com/topic/292685-i-have-a-question/ Share on other sites More sharing options...
Barand Posted November 24, 2014 Share Posted November 24, 2014 you may as well remove these two lines, they don't do anything. $_SESSION['username']; $_SESSION['password']; Have you got session_start() at the top of your login page? Quote Link to comment https://forums.phpfreaks.com/topic/292685-i-have-a-question/#findComment-1497516 Share on other sites More sharing options...
Tom8001 Posted November 24, 2014 Author Share Posted November 24, 2014 ok i removed them and should i add session_start(); ? Quote Link to comment https://forums.phpfreaks.com/topic/292685-i-have-a-question/#findComment-1497517 Share on other sites More sharing options...
Tom8001 Posted November 24, 2014 Author Share Posted November 24, 2014 I removed them and added session_start(); and i still have the same error Quote Link to comment https://forums.phpfreaks.com/topic/292685-i-have-a-question/#findComment-1497519 Share on other sites More sharing options...
Solution Ch0cu3r Posted November 24, 2014 Solution Share Posted November 24, 2014 Before header("Location: index.php"); add the following line session_write_close(); Is the data present in the session in index.php? Use the following to check printf('<pre>%s</pre>', print_r($_SESSION, 1)); Quote Link to comment https://forums.phpfreaks.com/topic/292685-i-have-a-question/#findComment-1497522 Share on other sites More sharing options...
Tom8001 Posted November 24, 2014 Author Share Posted November 24, 2014 I'm kinda new to PHP what does session_write_close(); mean? Quote Link to comment https://forums.phpfreaks.com/topic/292685-i-have-a-question/#findComment-1497524 Share on other sites More sharing options...
Tom8001 Posted November 24, 2014 Author Share Posted November 24, 2014 it logged in and said Array ( [loggedIn] => 1 ) Quote Link to comment https://forums.phpfreaks.com/topic/292685-i-have-a-question/#findComment-1497526 Share on other sites More sharing options...
Ch0cu3r Posted November 24, 2014 Share Posted November 24, 2014 'm kinda new to PHP what does session_write_close(); mean? It will write the data currently stored in the session to disk. This is done automatically when PHP reaches the end of the script. If you ever come across functions you don't know then always look at their documentation at php.net. TIP: You can type any function name after php.net/ to see their documentation. Example urls php.net/session_write_close php.net/session_start it logged in and said So data is being saved. Then you should not be getting the "You are not currently logged in ..." message? Quote Link to comment https://forums.phpfreaks.com/topic/292685-i-have-a-question/#findComment-1497527 Share on other sites More sharing options...
Tom8001 Posted November 24, 2014 Author Share Posted November 24, 2014 It is working now i think the session_write_close(); Fixed it Quote Link to comment https://forums.phpfreaks.com/topic/292685-i-have-a-question/#findComment-1497532 Share on other sites More sharing options...
Tom8001 Posted November 24, 2014 Author Share Posted November 24, 2014 Just another question what is <pre>%s</pre> ? Quote Link to comment https://forums.phpfreaks.com/topic/292685-i-have-a-question/#findComment-1497533 Share on other sites More sharing options...
Barand Posted November 24, 2014 Share Posted November 24, 2014 (edited) http://php.net/manual/en/function.printf.php Edited November 24, 2014 by Barand Quote Link to comment https://forums.phpfreaks.com/topic/292685-i-have-a-question/#findComment-1497535 Share on other sites More sharing options...
Tom8001 Posted November 24, 2014 Author Share Posted November 24, 2014 Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/292685-i-have-a-question/#findComment-1497536 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.