Jump to content

setcookie working only after refresh


xProteuSx

Recommended Posts

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?

Link to comment
https://forums.phpfreaks.com/topic/262759-setcookie-working-only-after-refresh/
Share on other sites

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.

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?!

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.