PHP | Unset Session Variable
Last Updated :
20 Jul, 2020
Whenever data are stored using cookies, there is a possibility of a hacker to insert some harmful data in the user's computer to harm any application. So its always advisable to use PHP sessions to store information on the server than on a computer. Whenever data is needed across many pages of a website or application, PHP
Session is used.
This
session creates a temporary file in a folder that stores registered variables with their values which are made available for the entire website. This
Session ends when the user logs out of the site or browser. Every different user is given unique session IDs that are linked to an individual's posts or emails.
<?php
session_start();
echo session_id();
?>
Note: The session IDs are randomly generated by the PHP engine which is difficult to make out.
Example: The following PHP function unregisters or clears a session variable whenever
$_SESSION is used in some code. It is mostly used for destroying a single session variable.
Note: The PHP
session_start() function is always written in the beginning of any code.
- Program 2:
php
<!DOCTYPE html>
<html>
<head>
<title>Unset Session Variable </title>
</head>
<body>
<h1 style="color:green">GeeksforGeeks</h1>
<b> Unset previous session of user</b>
<?php
echo '<br>';
echo '<br>';
if(isset($_SESSION["user_name"]))
{ echo "Welcome "; echo $_SESSION["user_name"]; }
?>
<form>
Input your name here:
<input type="text" id="user_id" name="user_id">
<input type=submit value=Submit>
</form>
<form action="#">
<input type="submit" name="submit"
value="Unset"
onclick="UnsetPreviousSession()">
</form>
<?php
session_start();
if(!isset($_SESSION["user_name"]) && (!empty($_GET['user_id'])))
{
$_SESSION["user_name"] = $_GET["user_id"];
}
else
{
UnsetPreviousSession();
}
function UnsetPreviousSession()
{
unset($_SESSION['user_name']);
}
?>
</body>
</html>
- Output: After clicking the Unset button, the following output screen is visible for re-entry which shows the previous session variable is unset.

Similar Reads
How to Use Session Variables with NodeJS? When building web applications with NodeJS, managing session data becomes an important task, especially for things like user authentication, shopping carts, or temporary data storage. In this article, we will explore how to use session variables in NodeJS.What are Session Variables?Session variables
5 min read
How to register a variable in PHP session ? The PHP session is required so that you can store the user information and use it on different pages of the browser. Approach: It creates a session with the name or any other useful information you want to store and access on different pages. Even after your page is closed you can access the informa
3 min read
How to set & unset session variable in codeigniter ? The session class in CodeIgniter allows the user to maintain a userâs âstateâ and track their activity while browsing the website. The session can be initialized using the library and auto-loaded in the environment using the following command. $this->load->library('session'); Set the session v
2 min read
PHP | Sessions A session in PHP is a mechanism that allows data to be stored and accessed across multiple pages on a website. When a user visits a website, PHP creates a unique session ID for that user. This session ID is then stored as a cookie in the user's browser (by default) or passed via the URL. The session
7 min read
PHP Variables A variable in PHP is a container used to store data such as numbers, strings, arrays, or objects. The value stored in a variable can be changed or updated during the execution of the script.All variable names start with a dollar sign ($).Variables can store different data types, like integers, strin
5 min read