Pass information Using COOKIE


What is Cookie?
A cookie is a small piece of information that is stored either on the client computer’s browser memory or in a file on the hard disk.
A cookie having name and value.
A cookie is useful for storing user specific information such as username, last visit of user etc…
You can pass variables between pages by the mean of cookies.
How to Create Cookie?
In PHP you can create a cookie using setcookie () function.
Syntax:
setcookie (‘Name’, Value, ExpireTime)
Here,

Name is the name of the cookie that you want to create.
Value is the value that is associated with the cookie created by you.
ExpireTime is the time when the cookie will expire and deleted. It is optional; if you do not set ExpireTime then it will be treated as a session cookie and removed when the browser is restarted.
Example:
$ExpireTime = 60 * 60 * 24 * 2 + time ();
Setcookie ('VisitTime', date ('d/m/y H: i: s a'), $ExpireTime);

It will create a cookie with name VisitTime and store the visit Date and Time of the server in it. It will set the ExpireTime of 2 Days. So it will be deleted after 2 Days from the time of its creation.

     

How to Retrieve Cookie?
You can retrieve cookies in your PHP script using following statement:
$_COOKIE [‘CookieName’];
You can also use the isset () function to determine weather the cookie with the specific name exist or not as shown below:
If (isset ($_COOKIE [‘CookieName’]))
{
echo $_COOKIE [‘CookieName’];
}
How to Delete Cookie?
You can delete the cookie using the setcookie () function. You will be surprised, if setcookie () function is used to create cookie then how it can be used to delete cookie? But it’s true you can use setcookie () function with empty value for cookie name to delete the cookie.
Example:
setcookie ('CookieName', “”);
Example:
Step 1: Create a file named Cookie1.php that accepts username and Password from the user.

<html>
<head>
<title>Use of Cookie</title>
</head>
<body>
<form name="Form1" method="POST" action="Cookie2.php">
Enter Your Name:
<input type="text" name="UserName">
<br>
Enter Your Password:
<input type="password" name="Password">
<br>
<input type="submit" name="submit" value="submit">
</form>
</body>
</html>

Step 2: Create a file named cookie2.php which authenticate the user and creates cookies that stores username and password and redirects user to cookie3.php file.

<?php
$Name = $_POST['UserName'];
$Pass = $_POST['Password'];
if ($Name=="Hemant" and $Pass=="Oza")
{
$ExpireTime = 60 * 2 + time();
setcookie('UserName',$Name,$ExpireTime);
setcookie('Password',$Pass,$ExpireTime);
header("location:Cookie3.php");
}
else
{
echo "Wrong username and password";
}
?>

Step 3: Now create a file named Cookie3.php which checks if the cookies exist for the username and password or not. If it exist then it will display username stored in the cookie otherwise it will redirects user to cookie1.php file.

<?php
if (isset($_COOKIE['UserName']) and isset($_COOKIE['Password']))
{
echo "Welcome ".$_COOKIE['UserName'];
}
else
{
header("location:Cookie1.php");
}
?>

Step 4: Now run the Cookie1.php file it will display following:

Pass Data Using Cookie

Step 5: Enter username and password and click on submit button. If you enter correct username and password then it will display welcome message as shown below:

Pass Data Using Cookie

Download Projects


Download Programs