Hello, folks! welcome back to a new section of our tutorial on PHP. In this tutorial guide, we will be studying about Cookies in PHP.
Cookie is a small piece of information that are stored on the client computer and they are kept for tracking purpose. PHP clearly supports HTTP cookies.
There are three steps involved in identifying returning users -
Cookie is a small piece of information that are stored on the client computer and they are kept for tracking purpose. PHP clearly supports HTTP cookies.
There are three steps involved in identifying returning users -
- Server script sends a set of cookies to the browser. For example name, age or sex.
- The browser stores this data on local machine for future use.
- When next the web browser sends a request to the server, it sends those cookies information to the server and server makes use of that information to identify the user.
In this tutorial, we will be giving a detailed explanation on how to set cookies, access the cookies, and how to delete them.
READ: PHP Functions
The Anatomy of a Cookie
Cookies are mostly set in an HTTP header. A PHP script that sets a cookie might send some headers that look like this -
HTTP/1.1 200 OK Date: Sun, 27 Sep 2020 11:03:38 GMT Server: Apache/1.3.9 (UNIX) PHP/4.0b3 Set-Cookie: name=xyz; expires=Sunday, 27-Sep-23 22:03:38 GMT; path=/; domain=webdesigntutorialz.com Connection: close Content-Type: text/html
From the above headers, the Set-Cookie header contains a name value pair, a GMT date, a path and a domain. The name and the value will be URL encoded. The expires field is an instruction to the web browser to "forget" the cookie after the given time and date.
If a browser is configured to store cookies, it will store this information until the expiry date. If the user points the browser at any page which matches the path and domain of the cookie, it will resend the cookie to the web server. The browser headers might look like this -
If a browser is configured to store cookies, it will store this information until the expiry date. If the user points the browser at any page which matches the path and domain of the cookie, it will resend the cookie to the web server. The browser headers might 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 the cookies in the environment variables which contains all the cookie names and values.
READ: PHP File Handling (I/O)
Setting Cookies with PHP
PHP makes available a setcookie() function to set a cookie. This function requires up to six arguments and should be called before <html> tag. For each cookie that's to be set, this function has to be called separately.
setcookie(name, value, expire, path, domain, security);
Parameter Details
Following below is the details of all the arguments -
- Name - Sets the name of the cookie and is stored in the environmental variable called HTTP_COOKIES_VAR. This variable is used while accessing cookies.
- Value - Sets the value of the name variable and is the content that you actually want to store.
- Expiry - Specify a future time in secs since 00:00:00 GMT on 1st Jan 1970. After this time the cookie will become inaccessible. If this parameter is not set then the cookie will expire as soon as the web browser is closed.
- Path - Specify the directory for which the cookie is valid. A single forward slash character permits the cookie to be valid for all directorie.
- Domain - Specifies the domain name in very large domains and must hold at least two periods to be valid. All cookies are only valid for the host and domain which created them.
- Security - This can be set to the value of 1 to specify that the cookie should only be sent via secure transmission using HTTPS else set to the value of 0, which means that the cookie can be sent via regular HTTP.
Example
The following example is going to create two cookies name and age. These cookies will expire after two hours.
<?php setcookie("name", "Precious Amah", time()+7200, "/","", 0); setcookie("age", "25", time()+7200, "/", "", 0); ?> <html> <head> <title>Setting Cookies with PHP</title> </head> <body> <?php echo "Set Cookies"?> </body> </html>
Accessing Cookies with PHP
PHP provides various ways for accessing cookies. The simplest method is by using either $_COOKIE or $HTTP_COOKIE_VARS variables to access the cookies.
Example
The following example will access all the cookies set in the above examples -
<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 the Isset() function to check if a 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... Not recognized" . "<br />"; ?> </body> </html>
READ: Loops in PHP
Deleting Cookies with PHP
To delete a cookie, call setcookie() function with the name argument only but this does not always work well, however, and should not be relied on.
It's safest to set the cookie with a date that has already expired -
It's safest to set the cookie with a date that has already expired -
<?php setcookie( "name", "", time()- 70, "/","", 0); setcookie( "age", "", time()- 70, "/","", 0); ?> <html> <head> <title>Deleting Cookies with PHP</title> </head> <body> <?php echo "Deleted Cookies" ?> </body> </html>
READ: PHP Comparison Operators
Alright guys! This is where we are rounding up for this tutorial post. In our next tutorial post, we are going to be discussing about PHP Sessions.
Feel free to ask your questions where necessary and we will attend to them as soon as possible. If this tutorial was helpful to you, you can use the share button to share this tutorial.
Follow us on our various social media platforms to stay updated with our latest tutorials. You can also subscribe to our newsletter in order to get our tutorials delivered directly to your emails.
Thanks for reading and bye for now.
Feel free to ask your questions where necessary and we will attend to them as soon as possible. If this tutorial was helpful to you, you can use the share button to share this tutorial.
Follow us on our various social media platforms to stay updated with our latest tutorials. You can also subscribe to our newsletter in order to get our tutorials delivered directly to your emails.
Thanks for reading and bye for now.