xProteuSx Posted May 19, 2012 Share Posted May 19, 2012 I am working on a login script and I am using cookies for the first time. I have it something like this: <?php if (correct user/pass entered) - set user/pass cookies ?> <html> <?php if (user/pass cookies are set) {echo 'you are logged in';} else {echo 'you are NOT logged in';} ?> </html> The problem is that if I enter a valid username and password, and the cookies are set, then I get the message 'you are NOT logged in' unless I leave the page then return to it, or if I refresh, in which case I get the message 'you are logged in'. Its almost as though I cannot use the cookies until I navigate away from the page on which they were set. Am I doing something wrong, or is this the way it works? Quote Link to comment https://forums.phpfreaks.com/topic/262759-setcookie-working-only-after-refresh/ Share on other sites More sharing options...
trq Posted May 19, 2012 Share Posted May 19, 2012 This is normal behaviour. Quote Link to comment https://forums.phpfreaks.com/topic/262759-setcookie-working-only-after-refresh/#findComment-1346754 Share on other sites More sharing options...
xProteuSx Posted May 19, 2012 Author Share Posted May 19, 2012 So, Thorpe, is that to say that you can't use the cookies on the page they were created on unless you refresh or leave the page and come back? If so, is there a way around this? Quote Link to comment https://forums.phpfreaks.com/topic/262759-setcookie-working-only-after-refresh/#findComment-1346757 Share on other sites More sharing options...
trq Posted May 19, 2012 Share Posted May 19, 2012 So, Thorpe, is that to say that you can't use the cookies on the page they were created on unless you refresh or leave the page and come back? That is indeed the case. If so, is there a way around this? Once you create your cookie, redirect the page back to itself. Quote Link to comment https://forums.phpfreaks.com/topic/262759-setcookie-working-only-after-refresh/#findComment-1346760 Share on other sites More sharing options...
xProteuSx Posted May 19, 2012 Author Share Posted May 19, 2012 The redirect to self, using header("Location: index.html"); exit; did not work. However, I just made the form action="login.php" which processed the input, then redirected back to index.html. That worked. Thanks for the heads up. I wonder why you can't use cookies right away, like session variables?! Quote Link to comment https://forums.phpfreaks.com/topic/262759-setcookie-working-only-after-refresh/#findComment-1346764 Share on other sites More sharing options...
trq Posted May 19, 2012 Share Posted May 19, 2012 I wonder why you can't use cookies right away, like session variables?! Because they are not actually set until your script has finished executing and the headers are sent ( this is what actually sets the cookies). Quote Link to comment https://forums.phpfreaks.com/topic/262759-setcookie-working-only-after-refresh/#findComment-1346766 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.