Web Info and IT Lessons Blog...

Thursday 5 February 2015

Cookies in PHP

The previous lesson of PHP Lessons Series was about Sessions and in this lesson we will learn about Cookies in PHP.

PHP Cookies

A Cookie is a file that is saved on the user's computer by the server after the user's interaction with the server. The purpose of saving of a cookie is to easily identify the user during future interactions with the server. Once a cookie file is saved on the user's computer then each time the user requests the webpage through browser, the cookie file will also be sent with the web page request.

Why Use Cookies?

We need cookies to identify a user, know his interests, his recent activity and show him things that interest him for better interaction. Once we read the cookies, we are not completely blank about the user anymore and we can control what we need to show to the user.

How to create a cookie?

In PHP we have a function setcookie() to create cookies. The basic structure of setcookie() function is given below:



setcookie(name, value, expire, path, domain, secure, httponly);


The above setcookie() function can have certain parameters i.e

name:
Name attribute can be a set of characters to assign name to a cookie and it's the only required attribute of a cookie while all other attributes are optional.

value:
Just like name, a value can be a set of character to assign some value to the cookie.

expire:
Expire attribute specifies the expiry date of the cookie and is assigned in seconds. If we want a cookie to expire in 1day, we will have to pass time() + 86400 in the setcookie() function as the expiry attribute.

path:
Path attribute specifies the location (directory, folder or file) of the requesting website for which the cookie is allowed. For example if we pass '/' in the path, the cookie will be allowed for the entire website.

domain:
Domain attribute specifies the website for which the cookie access is allowed.

secure:
If secure attribute of a cookie is set, the cookie will only be sent to HTTPS servers.

httponly:
httponly specifies that the cookie is only allowed for websites which are not hosted on secure servers.

To set a simple cookie that will expire in a day, check the code given below:


setcookie("Name", "John", time() + 86400 * 30, "/");

How to check if a cookie is set?

We can check if a cookie is set or not using isset() function i.e


if(isset($_COOKIE['Name'])){
 echo 'Cookie is set';
}

In the above code $_COOKIE is a global variable used to retrieve cookies.

How to get the value of a cookie?

The value of a cookie is saved against the name. To echo the value of the cookie created above, check the code given below:


if(isset($_COOKIE['Name'])){
 echo $_COOKIE['Name'];
}

The above code will echo the value of the cookie (i.e John). To modify the value of the above cookie assign a different value to $_COOKIE['Name'].

How to delete a cookie?

To delete a cookie we need to expire it. All we have to do is assign it expiry date that has already passed i.e


setcookie("Name", "John", time() - 1000, "/");

In the above code, the expiry time that is assigned to the cookie is less than current time which means that the cookie has already expired.

No comments:

Post a Comment