tags:

views:

260

answers:

6

I login in my system and set a cookie this way:

setcookie("hello",true,time()+3600);

Then I look in the cookie manager of firefox and see that my cookie is set.

When I restart my browser and restart I see in the cookie manager that the cookie is ther but this code:

if(isset($_COOKIE['hello'])){

         echo "yes"; exit;}

I don't see anything it redirects me to the login form. Does anyone know what's going on?

+3  A: 

While a regular cookie, like your 'hello' cookie, will survive a browser restart, a session cookie will not.

It sound like you are using a session cookie for your login, so when you hit the page after a restart, you need to log in again.

Paul Dixon
+3  A: 

Some things to try:

  1. Explicity set the path attribute of setcookie to / so the cookies are available in the entire domain.
  2. Instead of checking isset, try doing a print_r of $_COOKIE in an otherwise empty PHP file.
  3. The value part of setcookie expects a string, not a boolean. I would imagine as it is right now hello is being set to 1, although this wouldn't really matter on whether it is surviving a restart or not just thought I'd point it out.
  4. Do you have your browser set to delete cookies on exit? In Firefox, for example, you can go to Tools -> Options -> Privacy and there is an option that says "Always clear my private data when I exit Firefox." If you have this set any cookies you have will be deleted when you close your browser.
  5. Have you checked access to the cookie from PHP prior to the restart? You say you can see it in Cookie Manager before and after, but can you see it in PHP before or after?
Paolo Bergantino
A: 

@Paolo Thanks for your 5 tips but the first one was enough. It was the solution

sanders
A: 

When I want to remove a Cookie I try

unset($_COOKIE['hello']);

I see in my cookie browser from firefox that the cookie still exists. How can I really remove the cookie?

sanders
A: 

Sanders: Using the first example, just change your time to: -3600 instead of +3600 so it expires in the past: time()-3600 instead of time()+3600

A: 

How to restore the sessions, when the close the browser?

Ramesh