Cookie in PHP. Cookies are text files stored on the client and they are kept for tracking purposes. PHP supports HTTP Cookies.
There are 3 steps to identify the user who returns:
Server-side script sends a set of cookies to the browser. For example name, age, ...
The browser stores this information on the local machine for future use.
The next time the browser sends any request to the Web Server, it will send that cookie information to the server and the server will use that information to identify this user.
This chapter will show you how to set up Cookies, how to access them, and how to delete them.
Cookie in PHP
Structure of a Cookie
HTTP/1.1 200 OK Date: Fri, 04 Feb 2000 21:03:38 GMT Server: Apache/1.3.9 (UNIX) PHP/4.0b3 Set-Cookie: name=xyz; expires=Friday, 04-Feb-07 22:03:38 GMT; path=/; domain=tutorialspoint.com Connection: close Content-Type: text/html
As you can see, the Cookie header contains name/value pairs, GMT date, path, and a domain name. This name and value will be the URL encoded. The expires field is an instruction that tells the browser to "forget" this cookie after a given time.
If the browser is configured to store Cookies, it will keep this information until the expiration date. If the user points to any page with the same path and domain as the cookie, it will send this cookie to the server. The browser header can look like this:
GET / HTTP/1.0 Connection: Keep-Alive User-Agent: Mozilla/4.6 (X11; I; Linux 2.2.6-15apmac ppc) Host: zink.demon.co.uk:1126 Accept: image/gif, */* Accept-Encoding: gzip Accept-Language: en Accept-Charset: iso-8859-1,*,utf-8 Cookie: name=xyz
A PHP script will then have access to this Cookie with the environment variable $_COOKIE or $HTTP_COOKIE_VARS, which holds all of the cookie names and values. The above cookie can be accessed using $HTTP_COOKIE_VARS["name"].
Cookie setting using PHP
PHP provides setCookie() function to set a Cookie. This function requires up to 6 parameters and it should be called before the <html> tag. For each Cookie, this function must be called separately.
setcookie(name, value, expire, path, domain, security);
Details of each parameter:
- Name - Set the name of the Cookie and it is stored in an environment variable called HTTP_COOKIE_VARS. This variable is used when accessing Cookies.
- Value - Set the value of the variable name and it is the content you want to store.
- Expiry - Indicates the expiry date of Cookies. Time in seconds from 1/1/1970. After this time, the Cookie will not be accessible. If this parameter is not set, the Cookie will automatically expire when the browser is closed.
- Path - Define the folders for which the Cookie is valid. A single slash character (/) allows Cookie to be valid for all directories.
- Domain - Define the domain name. All Cookies are valid only for the given domain name.
- Security - It can be set to 1 to indicate that this Cookie is only sent via a secure transmission using HTTPS, otherwise if set to 0, it means that Cookie can be sent using regular HTTP.
The following example will create 2 Cookies, name, and age, which will expire after 1 hour.
<?php setcookie("name", "MR Blog", time () + 3600, "/", "", 0); setcookie("age", "25", time () + 3600, "/", "", 0); ?> <html> <head> <title> Setting Cookies in PHP </title> </head> <body> <?php echo "Cookie setting example"; ?> </body> </html>
Accessing Cookies in PHP
PHP provides many ways to access Cookies. The easiest way is to use the variable $_COOKIE or $HTTP_COOKIE_VARS. The following example will access all the Cookies set in the above example.
<html> <head> <title> Accessing Cookies with PHP </title> </head> <body> <?php echo $_COOKIE["name"]. "<br />"; / * is equivalent to * / echo $HTTP_COOKIE_VARS["name"]. "<br />"; echo $_COOKIE["age"]. "<br />"; / * is equivalent to * / echo $HTTP_COOKIE_VARS["age"]. "<br />"; ?> </body> </html>
You can use isset() function to check whether Cookie is set or not.
<html> <head> <title> Accessing Cookies with PHP </title> </head> <body> <?php if (isset($_COOKIE["name"])) echo "Welcome". $_COOKIE["name"]. "<br />"; else echo "Sorry ... Looks like I don't know you !!!" . "<br />"; ?> </body> </html>
Delete Cookies with PHP
To remove a Cookie you should call setCookie() with only the name parameter, but it doesn't always work well.
The safest way to set cookies with a date that indicates that have expired.
<?php setcookie("name", "", time () - 60, "/", "", 0); setcookie("age", "", time () - 60, "/", "", 0); ?> <html> <head> <title> Delete Cookies in PHP </title> </head> <body> <?php echo "PHP cookie deletion example"; ?> </body> </html>
- Set cookie PHP
- Session PHP
- Cookie in PHP
- Show _COOKIE PHP
- Cookie PHP
- Delete cookie PHP
- $_cookie php