migi0027 Posted April 18, 2012 Share Posted April 18, 2012 <?php function Login() { session_start(); $_SESSION["loggedin"] = true; echo "You are logged in!"; header("Location: ../L_index.php"); //Aka Logged in Index. } function Logout() { $_SESSION["loggedin"] = false; session_destroy(); } function checklogin() { session_start(); if ($_SESSION["loggedin"] == true) { return true; } else { return false; } } ?> This is just a tiny bit of my code, there is security holes, but as you see, there is 3 functions: Login Logout Checklogin But when i use the Login function, everything is fine, but if i then use the checklogin, then it always returns true(it returns true after i used the logout function), why? Quote Link to comment https://forums.phpfreaks.com/topic/261194-_session-variable-checking-does-not-work/ Share on other sites More sharing options...
AyKay47 Posted April 18, 2012 Share Posted April 18, 2012 it returns true because the Login() function sets $_SESSION["loggedin"] to true when called. Quote Link to comment https://forums.phpfreaks.com/topic/261194-_session-variable-checking-does-not-work/#findComment-1338539 Share on other sites More sharing options...
migi0027 Posted April 18, 2012 Author Share Posted April 18, 2012 it returns true because the Login() function sets $_SESSION["loggedin"] to true when called. I know that , but when i call the logout function and then call the check login, it still returns true. Quote Link to comment https://forums.phpfreaks.com/topic/261194-_session-variable-checking-does-not-work/#findComment-1338542 Share on other sites More sharing options...
creata.physics Posted April 18, 2012 Share Posted April 18, 2012 add session_start to the beginning of the logout function and try it again. Quote Link to comment https://forums.phpfreaks.com/topic/261194-_session-variable-checking-does-not-work/#findComment-1338550 Share on other sites More sharing options...
creata.physics Posted April 18, 2012 Share Posted April 18, 2012 Actually, I just ran your code and everything works fine. <?php function Login() { session_start(); $_SESSION["loggedin"] = true; } function Logout() { $_SESSION["loggedin"] = false; session_destroy(); } function checklogin() { if ($_SESSION["loggedin"] == true) { echo "You are logged in."; } else { echo "You are logged out."; } } Login(); checklogin(); logout(); checklogin(); ?> Output: You are logged in.You are logged out. Results were exactly as expected. Are each of these functions being called from the same file. If not then how do you have your script set up to run these functions? Quote Link to comment https://forums.phpfreaks.com/topic/261194-_session-variable-checking-does-not-work/#findComment-1338556 Share on other sites More sharing options...
migi0027 Posted April 18, 2012 Author Share Posted April 18, 2012 add session_start to the beginning of the logout function and try it again. Thank you so mouch, see, i knew i had done something stupid! (Added start session) Quote Link to comment https://forums.phpfreaks.com/topic/261194-_session-variable-checking-does-not-work/#findComment-1338560 Share on other sites More sharing options...
AyKay47 Posted April 19, 2012 Share Posted April 19, 2012 it returns true because the Login() function sets $_SESSION["loggedin"] to true when called. I know that , but when i call the logout function and then call the check login, it still returns true. you did not state this at all in your OP. Quote Link to comment https://forums.phpfreaks.com/topic/261194-_session-variable-checking-does-not-work/#findComment-1338708 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.