PHP Tutorials - Herong's Tutorial Examples - v5.17, by Herong Yang
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
Introduction and Installation of PHP
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
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
►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
Parsing and Managing HTML Documents
Configuring and Sending Out Emails
Managing PHP Engine and Modules on macOS