Sunday, December 22, 2013

How to Set and Use Cookies in PHP

Hiiiiii, as the title of this post suggests, this post is all about the concept of COOKIES as provided by PHP. We will here first see what does cookies means in php and then we'll see some examples of how to use these cookies in you code.

PHP Cookies

What are Cookies :

Cookies are mainly the files stored at you computer when you run any website in your browser. Usually these cookies files contains information about the user' login details when he/she login with "Remember Me" option, user id and name of a person logged in, user's current status or progress on any particular site so that whenever he login again he will be resumed from that particular progress. Cookies are stored in users browser for a limited span of time which is defined by the developer of a site which is setting a cookies in your computer system.

 

Using Cookies in PHP :

Syntax-

Cookies are having a very simple for their usage as shown below :

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

Here, setcookie is the php function to set cookies,
name is the name of the cookie variable (mandatory),
value is the value of that cookie variable whose name you have mentioned in "name" (mandatory),
expire is the time span for which you want the cookie to be saved in users's browser (mandatory),
path is the directory path if you want to set cookie for the particular directory on your site (optional), and
domain is the domain name of site on which you want to set cookie for some particular path directory (optional).

There are two more parameters in this setcookie function which are also optional namely secure and httponly, with this two variables,the syntax becomes-

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

Both of these variables (secure and httponly) are boolean and accept only TRUE or FALSE in terms of values. If value of secure is set to TRUE then the cookie will only be sent to client system from the site server if the connection from the client to website is secure HTTPS.
If the value of httponly is set to TRUE then cookies will only be accessible by HTTP protocol and none of the scripting languages like javascript will be able to access the cookies.

Example To set Cookie:

Example 1:

setcookie("username","Deepaanshu",time() + (86400 * 7)); // 86400 seconds denotes 1 day
 

The above used method will create a cookie with cookies variable namely "username" having a value - "Deepaanshu" and "time() + (86400 * 7)" indicates that the cookie will automatically delete on the 7th day from now.

Example 2:

$username="Deepaanshu";
setcookie("username",$username,time() + (86400* 15),'/~design/','example.com',true,true);
 

This example will create a cookie with cookies variable namely "username" having a value - "Deepaanshu" for next 15 days from now and "/~design/","example.com" indicates that this cookie will applied on "design" directory on "example.com" domain. As this cookie is having a two true values at the last so, this cookie will only be created on user pc if connection from his side is HTTPS and this cookie will not accessible by javascript.

Example To Get Cookie variables: 

if(isset($_COOKIE['username']))
{
 $login_user_name=$_COOKIE['username'];
}
else
{
 $login_user_name="Guest";
}
echo 'Welcome Back '.$login_user_name;  // This will echo Welcome Back Deepaanshu if $_COOKIE['username'] is set with a value "Deepaanshu" 

Deleting Cookies :

To delete a cookie you don't have any unset function like sessions but rather you can make the cookie expire by giving it a time earlier then the current time and making its value blank.

setcookie("username", "", time() - 3600);  //this will make the cookie to expire as time given to it is a hour ago from current time. 


Hope you like this post……..Please Comment…………!!!!!!!!!

 


Tags:

0 Responses to “How to Set and Use Cookies in PHP”

Post a Comment

© 2013 MyCodeStock. All rights reserved.
Designed by SpicyTricks