Cookie in PHP is a very important concept which deals with smooth handling and implementation of a website based on server-based scripting.
What is a Cookie ?
It is basically a small file with a maximum size of 4 kilobytes that the web server stores on the users/client device.
Once the cookie has been created and set, all the requests that are made after the first one return the cookie name and value to the calling page.
The most important thing that one must note here is that the cookie can only be read from the domain it has been created and issued from. Other users cannot see its value.
Most of the websites on the internet display elements from other domains such as advertising. Such domains can also create their own cookies known as Third Party Cookies.
NOTE: Many web browsers like Google Chome, Internet Explorer, and Mozilla Firefox have a feature of disabling the storage and creation of cookies.
Cookie in PHP
Uses and Features :
- Personalization of the user experience can be achieved with the help of cookies when user selects their preferences.
- Tracking the web pages frequently accessed by the user.
- Since https is a stateless protocol, cookies allow us to keep track of the state of the application using small files stored in the user’s system.
Creating Cookies in PHP
One can create a cookie by using the following syntax:
setcookie (cookie_name, cookie_value, expiry_time, cookie_path, cookie_domain, secure, httponly)
Now look at the following basic code to create a cookie:
<?php setcookie("mycookie","123",time()+3600); ?>
There are 7 parameters to the above mentioned function:
- Cookie_Name – Specifies the name of the cookie. This parameter is mandatory.
- cookie_value – The second parameter specifies the value associated with the cookie. This parameter is also mandatory.
- expiry_time – The third parameter specifies the lifetime of the cookie. The time is set using time() +/- the number of seconds.
- cookie_path – The fourth parameter is optional. It is used to specify the cookie path on the server.
- cookie_domain – The fifth parameter is also optional which is used to define the cookie access hierarchy.
- secure – This parameter is optional and its default value is false. Used to determine whether the cookie is sent via https or HTTP.
- httponly – The last parameter is also optional. If it is set to true the only client-side scripting languages cannot access them.
Here is the following code to implement a cookie for 300 seconds:
<?php setcookie(“username”, “123”, time()+300); //expires after 300 seconds echo “the cookie has been set for 300 seconds”; ?>
Deleting Cookies in PHP
If one wants to delete the cookie before its specified expiry time, then you can set the cookie again by giving the time in past. That means we have to give a syntax like this: time()-(number of seconds);
Example code:
<?php setcookie(“username”,”123”,time()-300); ?>
Modifying Cookies in PHP
In order to modify an existing cookie, just set the value again using the setcookie() function. This just the concept of overwriting the existing cookie. The following code is one example for such implementation:
<?php setcookie(“username” ,”345” ,time()+3600); ?>
Here the cookie name”username” was already created and used. Now we are again using it and updating its value.