paulgavrikov
paulgavrikov

Reputation: 1891

PHP cookie not being set

if I run this:

$today = date('Y-m-d H:i:s');
$expire = time()+(20*60);
$ssid = md5(rand()*1000000000);
setcookie('id', $ssid, $expire) or die("couldn't set cookie. login failed."); 

It always dies. Any ideas why?

Upvotes: 1

Views: 2746

Answers (2)

Mob
Mob

Reputation: 11098

The cookie headers should be set before anything. Remove the die() function and you should get a headers already sent error.

Upvotes: 1

David Schwartz
David Schwartz

Reputation: 182743

You must set the cookie before doing anything else that produces output. Once the HTTP headers are sent, it's too late to set the cookie.

Upvotes: 9

Related Questions