In this lesson I will show you how to set a cookie in PHP. The process is relatively easy and can be used for some incredibly useful tracking of information. However, recent changes in EU legislation means that you must now make users aware of and consent to the use of cookies if you live in the EU and your site uses cookies.
WHAT ARE COOKIES?
Cookies have become quite an essential part of the web browsing experience. A cookie is a small piece of data that is place by your web browser onto your computer so that, for example, browsing preferences for a particular website can be tracked.
You may have noticed that when you visit a particular website and to search for a product that you are then presented with adverts relating to that product when you visit other sites. This is because a tracking cookie has been placed on your computer.
It is this sort of tracking behaviour that has caused many privacy concerns.
WHAT IS THE EU COOKIE LAW?
The EU Cookie Legislation was introduced to try to protect the privacy of users within the EU states and countries.
If you own a website with the EU then you basically have to ensure that the following key points are adhered to:
Visitors to your website must be notified that cookies are used
Visitors must be provided with a means of accepting or rejecting the cookie(s). If they reject then you must ensure that cookies are not placed onto their device.
Visitors must be informed of how the data collected will be used
HOW TO SET A COOKIE IN PHP
Setting a cookie in PHP is as simple as using the setcookie function as follows:
First we set the name of the cookie as “MyCookieName”. Obviously you can call your cookie whatever you wish but something useful and relative to what you are storing would make sense.
The next argument within the setcookie function is the data that is being stored. In this case it is simply “Data to be stored in the cookie”.
Saved the script and visit the page to set the cookie.
You can now check your browser and view the contents of the cookie.
If you are using Google Chrome then fo to the Settings and select the Advanced option >Site Settings > Cookies > See all cookies and site data
Alternatively type in: chrome://settings/siteData?search=cookie
If you are on a localhost environment just search for your localhost cookies. If you are on a web host with domain then search for the domain name.
You should see something similar to this:
HOW TO SET A COOKIE EXPIRY TIME IN PHP
You may find it useful to set an expiry time for your cookie. This is achieved by passing in another argument to the setcookie function as follows:
The expiry in the above example has been set to one hour from the time the cookie is set. Note that the expiry time add 3600 seconds to the current time.
HOW TO RETRIEVE A COOKIE IN PHP
Retrieving the value of the cookie can be achieved as follows:
HOW TO MODIFY A COOKIE IN PHP
To modify a cookie you need to set a different value for it as follows:
Basically, you are overwriting the original cookie with the new data value.
HOW TO DELETE A COOKIE IN PHP
You might find this strange but there is no way to delete a cookie in the traditional way that you might think i.e. there is no deletecookie function.
So, what we can do is to set the value of the cookie to NULL and also set the expiry time to some time in the past.
For example:
DO YOU WANT TO LEARN PHP?
One of the best PHP for Beginners courses is currently being offered here. Every line of code is explained and you have access to full video tutorials and instructor support. It is well worth a look and highly recommended.