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) 2015, HerongYang.com, All Rights Reserved.
#
   $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: 2015.

Table of Contents

 About This Book

 Introduction and Installation of PHP 7.0

 PHP Script File Syntax

 PHP Data Types and Data Literals

 Variables, References, and Constants

 Expressions, Operations and Type Conversions

 Conditional Statements - "if" and "switch"

 Loop Statements - "while", "for", and "do ... while"

 Function Declaration, Arguments, and Return Values

 Arrays - Ordered Maps

 Configuring and Sending out Emails

 Retrieving Information from HTTP Requests

 Creating and Managing Sessions in PHP Scripts

Sending and Receiving Cookies in PHP Scripts

 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

 Controlling HTTP Response Header Lines in PHP Scripts

 MySQL Server Connection and Access Functions

 Functions to Manage Directories, Files and Images

 SOAP Extension Function and Calling Web Services

 SOAP Server Functions and Examples

 Localization Overview of Web Applications

 Using Non-ASCII Characters in HTML Documents

 Using Non-ASCII Characters as PHP Script String Literals

 Receiving Non-ASCII Characters from Input Forms

 "mbstring" Extension and Non-ASCII Encoding Management

 Managing Non-ASCII Character Strings with MySQL Servers

 Introduction of Class and Object

 Integrating PHP with Apache Web Server

 Outdated Tutorials

 References

 PDF Printing Version