---
title: "How to create and destroy session in php?"  
description: "Session management is a mechanism to maintain state about a series of requests from the same user across some period of time. That is, the term \"sessi"  
author: "Anonymous User"  
published: 2011-09-20  
updated: 2014-09-18  
canonical: https://www.mindstick.com/blog/235/how-to-create-and-destroy-session-in-php  
category: "php"  
tags: ["php"]  
reading_time: 3 minutes  

---

# How to create and destroy session in php?

[Session management](https://www.mindstick.com/forum/158230/what-is-session-persistence-and-how-does-it-affect-session-management) is a mechanism to maintain state about a series of requests from the same user across some period of time. That is, the term "session" refers to the time that a user is at a particular [web site](https://www.mindstick.com/articles/12285/the-finest-web-site-design-trends-you-may-expect-in-2017). The problem is that HTTP has no mechanism to maintain state. Individual requests aren't related to each other. The [Web server](https://www.mindstick.com/articles/23199/digital-personal-server-the-premium-web-server) can't easily distinguish between single users and doesn't know about [user sessions](https://www.mindstick.com/forum/159941/what-are-the-best-practices-for-handling-user-sessions-and-cookies-in-web-apps). Session management refers to the way that associate data with a user during a visit to a [Web page](https://www.mindstick.com/forum/718/sql-server-report-alignment-to-center-of-web-page).\

A PHP [session variable](https://www.mindstick.com/forum/12804/how-to-set-a-asp-dropdownlist-selectedvalue-to-a-session-variable) is used to store information about, or change settings for a user session. [Session variables](https://www.mindstick.com/forum/386/retreive-data-from-bo-object-to-session-variables) hold information about one single user, and are available to all pages in one application. I.e. A session is a way to store information (in the form of variables) to be used across multiple pages. Unlike a cookie, specific variable information is not stored on the user’s computer. It is also unlike other variables in the sense that we are not passing them individually to each new page, but instead retrieving them from the session we open at beginning of each page.

##### Start Session:

Before you can store user information in your PHP session, you must first start up the session. session_start() function is used for start session into PHP page.

##### Syntax:

```
?php// Start session  session_start();?><html>    <head>        <title></title>    </head>    <body>            </body>    </html>
```

##### Destroying Session:

There are basically two [functions](https://www.mindstick.com/forum/160140/explain-the-role-of-functions-as-a-service-faas-in-serverless-computing) which is for destroying Session, first one is unset() function and second one is session_destroy() .

The unset() function is used to free specified session variable.

##### Example: Using unset function

\

```
 <?php// Start Session session_start();// check session value has been set or not$_SESSION['TestUnset'] ="Unset";?><!DOCTYPE html><html>    <head>        <meta http-equiv="Content-Type" content="text/html; charset=windows-1252">        <title>PHP Session</title>    </head>    <body>        <div id ="viewContainer">            <h1> This is session page testing </h1>         <?php        // dispaly number of visitor in  page           echo 'Session Value is :  '.$_SESSION['TestUnset']."</br>";           unset ($_SESSION['TestUnset']);           echo 'Session Destroyed'."</br>";           echo 'Now Session Value is :  '.$_SESSION['TestUnset'];        ?>       </div>    </body></html>
```

##### Example: using session_destroy function

\

```
<?php// Start Session session_start();// check session value has been set or not$_SESSION['TestUnset'] ="Unset";?><!DOCTYPE html><html>    <head>        <meta http-equiv="Content-Type" content="text/html; charset=windows-1252">        <title>PHP Session</title>    </head>    <body>        <div id ="viewContainer">            <h1> This is session page testing </h1>         <?php        // dispaly number of visitor in  page           echo 'Session Value is :  '.$_SESSION['TestUnset']."</br>";           session_destroy() ;           echo 'Session Destroyed'."</br>";           echo 'Now Session Value is :  '.$_SESSION['TestUnset'];        ?>       </div>    </body></html>
```

---

Original Source: https://www.mindstick.com/blog/235/how-to-create-and-destroy-session-in-php

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
