How to set and get a cookie

Set a cookie in the browser

$name = 'userid'; 
$value = '87'; 
$expire = strtotime('+1 year'); 
$path = '/'; 
setcookie($name, $value, $expire, $path);

Get the value of a cookie from the browser

$userid = filter_input(INPUT_COOKIE, 'userid', FILTER_VALIDATE_INT); // returns 87, the value stored in the userid field

Delete a cookie from the browser

$expire = strtotime('-1 year'); 
setcookie('userid', '', $expire, '/');

Description

Back