---
title: "Session in PHP"  
description: "A session is used to store information in variables. These php variables are used to store user information to be used across multiple pages. Unlike a"  
author: "Jayden Bell"  
published: 2016-06-08  
updated: 2018-03-15  
canonical: https://www.mindstick.com/blog/11147/session-in-php  
category: "php"  
tags: ["php", "session"]  
reading_time: 2 minutes  

---

# Session in PHP

A session is used to [store](https://www.mindstick.com/articles/13125/tips-to-increase-sales-of-your-woocommerce-store) information in variables. These [php variables](https://www.mindstick.com/Articles/12149/php-constant-and-php-variables) are used to store user information to be used across [multiple](https://www.mindstick.com/blog/12797/iowa-is-expected-to-see-heavy-growth-in-multiple-sectors) pages. Unlike a [cookie](https://www.mindstick.com/blog/123/what-is-cookie-poisoning) information is not [stored](https://www.mindstick.com/forum/157561/what-is-the-stored-procedure-create-a-procedure-to-find-the-record-by-stu_id-from-the-student-table) in the user computer.PHP session is started by using **session_start()** function.

##### Now, let’s create a new page session.php

```
<?php
session_start(); // Start the session
$_SESSION["name"] = "Geeta";  // Set session variables
$_SESSION["age"] = "23";
echo "Session variables are set.";
?>
```

##### Output:

[Session variables](https://www.mindstick.com/forum/386/retreive-data-from-bo-object-to-session-variables) are set.

##### Get PHP session variable

We will create an another page **(demo.php)** from which we get the [session variable](https://www.mindstick.com/forum/12804/how-to-set-a-asp-dropdownlist-selectedvalue-to-a-session-variable) from session.php\

```
<?php
session_start();
?>
<?php
```

echo "My name is " . $_SESSION["name"] . ".<br>";// session variables that were set on [previous](https://yourviews.mindstick.com/view/81421/unlock-2-0-is-just-like-previous-corona-lockdowns) page\
echo "Age: " . $_SESSION["age"] . ".";?>\

[Output](https://www.mindstick.com/forum/161319/how-does-the-print-function-handle-output-in-python):

My name is Geeta.

Age: 23.

##### Modify session Variable

To modify a session variable just overwrite.

```
<?php
session_start();
$_SESSION["name"] = "Ragini";// to modify a session variable
print_r($_SESSION);
?>
```

Output:

Array ( [name] => Ragini [age] => 23 )// Ragini will be overwritten

To destroy Session

To destroy session variable and session use **session_unset()** and **session_destroy().**

```
<?php
session_start();
session_unset();// remove all session variables
// destroy the session
session_destroy();
echo " Session variable are removed and session is destroyed "
?>
```

Output:

Session variable are removed and session is destroyed.

\

---

Original Source: https://www.mindstick.com/blog/11147/session-in-php

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
