Sending and Receiving Cookies - Example

This section provides a tutorial example on how to set and send a cookie with the setcookie() function, and how to receive a cookie with the $_COOKIE array.

To help us understand how to send and receive cookies, I wrote the following PHP tutorial example script page, CookieTest.php:

<?php
#  CookieTest.php
#- Copyright 2003 (c) HerongYang.com. All Rights Reserved.
#
   $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";
?>

When I ran run this script with a browser, I got:

Cookies added by the server:
   Cookie_1: My cookie value

Cookies received by the server:

When I click the refresh button of the browser, the page changed with:

Cookies added by the server:
   Cookie_2: My cookie value

Cookies received by the server:
   Cookie_1 = My cookie value

What happened here was that when I opened the page the first time, the server received no cookie from the browser's HTTP request. So my page added the first cookie named as "Cookie_1" to the HTTP response.

When I clicked the refresh button, the browser sent my first cookie back to the server in the HTTP request. Then my page added the second cookie named as "Cookie_2" in the response.

If I keep clicking the refresh button, more and more cookies would be added to the request and response. But there is a limit. The browser will only take up to 20 cookies from one Web server.

Table of Contents

 About This Book

 Introduction and Installation of PHP

 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

 Interface with Operating System

 Introduction of Class and Object

 Integrating PHP with Apache Web Server

 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

 Managing File Upload

 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

 Parsing and Managing HTML Documents

 Configuring and Sending Out Emails

 Image and Picture Processing

 Managing ZIP Archive Files

 Managing PHP Engine and Modules on macOS

 Managing PHP Engine and Modules on CentOS

 Archived Tutorials

 References

 Full Version in PDF/EPUB