pugboy Posted June 7, 2008 Share Posted June 7, 2008 For some reason, the code below doesn't work when I navigate to index.php?do=setsession It seems that the session is not being set, or something weird... <?php $do = $_GET["do"]; $type = $_POST["type"]; $user = $_SESSION["user"]; if(!$user=="admin"){ echo "LOGGED OUT"; } if($do=="setsession"){ session_start(); $_SESSION["user"] = "admin"; } ?> Is there something wrong with my code? Quote Link to comment https://forums.phpfreaks.com/topic/109087-solved-sessions-not-working/ Share on other sites More sharing options...
.josh Posted June 7, 2008 Share Posted June 7, 2008 session_start() needs to be at the top. You are assigning a session variable to $user ... Quote Link to comment https://forums.phpfreaks.com/topic/109087-solved-sessions-not-working/#findComment-559616 Share on other sites More sharing options...
pugboy Posted June 7, 2008 Author Share Posted June 7, 2008 Still doesn't work with this code: <?php if($do=="setsession"){ session_start(); $_SESSION["user"] = "admin"; } $do = $_GET["do"]; $type = $_POST["type"]; $user = $_SESSION["user"]; if(!$user=="admin"){ echo "LOGGED OUT"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/109087-solved-sessions-not-working/#findComment-559620 Share on other sites More sharing options...
.josh Posted June 7, 2008 Share Posted June 7, 2008 okay because now you moved your $do assignment below your condition so $do will never equal setsession and you still have session_start inside the condition which will always fail now, so $user will never be assigned your session variable. just put your code back to the way it was before, except move the session_start out of the condition to the top of your page. Quote Link to comment https://forums.phpfreaks.com/topic/109087-solved-sessions-not-working/#findComment-559623 Share on other sites More sharing options...
pugboy Posted June 7, 2008 Author Share Posted June 7, 2008 Oh. I am so careless EDIT: Says I am Logged out if I go to the regular index.php (without the query string)... Shouldn't it stay logged in and not display the logged out text? EDIT2: Oh... So the session start always has to be at the top... Quote Link to comment https://forums.phpfreaks.com/topic/109087-solved-sessions-not-working/#findComment-559624 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.