I'm sure there must be a perfectly simple explanation to this, which is staring me in the face, but I presently can't see it and hopefully someone can point me in the right direction 🙂
I am trying to set a cookie based on a form submission. I set the cookie, but my page doesn't recognise that the cookie has been set until I reload the page (even though in my browser, I can see that the cookie has been set). So I tried to do a redirection back to the same page, but then the cookie doesn't set at all.
This is what I have:
if(!isset($_COOKIE['KSPHPUserID']) && isset($_POST['abluserid']) && $_POST['abluserid'] != "null"){
$user = $_POST['abluserid'];
//echo 'Userid: ' . $user;
setcookie("KSPHPUserID",$user);
$_POST['abluserid'] = "null";
//header("Location: Forum.php?forum=4");
//exit();
}
echo 'cookie is: ' . $_COOKIE['KSPHPUserID'];
If I use
header("Location: Forum.php?forum=4");
then the cookie doesn't set at all. If comment it out, the cookie sets (as I can see it in the browser settings), but at the
echo 'cookie is: ' . $_COOKIE['KSPHPUserID'];
it prints nothing. If I resubmit the page, only then does the cookie value print.