PHP Tutorials - Herong's Tutorial Examples
Dr. Herong Yang, Version 3.00

Persistent Cookies Saved on Hard Disk

This section describes what is a persistent cookie - A cookie with an expiration time in the future and saved in a cookie file on the hard disk of the browser computer.

There are two kinds of cookies: persistent cookies and temporary cookies.

A persistent cookie is stored in a file on your computer's hard disk. It remains there after you close your Web browser. A persistent cookie will be picked up by the browser and included in HTTP requests for the Web server, where the cookie came from.

A temporary or session cookie is stored in memory only for your current browsing session. A temporary cookie will be deleted when you close your browser.

The default behavior of setcookie(name,value) is to set a cookie as a temporary cookie. To set a persistent cookie, we need to add another parameter to the setcookie() function call using the following syntax:

bool setcookie(string name, string value, int expire)

where "expire" specifies when this cookie should be expired. If the expiration time is a future time, like 30 days from today, this cookie will be set as a persistent cookie. Note that "expire" should be represented in number of seconds since the epoch. The best way to set "expire" is use the time() function, which represents the current time in number of seconds since the epoch. Example, 30 days from today can be expressed as "time()+60*60*24*30".

If "expire" is not given, a temporary cookie will be created.

To show you how to set a persistent cookie, and how the cookie is store in a file, I wrote the following PHP script page, CookiePersisted.php:

<?php #CookiePersisted.php
# Copyright (c) 2005 by Dr. Herong Yang, http://www.herongyang.com/
#
   $cookieName = "User";
   $cookieValue = "Herong Yang";
   $expiration = time()+60*60*24*30;
   setcookie($cookieName, $cookieValue, $expiration);

   print("<pre>\n");
   print("Cookies added by the server:\n");
   print("   $cookieName: $cookieValue\n");
   print("   Expires at: $expiration\n");
   print "</pre>\n";
?>

I opened this page with IE (Internet Explorer), I got:

Cookies added by the server:
   User: Herong Yang
   Expires at: 1134531525

To find out in which file this cookie is stored in my computer, I clicked at IE's "Tools" menu, selected "Internet Options...". and clicked the "Settings..." button in the "Temporary Internet files" section of the "General" tab. I saw where is my "Temporary Internet files folder". So I went to that folder, and saw a cookie file named something like "Cookie:user@localhost/". I double clicked on that file, and managed to open it in notepad:

User
Herong+Yang
localhost/
1024
3801469056
29753439
3934260416
29747404
*

Actually, I saw a lots of other cookie files created by other Web sites that I have visited in the past. I deleted all of them.

Last update: 2005.

Sections in This Chapter

What Is a Cookie?

Sending and Receiving Cookies

Sending and Receiving Cookies - Example

ob_start() - Output Buffering Function

Persistent Cookies Saved on Hard Disk

Other Cookie Properties - Domain and Path

Dr. Herong Yang, updated in 2009
Persistent Cookies Saved on Hard Disk