PHP Cookie and Session

Welcome to all our readers on our website at As you all know that here on our website we will provide you lots of information related to the latest and newly introduced technology which help you to get updated with the latest tech. Here you will get all the useful information which is required in this tech world. As you know technology is one of the trending part in this world. So, here again we came up with a latest and new tech information which is related to the process of creating PHP Cookie and Session . With the help of this article you will get to know the complete process of how to create PHP Cookie and Session in a step by step manner. So, simply have a look to this article and grab all the useful information which is going to be very helpful for you.

PHP Cookie

  • Cookie is a small piece of information stored as a file in the user’s browser by the web server. Once created, cookie is sent to the web server as header information with every HTTP request.
  • You can use cookie to save any data but it should not exceed 1K(1024 bytes) in size.
  • Before we move on to how to create, update and delete a cookies, let’s learn a few real world use of cookies.
  • PHP cookie is a small piece of information or file that is stored in user’s browser.

PHP setcookie() function

  • In PHP, you can set cookies by using the setcookie() function or setrawcookie() function.
  • PHP setcookie() function defines to set a cookie and cookies are part of the with HTTP headers, so setcookie() function must be called before any output is sent to the browser.
  • Once the cookie is set, then you can access it by $_COOKIE superglobal variable. And the Cookie values can also exist in $_REQUEST.

The setcookie() function requires up to six arguments and should be appear before the <html> tag. For each cookie, setcookie() function has to be called separately:–

And it’s a setcookie (name, value, expire, path, domain, security);


bool setcookie ( string $name [, string $value [, int $expire = 0 [, string $path [, string $domain [, bool $secure = false [, bool $httponly = false ]]]]]] )


  1. setcookie(“CookieName”, “CookieValue”, time()+1*60*60, “/mypath/”, “”, 1);
  2. setcookie(“CookieName”, “CookieValue”, time()+1*60*60);   //using expiry in 1 hour(1*60*60 seconds or 3600 seconds)
  3. setcookie(“CookieName”, “CookieValue”);    /* defining name and value of cookie only*/


The PHP $_COOKIE superglobal variable is used to get cookie.


  1. $value = $_COOKIE[“CookieName”];   //returns the cookie value

PHP Cookie Example

File: cookie.php




Sorry, cookie is not found!

Firstly, the cookie is not set. But, when if you refresh the page,then you will see cookie is set now.

Second Output after refresh browser:-

Delete a Cookie in PHP

To delete/remove a cookie, we need to expire the cookie, which can be done by updating the cookie using the setcookie() function with expiration date in past.



PHP Session

  • To store information accessible across web pages, we use sessions. Session is not stored on the user browser like cookie, hence it is a more secure option.
  • As we know HTTP is a stateless protocol, if a user visits a webpage and perform some action, there is no way to remember what he did when the user navigates to the next webpage.
  • Let’s take a practical example, when you log into your Facebook account, by providing your email address and password, until and unless you logout, the web application remembers who you are and display what your friends are posting and liking on your News Feed, you can update your profile, send someone message, join a group etc, this is accomplished by Session.
  • Session is not limited by any size limit, you can store any information in the session, irrespective of its size.


  • A PHP session is easily started by making a call to the session_start()function.This function first checks if a session is already started and if none is started then it starts one. It is recommended to put the call to session_start() at the beginning of the page.
  • Session variables are stored in associative array called $_SESSION[]. These variables can be accessed during lifetime of a session.


bool session_start ( void )

Example: Store information

  1. $_SESSION[“user”] = “Tutorialpath”;

Example: Get information

  1. echo $_SESSION[“user”];

PHP Session Example

Let’s create a new page called “session.php”. In this page, you start a new PHP session and set some session variables.




PHP Session Counter Example



PHP Destroying Session – session_destroy()

After Creating the session, if you want to delete all the session variables completely, then in case SESSION_DESTROY() function will help you to achieve this goal.



Leave a Reply

Your email address will not be published.