user1642439
user1642439

Reputation: 89

How to get a cookie

I have this code

<?php
setcookie('page', 'settings', time(),'/');
header('Location: settings.php');
?>

but every time I check the cookie with $_COOKIE['page'] is empty?

Upvotes: 2

Views: 49

Answers (2)

Gung Foo
Gung Foo

Reputation: 13558

The cookie you are sending expires at the moment you send it.

setcookie("TestCookie", $value, time()+3600);  /* expire in 1 hour */

Upvotes: 1

Mihai Iorga
Mihai Iorga

Reputation: 39704

Your cookie always expires, use:

setcookie('page', 'settings', time()+3600,'/'); // + 1 hour

also add a an exit for your script in case you have something bellow header:

<?php
    setcookie('page', 'settings', time()+3600, '/');
    header('Location: settings.php');
    exit();
?>

Upvotes: 5

Related Questions