narpassword000 Posted May 30, 2012 Share Posted May 30, 2012 I've spent some time on google hunting for a solution to my issue, but to no avail. I'm doing a PHP-based website for a school project, and have encountered a weird user experience glitch. When the user attempts to log into their account, they are sent to a new page where the login info is verified and if it correct, a cookie is set. Currently for testing purposes the cookie is just their name. A variable is then set to determine what to do later. <?php $loginfail = true; $fileContents = file_get_contents("users.txt"); $ids = json_decode($fileContents, true); foreach ($ids as $user) { if ($user['email'] == $_POST['email'] && $user['pass'] == $_POST['pwd']) { $loginfail = false; if (isset($_POST['remember'])) { $expire = time() + 60 * 60 * 24 * 30; setcookie("user", $user['first'], $expire); } else setcookie("user", $user['first']); break; } } ?> This is really basic user verification for testing purposes. Further down in the page, if the login variable was set to false then I used Javascript to redirect back to the homepage. <script language="javascript"><!-- var t = setTimeout("redir()", 2000); function redir() { location.replace("index.php"); }//--> </script> However, when I reach the homepage, the cookie isn't being read until after I refresh. I assumed that when I sent the user to a new page it should be considered a fresh load and the cookie would be accessible. Apparently I was wrong. Any advice? Quote Link to comment https://forums.phpfreaks.com/topic/263391-cookies-not-being-read-until-second-load/ Share on other sites More sharing options...
narpassword000 Posted May 30, 2012 Author Share Posted May 30, 2012 I've tested it a bit further. The cookie will not be read even if I change pages until I refresh the page, no matter which page I'm on. I had a thought. The code to read the cookie is stored in a separate file (header.inc) that is used for the banner, navigation bar and what-not in every page. I think this might be causing the issue...is there any way I can resolve this without having incorporate this file into every page? Is there a way to force the browser to reload the header when I change pages? It's just a basic <?php include "header.inc"; ?> at the top of the main div block. Quote Link to comment https://forums.phpfreaks.com/topic/263391-cookies-not-being-read-until-second-load/#findComment-1349901 Share on other sites More sharing options...
Pikachu2000 Posted May 30, 2012 Share Posted May 30, 2012 A file that has an extension of .inc will not be parsed by the php interpreter unless you've set up the server specifically to do so. Have you? If not, try changing the file extension to .php, and change the include() to correspond. Quote Link to comment https://forums.phpfreaks.com/topic/263391-cookies-not-being-read-until-second-load/#findComment-1349908 Share on other sites More sharing options...
requinix Posted May 30, 2012 Share Posted May 30, 2012 A file that has an extension of .inc will not be parsed by the php interpreter unless you've set up the server specifically to do so. Have you? If not, try changing the file extension to .php, and change the include() to correspond. Unless he include()s it, which he does. PHP assumes any file given there has PHP to execute regardless of extension. Using something that inspects headers (like Chrome or Firebug) can you see when the cookie is being set? Quote Link to comment https://forums.phpfreaks.com/topic/263391-cookies-not-being-read-until-second-load/#findComment-1349914 Share on other sites More sharing options...
Pikachu2000 Posted May 30, 2012 Share Posted May 30, 2012 You know, that's what I thought, so I tested it in 5.2.11 before I posted that, and when I included a .inc file, the raw php code was in the html source. Now that I look at the file again I see what I did, though. I accidentally left the ? out of the opening <?php tag. When I put it in there, the file was interpreted. So, yeah, you're right. My previous post can be safely disregarded. Quote Link to comment https://forums.phpfreaks.com/topic/263391-cookies-not-being-read-until-second-load/#findComment-1349949 Share on other sites More sharing options...
narpassword000 Posted June 6, 2012 Author Share Posted June 6, 2012 After a LONG delay, I've got the chance to try to work on this again finally. The cookie seems to be set immediately upon login, according to Chrome's cookie reading tool, but will not appear in-browser until the page is fully refreshed. Clicking a link that returns you to the same page does not work, nor does going to another page. I'm almost certain that this is because the header.inc is just being accessed through a local copy that doesn't get re-run until a basic refresh, or something like that, but I don't have the expertise (or beginner-ise, for that matter) to know if I'm right, or more importantly how to fix it. Quote Link to comment https://forums.phpfreaks.com/topic/263391-cookies-not-being-read-until-second-load/#findComment-1351656 Share on other sites More sharing options...
ejay Posted June 7, 2012 Share Posted June 7, 2012 If you are using header("Location: ....."); after setCookie(...) , cookie might not be set in all cases Quote Link to comment https://forums.phpfreaks.com/topic/263391-cookies-not-being-read-until-second-load/#findComment-1351834 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.