pankirk Posted August 28, 2009 Share Posted August 28, 2009 I wrote this login code and I'm using cookies instead of session. Basically my problem is that when the users login is correct it sets the cookie but doesnt redirect until I login a second time. {//grab user id from database; while($getId = $checkLogin->fetch_assoc()) { $cookieId = $getId['id']; } setcookie("username", $cookieId, time()+3600); if(isset($_COOKIE['username'])) {//redirect if($redirect != "none") { redirect($redirect); } else if($redirect == "none") { redirect("user.php?action=home"); } } else if(!isset($_COOKIE['username'])) { PullBox("main", "red", "Failed to register correctly."); } } Thats the part of the code that I wrote for the login. You can see that I set the cookie after pulling the users ID out of the database but apparently it's not getting set because it goes straight to the "if(!isset($_COOKIE['username']))" where I need it to be set to "if(isset($_COOKIE['username']))" in order for the users to be redirected to the home page. Thanks in advance! Link to comment https://forums.phpfreaks.com/topic/172313-cookie-not-being-set/ Share on other sites More sharing options...
rhodesa Posted August 28, 2009 Share Posted August 28, 2009 $_COOKIE get's set when the script initiates...so after using setcookie(), $_COOKIE doesn't update (until the page reloads). after you set the cookie, just set a variable like $safe and then use: if(isset($_COOKIE['username']) || $safe) Link to comment https://forums.phpfreaks.com/topic/172313-cookie-not-being-set/#findComment-908539 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.