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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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).

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.