PHP Tutorials - Herong's Tutorial Notes
Dr. Herong Yang, Version 2.21

Using Cookies

Part:   1  2  3  4 

PHP Tutorials - Herong's Tutorial Notes © Dr. Herong Yang

Non ASCII Characters with MySQL

Inputting Non ASCII Characters

Controlling Response Header Lines

HTTP Request Variables

Sessions

Using Cookies

PHP SOAP Extension

PHP SOAP Extension - Server

Directories, Files and Images

Using MySQL with PHP

... Table of Contents

This chapter describes:

  • What is a Cookie?
  • Sending and Receiving Cookies
  • Output Control Functions
  • Persistent Cookies
  • Other Cookie Properties

What is a Cookie?

Cookie: A small amount of information sent by a Web server to a Web browser, saved by the browser, and sent back to the server later. Cookies are transmitted inside the HTTP header.

Cookies move from server to browser, and back to server as follows:

Web         Web         Local       Web           Web
Server      Browser     System      Browser       Server
Send        Receive     Save        Send back     Receive
cookies --> cookies --> cookies --> cookies   --> cookies

As you can see from the diagram, cookies are actually saved to the hard disk of Web browser user's machines. Many users are concerned about this. But I think it is pretty safe to allow your browser to save cookies.

If you are really concerned, you can change your browser's settings to reject cookies. But this may cause many Web based applications fail to run on your browser.

Sending and Receiving Cookies

Cookies are supported in PHP in the following ways:

1. setcookie() - A built-in function that defines a cookie to be sent along with the rest of the HTTP headers. Like other headers, cookies must be sent before any output from your script (this is a protocol restriction). This requires that you place calls to this function prior to any output, including <html> and <head> tags as well as any whitespace. If output exists prior to calling this function, setcookie() will fail and return FALSE. setcookie() can be called using the following simple syntax:

bool setcookie(string name, string value)

where "name" is the name of the cookie, and "value" is the value of the cookie.

2. $_COOKIE[] - A pre-defined associate array that stores cookies submitted by the browser.

To demontrate how to send and receive cookies, I wrote the following PHP script page, CookieTest.php:

<?php #CookieTest.php
# Copyright (c) 2005 by Dr. Herong Yang, http://www.herongyang.com/
#
   $numCookies = count( array_keys($_COOKIE) );
   $numCookies++;
   $cookieName = "Cookie_$numCookies";
   $cookieValue = "My cookie value";
   setcookie($cookieName, $cookieValue);

   print("<pre>\n");
   print("Cookies added by the server:\n");
   print("   $cookieName: $cookieValue\n");

   print("\nCookies received by the server:\n");
   foreach ($_COOKIE as $k => $v) {
      print "   $k = $v\n";
   }
   
   print "</pre>\n";
?>

I opened this page with IE, I got:

Cookies added by the server:
   Cookie_1: My cookie value

Cookies received by the server:

(Continued on next part...)

Part:   1  2  3  4 

Dr. Herong Yang, updated in 2006
PHP Tutorials - Herong's Tutorial Notes - Using Cookies