SIGN UP MEMBER LOGIN:    
ARTICLE

Working with cookies in PHP

Posted by Mike Steward Articles | PHP December 12, 2011
Today I will discuss about cookies in PHP which, in short "cookies are small strings of data created by a Web server but stored on the client".
Reader Level:

Cookies in PHP

Hi guys, In this article we are going learn how to deal with cookies; first of all we need to know "what is the meaning of cookies" ?

A cookie is object used to identify the user by his/her computer. Cookies are small strings of data created by a Web server but stored on the client. In addition to having names and values, cookies have an expiration time. Cookies are often used to track user information.

'Or' 

A cookie is often used to identify a user. A cookie is a small file that the server embeds on the user's computer. Each time the same computer requests a page with a browser, it will send the cookie too.

For this purpose you must install a XAMPP server.

Creating Your First PHP Cookie

For creating the very first cookies with PHP, we used the setcookie() function. This is a built in function in php.

Syntax

setcookies(name,value,expiration)

Now we will understand the meaning of these arguments, the explanation of name, value and expiration are given below:

  • name: The name of your cookie. You will use this name to later retrieve your cookie, so don't forget it.
  • value: The value that is stored in your cookie. Common values are username(string) and last visit(date).
  • expiration: The date when the cookie will expire and be deleted. If you do not set this expiration date, then it will be treated as a session cookie and be removed when the browser is restarted.

This is the PHP script to set the cookies for the particular user :

<html>
<
head><title>PHP cookies info</title>
</
head>
<
body bgcolor="cyan">
<center>
<
h3> Setting up cookies</h3><hr>
<
p> setcookie('lastVisit', date("G:i - m/d/y"), $oneMonth) </p><br>
<
p>with in php scripting<hr>
//Calculate 30 days in the future<br>
//seconds * minutes * hours * days + current time <br>
$oneMonth = 60 * 60 * 24 * 30 + time(); <br>
setcookie('lastVisit', date("G:i - m/d/y"), $oneMonth);<hr>
</p>
<p> Cookies enabled............</p>
<?php
$oneMonth = 60 * 60 * 24 * 30 + time();
setcookie(
'lastVisit', date("G:i - m/d/y"), $oneMonth);
?>
</center>
</
body>
</
html>

Save it as abc.php.

Output of above php scripting

Type http://localhost/yourfoldername/function.php at your browser, here your output look like:


c3.gif


PHP script to retrieve the information

<html>
<
head>
<
title>PHP cookies info</title>
</
head>
<
body bgcolor="orange">
<center>
<
h3> COOKIES INFORMATION </h3><hr>
<?php
if(isset($_COOKIE['lastVisit']))
      $visit = $_COOKIE[
'lastVisit'];
else
      echo
" you got stale cookies!";
echo "Welcome back! <br> You last visited on ". $visit;
?>
</center>
</body>
</html>

Save it as ref.php.

Output

Type http://localhost/yourfoldername/function.php at your browser, here your output look like :


c2.gif


Destroying the cookies

To destroy the cookie, simply use setcookie again, only set the expiration date to be in the past. Here is an example:

<?php
$past = time() - 60;
//this makes the time 60 seconds ago
setcookie(lastVisit, date("G:i - m/d/y"), $past);
?>

Remember: Cookies need to be set in the header. This means they must be sent before any HTML is set to the page, or they will not work. Thanks !!

Login to add your contents and source code to this article
share this article :
post comment
 
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
Nevron Gauge for SharePoint
Become a Sponsor