samayo
samayo

Reputation: 16495

cookie getting lost between page navigation

I have a cookie set like this:

$_COOKIE['admin'] = 'foo';

Now the first time, I can see this cookie getting serialized using var_dump($_COOKIE['admin']) So, I removed that cookie and just placed this instead.

if(isset($_COOKIE['admin']){
  echo 'hello admin';
}else{
  echo 'hello visitor';
}

Normally this should work for all pages, but it only works once. Meaning, if I browse index page, it works, if I navigate to other page (same website) then comeback to the index page, the cookie gets lost. And there is nothing to destroy/unset any cookie/session in any page.

What could be the problem here

Upvotes: 0

Views: 3067

Answers (2)

IamGhale
IamGhale

Reputation: 1240

To make cookie work in all pages, use like this

$value = 'foo';
setcookie('admin', $value, time() + (60 * 60 * 24));

Now, a cookie named 'admin' with value 'foo' will be available for 1 day. The path parameter is optional. But if you set it to "/", it will be availabel within entire domain.

Upvotes: 3

Scott Helme
Scott Helme

Reputation: 4799

I think you're supposed to set cookie values like this:

setcookie("name","value", $time, "/");

This is covered here in the PHP docs.

Upvotes: 4

Related Questions