Mike
Mike

Reputation: 37

Query about PHP cookie

I'm new PHP guy and I'm using PHP cookie and I'm facing a problem that the cookie can not be set correctly. Here is the statement of Set Cookie

setcookie('cookieusername', $username, 100000);

and the statement of Get Cookie

$cookieusername = $_COOKIE["cookieusername"];

The problem is, the value of $_COOKIE["cookieusername"]; is not defined.

I don't know what the problem is. I have tired to set the cookie path to '/' but which was still not work.

Upvotes: 1

Views: 203

Answers (1)

Leri
Leri

Reputation: 12535

Instead of:

setcookie('cookieusername', $username, 100000);

you have to do:

setcookie('cookieusername', $username, time() + 100000);

The reason is that the third parameter is the expiry time (as a Unix timestamp (number of seconds since the epoch)), not the time until expiry. And here's the link of manual.

Upvotes: 11

Related Questions