Jump to content

Cookies not being read until second load.


narpassword000

Recommended Posts

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.