Other Cookie Properties - Domain and Path

This section describes what is an array - an ordered pairs of keys and values. If sequential integer keys are used, an array is a simple indexed list. If string keys are used, an array is a map.

A cookie also has two other properties:

1. "domain" - A property that defines the domain of Web servers to which this cookie should be made available. Web browsers will send a cookie back to a Web server when the Web server matches its defined domain. Web browsers will never send back a cookie to a domain other than its defined domain.

For example, if a cookie's domain is www.google.com, the browser will send back this cookie to the server only when the browser is visiting www.google.com. The browser will never send back this cookie to www.yahoo.com.

To make a cookie available for all sub domains of a top level domain, set the domain property to the top level domain name. For example, if a cookie's domain is set to ".google.com", this cookie will be available to all google sub domains, like groups.google.com and gmail.google.com.

2."path" - A property that defines a Web server path to which this cookie should be made available. Web browsers will send a cookie back to a Web server when the Web server matches its defined domain, and the requested page matches its defined path. Web browsers will never send back a cookie to requested path other than its defined path.

Note that the defined path also includes all its sub paths. For example, if a cookie's domain is "www.amazon.com", and path is "/order/", then a browser will send back this cookie for requests like "http://www.amazon.com/order/checkout.html", and "http://www.amazon.com/order/report/invoice.html". But a browser should not send back this cookie for requests like "http://www.amazon.com/catelog/book.html".

The setcookie() function offers two more parameters to allow you to set "domain" and "path" properties on a cookie as in the following syntax:

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

where "path" specifies the cookie's path property, and "domain" specifies the cookie's domain property. If "path" is not given, the cookie will have "/" as the default path. If "domain" is not given, the cookie will have the current domain as the default domain.

Okay. Let's play the properties with the following script, CookieProperties.php:

<?php #CookieProperties.php
#- Copyright (c) 2015, HerongYang.com, All Rights Reserved.
#
   print("<pre>\n");
   print("\nAdding a cookie with default properties:\n");

   $cookieName = "User";
   $cookieValue = "Herong Yang";
   $expiration = time()+60*60*24*30;
   setcookie($cookieName, $cookieValue, $expiration);
   print("   Name: $cookieName\n");
   print("   Value: $cookieValue\n");
   print("   Expiration: $expiration\n");

   print("\nAdding a cookie with non-default properties:\n");
   $cookieName = "Book";
   $cookieValue = "Herong's Tutorial Notes on PHP";
   $expiration = time()+60*60*24*30;
   $path = "/";
   $domain = "localhost";
   setcookie($cookieName, $cookieValue, $expiration, $path, $domain);
   print("   Name: $cookieName\n");
   print("   Value: $cookieValue\n");
   print("   Expiration: $expiration\n");
   print("   Path: $path\n");
   print("   Domain: $domain\n");

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

   print "</pre>\n";
?>

Ran this script in IE, I got:

Adding a cookie with default properties:
   Name: User
   Value: Herong Yang
   Expiration: 1134622043

Adding a cookie with non-default properties:
   Name: Book
   Value: Herong's Tutorial Notes on PHP
   Expiration: 1134622043
   Path: /
   Domain: localhost

Clicked the refresh button on IE, I got:

Adding a cookie with default properties:
   Name: User
   Value: Herong Yang
   Expiration: 1134622059

Adding a cookie with non-default properties:
   Name: Book
   Value: Herong's Tutorial Notes on PHP
   Expiration: 1134622059
   Path: /
   Domain: localhost

Cookies received by the server:
   User = Herong Yang

Apparently, my script did not set the properties correctly. The browser should have sent back my second cookie also. So either the "path=/" or "domain=localhost" did not match my local IIS environment. I could not figure it out why.

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