The use of session and cookies has great significance in a PHP website. In this article, we will cover sessions and cookies variable concepts and their practical examples. You will learn how to create, update, and delete a cookie.
Definition of Cookie
A cookie is a variable which is stored in a user’s web browser. It is dedicated for the purpose of authentication, saving preferences and identification of server session.
How Do Cookies Work
Every time a browser is connected to the server, the cookie variable’s value is sent to the server. Hence, only the relevant cookies are sent to the domain. Cookies are a suitable method of linking a page for the user’s interaction with a website. The cookies sent by the client will be included in $_COOKIE global variable.
How To Set Cookies
Cookies can be set using setcookie() and setrawcookie() functions. Consider the example below in which a cookie variable is set for a month. This sign ‘/’ indicates that the cookie is available for the entire website. We have used $_COOKIE variable to retrieve the value of cookie. It should be noted that set_cookie function must be initiated before the html tag.
How To Modify A Cookie
We can even modify a cookie value. Consider the following example:
- <?php
- $cookie_name = "employee";
- $cookie_value = "John Steveson";
- setcookie($cookie_name, $cookie_value, time() + (86400 * 30), "/");
- ?>
- <html>
-
- <body>
- <?php
- if(!isset($_COOKIE[$cookie_name])) {
- echo "Cookie named '" . $cookie_name . "' is not set!";
- }
- else {
- echo "Cookie '" . $cookie_name . "' is set!<br>";
- echo "Value is: " . $_COOKIE[$cookie_name];
- }
- ?>
- </body>
-
- </html>
Output:
Cookie 'employee' is set!
Value is: John Steveson
Examples:
- <!DOCTYPE html>
- <?php
- $cookie_name = "employee";
- $cookie_value = "David Klarc";
- setcookie($cookie_name, $cookie_value, time() + (86400 * 30), "/");
- ?>
- <html>
-
- <body>
- <?php
- if(!isset($_COOKIE[$cookie_name])) {
- echo "Cookie named '" . $cookie_name . "' is not set!";
- }
- else {
- echo "Cookie '" . $cookie_name . "' is set!<br>";
- echo "Value is: " . $_COOKIE[$cookie_name];
- }
- ?>
- <p><strong>Note:</strong> You might have to reload the page to see the value of the cookie.</p>
- </body>
-
- </html>
Output:
Cookie 'employee' is set!
Value is: David Klarc
Note: You might have to reload the page to see the value of the cookie.
How To Delete a Cookie
A cookie can be simply deleted by using the same function set_cookie but with a date in the past. Consider the following example:
- <?php
-
- setcookie("employee", "", time() - 1800);
- ?>
- <html>
-
- <body>
- <?php
- echo "Cookie 'employee' is deleted.";
- ?>
- </body>
-
- </html>
Output:
Cookie 'employee' is deleted
How To check if Cookies are Enabled
You can simply check if cookies are enabled in a PHP script as follows:
- <?php
- setcookie("cookie_test", "check", time() + 3600, '/');
- ?>
- <html>
-
- <body>
- <?php
- if(count($_COOKIE) > 0) {
- echo "Cookies are enabled.";
- }
- else {
- echo "Cookies are disabled.";
- }
- ?>
- </body>
-
- </html>
Output:
Cookies are enabled.
Definition of Session
Session is a variable which is stored on the server side. It keeps values safe within the variable throughout the user’s interaction. It is a method of tracking and identifying returning users.
How Does It work
A unique identifier known as session id is stored on the client side. Whenever a HTTP request is passed the web server receives the session id.
How To Set Sessions
Session is started using session_start(). They are started with $_SESSION global variable.
Let's consider following examples to understand the concept of Session and cookies
Example 1:
Lets create a page test.php. In this page session variables will be created as follows:
- <?php
-
- session_start();
- $_SESSION["pet"] = "cat";
- $_SESSION["petowner"] = "david";
- echo "Session variables are set.";
- ?>
- </body>
-
- </html>
Output:
Session variables are set.
It should be noted that session is always started before any HTML tags.
Method2 for displaying Session variable value:
- <?php
- session_start();
- ?>
- <!DOCTYPE html>
- <html>
-
- <body>
- <?php
-
- echo "Favorite pet is " . $_SESSION["pet"] . ".<br>";
- echo "Pet owner is " . $_SESSION["petowner"] . ".";
- ?>
- </body>
-
- </html>
Output:
Favorite pet is cat.
Pet owner is david
How To Change A Session Variable Value?
You may change session variable value simply by overwriting it.
Consider the previous example:
- <?php
- session_start();
- ?>
- <!DOCTYPE html>
- <html>
- <body>
- <?php
-
- $_SESSION["pet"] = "rabbit";
- print_r($_SESSION['pet']);
- ?>
- </body>
- </html>
Output:
Rabbit
How To destroy a PHP Session
All session variables can be destroyed using function session_unset() and session_destroy()
Examples:
- <?php
- session_start();
- ?>
- <!DOCTYPE html>
- <html>
-
- <body>
- <?php
-
- session_unset();
-
- session_destroy();
- echo "All session variables are now removed, and the session is destroyed.";
- ?>
- </body>
-
- </html>
Output:
All session variables are now removed, and the session is destroyed.
Differences Between Session And Cookie
There are many differences between session and cookie. The major difference is that cookie variables are stored on the client's browser, whereas session variable is stored on the server side. The cookie expires according to the expiration date or time specified within the script whereas the lifespan of a session is until the user closes the browser.
Hence, in this article we have covered the procedure of declaring session and cookie variables. Also, we learned how to retrieve them. If you have any confusion about anything in this lesson, leave a comment and I’ll surely try to clear it up for you.