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?