PHP setcookie() Function
Example
The following example creates a cookie named "user" with the value "John Doe". The cookie will expire after 30 days (86400 * 30). The "/" means that the cookie is available in entire website (otherwise, select the directory you prefer).
We then retrieve the value of the cookie "user" (using the global variable $_COOKIE). We also use the isset() function to find out if the cookie is set:
<?php
$cookie_name = "user";
$cookie_value = "John Doe";
setcookie($cookie_name, $cookie_value, time() + (86400 * 30), "/"); // 86400 = 1 day
?>
<!DOCTYPE html>
<html>
<body>
<?php
if(isset($_COOKIE[$cookie_name])) {
echo "Cookie '" . $cookie_name . "' is set!<br>";
echo "Value is: " . $_COOKIE[$cookie_name];
} else {
echo "Cookie named '" . $cookie_name . "' is not set!";
}
?>
</body>
</html>
Definition and Usage
The setcookie() function defines a cookie to be sent along with the rest of the HTTP headers.
Note: The setcookie() function must appear BEFORE the <html> tag, since cookies are a part of the HTTP header.
The name of the cookie is automatically assigned to a variable of the same name. For example, if a cookie was sent with the name "user", a variable is automatically created called $user, containing the cookie value.
To retrieve the value of a cookie, use the superglobal variable
$_COOKIE.
Note: The value of the cookie is automatically URLencoded when sending the cookie, and automatically decoded when received (to prevent URLencoding, use setrawcookie() instead).
Syntax
setcookie(name, value, expire, path, domain, secure, httponly);
Parameter Values
| Parameter | Description |
|---|---|
| name | Required. Specifies the name of the cookie |
| value | Optional. Specifies the value of the cookie |
| expire | Optional. Specifies when the cookie expires. The value: time()+86400*30, will set the cookie to expire in 30 days. If this parameter is omitted or set to 0, the cookie will expire at the end of the session (when the browser closes). Default is 0 |
| path | Optional. Specifies the server path of the cookie. If set to "/", the cookie will be available within the entire domain. If set to "/php/", the cookie will only be available within the php directory and all sub-directories of php. The default value is the current directory that the cookie is being set in |
| domain | Optional. Specifies the domain name of the cookie. To make the cookie available on all subdomains of example.com, set domain to "example.com". Setting it to www.example.com will make the cookie only available in the www subdomain |
| secure | Optional. Specifies whether or not the cookie should only be transmitted over a secure HTTPS connection. TRUE indicates that the cookie will only be set if a secure connection exists. Default is FALSE |
| httponly | Optional. If set to TRUE the cookie will be accessible only through the HTTP protocol (the cookie will not be accessible by scripting languages). This setting can help to reduce identity theft through XSS attacks. Default is FALSE |
Technical Details
| Return Value: | TRUE on success. FALSE on failure |
|---|---|
| PHP Version: | 4+ |
| PHP Changelog: | PHP 8.2: The date format of the cookie is now 'D, d M Y H:i:s \G\M\T'. Before it was 'D, d-M-Y H:i:s T' |
More Examples
Example
Several expire dates for cookies:
<?php
$name = "user";
$value = "John Doe";
// cookie will expire when the browser close
setcookie($name, $value);
// cookie will expire in 1 hour
setcookie($name, $value, time() + 3600);
// cookie will expire in 1 hour, and will only be available
// within the php directory + all sub-directories of php
setcookie($name, $value, time() + 3600, "/php/");
?>
<html>
<body>
...some code...
</body>
</html>
Example
To modify a cookie, just set (again) the cookie using the setcookie() function:
<?php
$cookie_name = "user";
$cookie_value = "Alex Porter";
setcookie($cookie_name, $cookie_value, time() + (86400 * 30), "/");
?>
<html>
<body>
<?php
if(isset($_COOKIE[$cookie_name])) {
echo "Cookie '" . $cookie_name . "' is set!<br>";
echo "Value is: " . $_COOKIE[$cookie_name];
} else {
echo "Cookie named '" . $cookie_name . "' is not set!";
}
?>
</body>
</html>
Example
To delete a cookie, use the setcookie() function with an expiration time in the past:
<?php
// set the expiration date to one hour ago
setcookie("user", "", time() - 3600);
?>
<html>
<body>
<?php
echo "Cookie 'user' is deleted.";
?>
</body>
</html>
Example
Create a small script that checks whether cookies are enabled. First, try to create a test cookie with the setcookie() function, then count the $_COOKIE array variable:
<?php
setcookie("test_cookie", "test", time() + 3600, '/');
?>
<html>
<body>
<?php
if(count($_COOKIE) > 0) {
echo "Cookies are enabled.";
} else {
echo "Cookies are disabled.";
}
?>
</body>
</html>
❮ PHP Network Reference