Tutorials
PHPA cookie is flat file based system used to represent a user of the website. It is stored on the local computer of the user. When you visit a website a cookie may be set to represent the user. Next time he visits the website, he does not need to identify himself manually; instead the cookie will represent him on that website. With the help of PHP, cookies can be created, read and destroyed.
The main difference between a session and a cookie is that cookies are stored on the user’s computer where as sessions are stored on the server for particular usage. Cookies can be manually deleted by the users also to make sure security is not breached.
We can literally put 4000 characters of data in a flat cookie file and store information about the user preferences for a particular website. Some of the practical uses of Cookies are as follows:
Practically there is no security threat while using cookies. A cookie set by a particular website cannot be accessed or even check if it exists by another website even if it wants to. But since cookie is just a flat text file, it can be opened and read on the computer it is stored in.
If a website has stored a password in a cookie it can be read and this can pose threat to hacking. But if the same password is encrypted using a hash like md5() or sha1() then it can be more secure since this content is used to match it with the password stored on the website.
In PHP, we have a function setcookie() which is used to SET as well as UNSET the cookie.
Syntax:
setcookie("name", "value", expire, "path", "domain");
Name: it’s the name of the cookie
Value: the value that is to be stored in the cookie. Ex: username, password, email id
Expire: it’s the expiring time of the cookie since it was set.
Path: the path of the website where the cookie is valid. Like a subdomain
Domain: The website this cookie is valid for.
Example
setcookie("username", "Harsha M V", time()+3600);
In the above example, the Cookie name is username. value is Harsha M V, it expires in 1 hour. It is mentioned in seconds 60 seconds multiplied by 60 minutes. The value of the cookie is automatically URLencoded when sending the cookie, and automatically decoded when received (to prevent URLencoding, use setrawcookie() instead).
PHP comes with a super global $_COOKIE. All cookies set by a website on their clients website is retrieved via this super global.
The above script checks if the cookie with the name username is set. If it is set it prints Welcome “ Harsha M V”. If the cookie is not set then is just prints out Welcome Guest!
There is no special function to delete a cookie. It can be done by reversing the timing of expiry in the cookie by resetting it again as shown below.
setcookie("username", "", time()-3600);
From the above statement, if you observe the name of the cookie is the same as it was set. The value of the cookie is set to NULL and the expiry time is subtracted from the current time to 1 hour earlier making it to expire at the moment the above function is run.
Practical Example of using a Cookie on a Website:
Working:
First, when the page is loaded, it checks if the cookie by name Email is set. If not then it displays a form for the user to enter his email ID. Once the USER submits the form it checks if the Email field was filled. If it was filled then a cookie with the name Email is SET with its value set to the user input email ID setting a expiry date along.
Another cookie with the name lastsave is also set with the value set to the current time it was set at. If the email is not entered then it shows a message. Now we can retrieve the cookie using the $_COOKIE super global.
COOKIE mixed with SESSIONS can do wonders for your website. In the next tutorial, let us learn about SESSIONS and how to work with SESSION variables.