PHP Session Vs Cookies

Hello Guys Welcome to Coding Gurus Online Portal ...!

PHP Session is a persistent storage type which holds the values set to the $SESSION[], until we attempt to destroy the session. Whereas Cookies are the temporary and also small storage. For each data, there must be a bundle of information like name, value, expiration period and etc will be send. And, Cookies preserves data at client side until it reaches the expiration time or the browser get closed.


Working with Sessions

For each session there will be life cycle to process over the following operation.

  • 1. Starting session
  • 2. Creating session variable
  • 3. Accessing session variable
  • 4. Clearing session variable

Starting session

The following code shows how to start a session. This function will mostly be in the first line of the PHP program which is working with session. Because, no output should be sent to the browser before getting current session id by this function.


Creating session variable

A session variable will be created by associating some required value to the global $_SESSION index. For example,

session_start(); $_SESSION["tagName"] = "PHP";

It can also be started automatically by setting appropriate flag in php.ini file.

Accessing session variable

Newly created sessions are stored into a temporary directory. So we can access these session from any PHP page. We can create a new PHP page and write the following code into it to check this step practically.

session_start(); $tag = $_SESSION["tagName"]; echo "Welcome to $tag world!";

Clearing session variable

If we think that a session variable is no longer needed, then it can be destroyed by the use of session_destroy(). But this method, allow the destroyed session to be remained until refresh or redirect to new page. If we want it destroy right immediately, then we need to call unset(). Let us go with an example code snippets as follows.

echo $_SESSION["tagName"];//session remains until refresh
echo $_SESSION["tagName"];//session no more;

Working with Cookies

There are only two possible steps while working with cookies. These are,

  • 1. Setting cookies
  • 2. Accessing cookies

Setting cookies

In PHP, There are two possible ways in setting cookies. One is by calling header() function by setting needed values for cookie header. For example,

header("Set-Cookie: platform=php; expires=Mon, 20-April-13 17:30:48 GMT; path=/;");

This method should be called in the first line of our program as like as session_start(). Because, the cookie details should be send before creating the header, and, these information should be added to the header. If any output send to the browser, then the header will automatically created without cookie information.

Second method of setting cookie is by the use of PHP built-in function setcookie(). It contains the name, value, expiration time of the cookie to be set and also the path and host details as follows.

setcookie("platform", "php", time()+7200, "/", "", 0);

Accessing cookies

There are three ways to access the cookies. Two of them are used to print all cookies from environment variable or global variable.
For example,

echo $_SERVER['HTTP_COOKIE']; // Output: platform=php echo getenv('HTTP_COOKIE'); // Output: platform=php

But the thrid method will be used to access the specified cookie by its name.

echo $_COOKIE["platform"]; // Output: php

Note :

Since COOKIES are not persistent storage type, we should prefer SESSION to store data that are expected to be non-volatile.