All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

How To Unset Session In PHP

Last Updated : Mar 11, 2024

How To Unset Session In PHP

In this tutorial we will show you the solution of how to unset session in PHP, as, we know that session in php is used to store data securely.

We can also use cookies but these are not secured method because data is stored in user’s computer. So, we use session instead of cookies.

One thing to note here that we always have to destroy session after start session.

Step By Step Guide On How To Unset Session In PHP :-

Here, to unset session we have to start session variable. So, for this we use session_start() function in php at the beginning of webpage or say at codes.

If you want to know more about start session in php then you can visit to our articles in which we learn about what session and how to start them.

Whereas to we are going to show the codes to unset session, let us see the codes given below to see how to unset session.

<!DOCTYPE html>
<html>
<head>
<title>Page Title</title>
</head>
<body>
<?php
session_start();
if( isset($_SESSION['name']) ) {
    echo 'Session name is set.'.'<br>';
}
else {
    echo 'Session name is not set!'.'<br>';
}
 echo'<br>';
 $_SESSION['name'] = 'TalkersCode';
 echo "Session Name : ".$_SESSION['name'].'<br>';
?>
// after run above code use the below code where we use unset session code.
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<title>Page Title</title>
</head>
<body>
<?php
session_start();
if( isset($_SESSION['name']) ) {
    echo 'Session name is set.'.'<br>';
}
else {
    echo 'Session name is not set!'.'<br>';
}
 echo'<br>';
 $_SESSION['name'] = 'TalkersCode';
unset($_SESSION['name']);
session_destroy();
?>
</body>
</html>
  1. First, we write <! DOCTYPE html> which we used as an instruction to the web browser about what version of HTML file is written in.
  2. Secondly, the <html> tag is used to indicate the beginning of an HTML document.
  3. As above now <head> tag is used to contain information about web page. In this tag a <title> tag is used which helps us to specify a webpage title. Both <head> and <title> tags are Paired tags. So, both have </head> and </title> ending tags respectively.
  4. Here, then we create a body tag. All the content which we want to show on browser’s screen or display is always written inside this codes.
  5. Here, first of all as we can see that we use basic php structure. And inside this <?php is our opening tag of php and ?> is our closing php.
  6. Here, as we see that we create a session with variable name. And inside this we store value of a string. If our session is set then it will show you message in if condition otherwise else runs.
  7. Now, after getting a successful message of session set. We add a line of unset session and destroy session. One thing to note here that unlink in session is used to clear or free up space occupied by session variables whereas destroy in session is used to destroy all the session variables completely.
  8. At last after running the code of unset session. Our session variable gets cleared and the message in else condition runs successfully. We hope you understand unset session easily now.
  9. At last, the <body> and <html> tags are closed with </body> and </html> respectively.

Conclusion :-

At last in conclusion, here we can say that with the help of this article we are able to understand how to unset session in php.

I hope this tutorial on how to unset session in PHP helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Dikshita

Passionate Electronics and Communication Engineering student with expertise in web development (HTML, CSS, JS,PHP, Bootstrap, React.js) and content writing. Eager problem solver and tech enthusiast, adept at creating engaging web experiences.

Follow Dikshita On Linkedin 🡪