Create Retrieve Update and Remove a Cookie using PHP
Today we’ll show you how to create, retrieve, update and remove a cookie using PHP. A cookie is a small file that is stored in a user’s web browser and used for tracking users browsing activity. Maximum size of cookie is 4KB.
We can use the session in PHP to store the data at the server side whereas cookie will be used at client side.
Way to manage cookies in PHP
1. Create cookie
Use the setcookie()
function, to set a cookie using PHP.
Syntax:
1 | setcookie(name, value, [expire], [path], [domain], [secure]); |
Parameters:
- name – The name parameter is required and it’s the name of the cookie.
- value – The value parameter is also required and it is the value of the cookie.
- [expire] – The expire parameter is optional and used to define the expiry time for the cookie. After expiry time the cookie can’t be accessed. The default time for cookies is 30 minute.
- [path] – The path parameter is optional and used to set the cookie path on the server.
- [domain] – The domain parameter is optional and used to specify the domain name for which the cookie is available.
- [secure] – The secure parameter is also optional and specifies that the cookie is sent via https secure connection.
Let’s check an example of creating a cookie.
1 2 3 4 5 | <?php $cookieName = "userName"; $cookieValue = "Vihaan"; setcookie = ($cookieName, $cookieValue, time() + 3600, '/' ); // set cookie for 1 hour ?> |
Here, we used the /
as a path that specifies the cookie is available on all websites.
Take another example to set multiple cookies.
We have an array of user data and want to set all user data in cookies then write a code as below.
1 2 3 4 5 6 7 8 9 10 11 | <?php $userdata = array( 'user_id' => '1', 'user_name' => 'Vihaan', 'status' => '1' ); setcookie('userId', $userdata['user_id'], time() + 86400); // set cookie for 1 day setcookie('userName', $userdata['user_name'], time() +86400); setcookie('status', $userdata['status'], time() + 86400); ?> |
Run the following code If you want to set a cookie with an array as a value.
1 2 3 4 5 6 7 8 9 | <?php $userdata = array( 'user_id' => '1', 'user_name' => 'Vihaan', 'status' => '1' ); setcookie('userData', serialize($userdata), time() + 86400); ?> |
Note: If expiry time is set to 0, the cookie will expire when the browser closes.
2. Retrieve cookie
To retrieve the cookie, we use the $_COOKIE
variable. It is an associative array that holds the all cookies value and can be retrieved as individual cookie value. We can access the cookie value on another page, once cookies have been set.
Now we have set all user data in cookie and want to retrieve username then write the following code.
1 2 3 4 | <?php echo $_COOKIE["userName"]; ?> // Output: Vihaan |
3. Update cookie
To update cookie value, again we need to set a cookie value using setcookie()
function.
1 2 3 4 5 | <?php $cookieName = "userName"; $cookieValue = "Reyansh"; setcookie = ($cookieName, $cookieValue, time() + 3600, '/' ); ?> |
4. Remove cookie
To remove cookie value, we can use the setcookie()
function with expiry time in the past as shown below.
1 2 3 4 5 | <?php $cookieName = "userName"; $cookieValue = "Reyansh"; setcookie = ($cookieName, $cookieValue, time() - 3600); ?> |
That’s it for today to create, retrieve, update and remove a Cookie in PHP. Hope you like it.
Thank you for reading. Happy Coding..!!