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? 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 ... 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"; } ?> 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. 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... Link to comment https://forums.phpfreaks.com/topic/109087-solved-sessions-not-working/#findComment-559624 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.