PHP cookies
In web programming, the term 'cookie' means something totally different from what it is popularly known as. In web programming, a 'cookie' is a small text file stored on a users hard drive by a website for various purposes such as remembering a user who frequents that website.
This lesson focuses on:
- Setting a cookie
- Reading data from a cookie
Setting a cookie
The function used to set a cookie is setcookie(). A cookie must be sent along with the HTTP headers before any other code. This means that when using the setcookie() function it must be declared first thing on the page - even before the <HTML> tag used in the HTML code. If setcookie() is not the first thing on the page, then the function will fail, a cookie will not be set, and the function will return false! When a cookie is successfully sent, the function returns true.
Syntax of the setcookie() function:
setcookie(name, value, expirationDate, path, domain,
isSecure, httpAccess);
The setcookie() function can take seven parameters, but only one is required - the name parameter (to set the name of the cookie.
setcookie() function parameters:
-
name
This is the only required parameter for the setcookie() function. It sets the name of the cookie.
-
value
This parameter sets the value of the cookie.
-
expirationDate
This parameter sets the expiration date of the cookie. If this parameter is not used, the cookie will expire when the browser is closed by default.
-
path
This parameter denotes to which directories on the server the cookie will be available. For example, if this parameter is set to '/dir5' then it will be available only to that directory and all its subdirectories. If this parameter is set to '/' then it will be available to all directories on the server.
-
domain
This parameter denotes the domain in which the cookie will be available.
-
isSecure
This parameter takes a value of either true or false and denotes whether the cookie should be transmitted over a secure HTTPS connection or not.
-
httpAccess
This parameter takes a value of either true or false and denotes whether the cookie is accessible only through the HTTP protocol or not. If set to true, the cookie will not be accessible to other scripting languages such as Javascript.
Example:
<?php
setcookie("cookie1", "this is a cookie");
?>
The above example will set a cookie named cookie1 that stores the value 'this is a cookie'. This cookie will expire automatically when the web browser is closed since no expiration date is set for it.
Reading data from a cookie
To read data from a cookie, you first have to check if the cookie actually exists. This is achieved through the isset() function. The isset() function is used to check for the existence of a variable, in this case, a cookie variable through the use of the $_COOKIE associative array which stores an array of existing cookies.
Syntax:
isset($_COOKIE['nameOfCookie']);
If the cookie specified in the isset() function exists, then the function will return true, otherwise it will return false.
Example:
<?php
if (isset($_COOKIE['cookie1'])) {
$cookie1 = $_COOKIE['cookie1'];
}
?>
In the above example, an if statement checks for the existence of a cookie named cookie1. If a cookie named cookie1 exists, then its value will be passed to the variable $cookie1. If it does not, then it will remain empty. The isset() function checks for this.




