Cookies with Remember Me in PHP?

waveparticle

Active Member
Joined
Dec 4, 2013
Messages
96
Reaction score
0
Hi people,

So I'm trying to make a remember me checkbox in my login system but it doesn't work. Here's my code:

PHP:
if($checkbox== TRUE){
			setcookie('lolfish', $bob['lolfish'], time() +10);
			header('Location: ../index.php');
}

So I did that and the cookie gets set. But when I check if the Cookie is set to modify my html, it doesn't work.

Let's say I need to see if the Cookie is checked to enter user panel, it doesn't work but the cookie is set!

Here is the code I used to check if cookie is set:

PHP:
if(isset($_COOKIE['lolfish'])){
       echo "LOL FISH!";
}

It doesn't echo though.

I appreciate your help.

Thanks.
 
time() +10

^ so you want to let it expire in 10 seconds

I don't want to grow old testing if my cookie is working. :whistle: And I'm too lazy to clear cookies :P

Do a print_r($_COOKIE); on the "index.php" to dump the cookies to the screen.

This is what I get:

PHP:
Array ( [PHPSESSID] => v2d1gz5ea2ok12g65ugo0e32a5 )

I'm not sure what's next.
 
Last edited:
something to note.

You cant set a cookie and then use the cookie straight away without a page refresh using straight PHP.
So if you are setting the cookie, on the same page you need to refresh it properly.

So when you check the box have it set the cookie on "rememberme.php" and then redirect it back to index.php using the header




The above obviously assumes you are only using index.php
 
time() +10

^ so you want to let it expire in 10 seconds

Sure it's not 10 milliseconds? (I'm guessing, I'm not big on PHP)

OP just make sure the browser doesn't delete the cookie before you get to checking its existence.
 
@waveparticle I don't believe the array "$bob" has a value for the key 'lolfish' and this is why your code is failing.

setcookie discards setting the value of NULL as well and actually deletes the cookie when you do so.

So check that $bob['lolfish'] is not NULL or allocate a value to it.
 
Sure it's not 10 milliseconds? (I'm guessing, I'm not big on PHP)

OP just make sure the browser doesn't delete the cookie before you get to checking its existence.

I had to double check, it is seconds and not milliseconds (I also thought it would be milliseconds but that is the lovely inconsistency of PHP)
 
Top
Sign up to the MyBroadband newsletter
X