Fredundant14 Posted June 30, 2012 Share Posted June 30, 2012 So lets say the user goes to my gallery page. It says "welcome guest, Login or Register" The links to the login page works fine. The registration page works fine, and the logging in works fine. The user is logged in. But then when they return to the gallery page it says... "welcome guest, Login or Register" They have automatically been logged out. What am I missing? <?php session_start(); $username = $_SESSION['username']; $password = $_SESSION['password']; if(!$username && !$password){ echo "Welcome Guest! <br> <a href=main_login.php>Login</a> | <a href=register.php>Register</a>"; }else{ echo "Welcome ".$username."; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/265058-users-dont-stay-logged-in/ Share on other sites More sharing options...
PFMaBiSmAd Posted June 30, 2012 Share Posted June 30, 2012 The most common reason navigating or redirecting around on a site does not carry session variables is because the URL's change between having and not having the www. on them and by default the session id cookie only matches the exact variation of your domain name where the session was first started. Do the URLs consistently use the same variation of your domain, all with or all without the www. in them? Quote Link to comment https://forums.phpfreaks.com/topic/265058-users-dont-stay-logged-in/#findComment-1358217 Share on other sites More sharing options...
Fredundant14 Posted June 30, 2012 Author Share Posted June 30, 2012 Thanks for a reply Yes, the URLs consistently use the same variation. Quote Link to comment https://forums.phpfreaks.com/topic/265058-users-dont-stay-logged-in/#findComment-1358220 Share on other sites More sharing options...
PFMaBiSmAd Posted June 30, 2012 Share Posted June 30, 2012 Do you have php's error_reporting set to E_ALL and display_errors set to ON so that any session_start() related errors will be reported and displayed? Quote Link to comment https://forums.phpfreaks.com/topic/265058-users-dont-stay-logged-in/#findComment-1358223 Share on other sites More sharing options...
Rage Posted July 1, 2012 Share Posted July 1, 2012 Just apply a login cookie to hold some data so they stay logged in Quote Link to comment https://forums.phpfreaks.com/topic/265058-users-dont-stay-logged-in/#findComment-1358305 Share on other sites More sharing options...
gizmola Posted July 1, 2012 Share Posted July 1, 2012 Just apply a login cookie to hold some data so they stay logged in That is exactly what php sessions already do. Quote Link to comment https://forums.phpfreaks.com/topic/265058-users-dont-stay-logged-in/#findComment-1358309 Share on other sites More sharing options...
gizmola Posted July 1, 2012 Share Posted July 1, 2012 Fredundant: I would suggest you put your login check into a single function that you include and use in all your pages. This will eliminate the possibility that a variable typo or some other logic error causes a variation. It will also help you pinpoint what to look for and what not to look for, as PFMaBiSmAd suggested. Last but not least, it's part of the DRY philosophy that all good programmers try and follow. Quote Link to comment https://forums.phpfreaks.com/topic/265058-users-dont-stay-logged-in/#findComment-1358311 Share on other sites More sharing options...
Fredundant14 Posted July 1, 2012 Author Share Posted July 1, 2012 I understand DRY and never do. It sometimes takes me a while to figure out exactly how to code the easiest way (if that makes sense). I'll try the function like you suggested and let you know the results. Thankyou Quote Link to comment https://forums.phpfreaks.com/topic/265058-users-dont-stay-logged-in/#findComment-1358424 Share on other sites More sharing options...
Fredundant14 Posted July 1, 2012 Author Share Posted July 1, 2012 Found the bug, It was my error. Die to coding in the check login page, the code needed to be: $username = $_SESSION['myusername']; $password = $_SESSION['mypassword']; Embarrassing I missed that. Thank you all for your help. Quote Link to comment https://forums.phpfreaks.com/topic/265058-users-dont-stay-logged-in/#findComment-1358426 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.