- What are cookies?
- How to set cookies using PHP?
- How to get cookie value in PHP?
- How to set the expiry time for a cookie?
- Alternative syntax of the setcookie function.
- Check whether the cookie has been set?
How to set cookies using PHP?
The PHP setcookie() function is used to set or create cookies. Let’s see how to use this function –
The setcookie()
function has 7 parameters, but only the name parameter is required, and the others are optional.
setcookie(name, value, expires, path, domain, secure, httponly)
Parameter | Description |
---|---|
name (Required) | With the cookie name, you can access the cookie value. |
value | value of the cookie. The default value is empty. |
expires | The time the cookie expires. If you do not define the cookie will not be removed until the browser cache is cleared. |
path | It defines which pages can access this cookie. |
domain | Domain-level cookie accessibility. Suppose you have a domain name that is example.com and to make the cookie available on all subdomains of example.com, set the domain to “example.com”. “www.example.com” will make the cookie available only to the www subdomain. |
secure | It takes a boolean value (true or false). If it is true the cookie should only be transmitted over a secure HTTPS connection from the client. |
httponly | Cookies can also be accessed on the client side using JavaScript, but if you pass true to this parameter, the cookie will not be accessible by JavaScript. |
Example of setcookie
<?php
$cookie_name = "username";
$cookie_value = "Rahul Gandhi";
setcookie($cookie_name, $cookie_value);

How to get cookie value in PHP?
Using the $_COOKIE global variable we can get the cookie value.
This $_COOKIE
variable gives you an associative array of all your cookies, and you can access a specific cookie value using the cookie name. Let’s see –
<?php
print_r($_COOKIE);
Array
(
[username] => Rahul
)
Get value of specific cookie
<?php
echo $_COOKIE['username'];
Rahul
How to set the expiry time for a cookie?
In the third parameter, you can set the expiry for a cookie, and the time must be in Unix timestamp.
For the Unix timestamp, we will use the time() function. this function returns the current Unix timestamp.
Some examples of the time() function
- time()+60 = after 1 minute.
- time()+60*2 = after 2 minute.
- time()+60*60 = after 1 hour (60*60 = 3600 seconds).
- time()+60*60*24 = after 24 hours or one day.
- time()+60*60*24*9 = after 9 days.
<?php
$cookie_name = "username";
$cookie_value = "Rahul";
$expire = time() + 60; // after 60 seconds
setcookie($cookie_name, $cookie_value, $expire);
Alternative syntax introduced in PHP 7.3.0
setcookie(name, value, array options[])
An example of the alternative syntax
<?php
$cookie_name = "username";
$cookie_value = "Rahul";
$expire = time() + 60;
setcookie($cookie_name, $cookie_value, [
"expires" => $expire,
"httponly" => true
]);
How to check whether the cookie has been set?
The setcookie() functoin will return true if the cookie has been set.
<?php
$cookie_name = "username";
$cookie_value = "Rahul";
$expire = time() + 60;
$myCookie = setcookie($cookie_name, $cookie_value);
if ($myCookie) {
echo "The cookie has been sent successfully.";
} else {
echo "Oops!, Something is going wrong.";
}
The cookie has been sent successfully.