All TalkersCode Topics

Follow TalkersCode On Social Media - A Social Media Network for developers Join Now ➔

Session Unset In PHP

Last Updated : Mar 11, 2024

Session Unset In PHP

In this tutorial we will show you the solution of session unset in PHP, here first we need to start session and we setting session name to one string then printing session name.

Using if() condition we checking session name is set or not if sets then we unsetting session by unset() method.

As we know session used to store data on a server rather than the computer of the user so we used unset() to resets the variable we stored on session.

Step By Step Guide On Session Unset In PHP :-

When handling session we need to start session first then only we can use them so we start our session then we storing ‘name:john’ to session.

Now we can use this name on various pages by referring session.

We printed session name using echo() then we checks using if() condition if session name is set or not is they get set then we just resetting by using unset() method.

So now session name is not set it gives error when we try to print means.

echo "Session name is : ".$_SESSION['name']."<br>";
else{echo "session is set.";}
function unsetss(){
    echo "Session is unset & its name is: ".$_SESSION['name'];
  1. A php script can be placed anywhere in the document. A php script starts with <?php and end with ?>.
  2. The default file extension for php files is “.php” and php statements end with ‘;’ semicolon.
  3. Here we defined session start() function for start session. Then we inserting name ‘John’ on session variable it will remind this name until we unset them so we can have it where we need it.
  4. We printed session name using echo it will print ‘John’ then here we needs to unset so we have to set them first then only we can unset because initially they don’t have any values.
  5. Then we checks whether session name is set or not by isset() method if they sets then we resets using unset() method it will reset them easily by calling function unsetss().
  6. In unsets() function we defined unset() method on session[‘name’] variable then displayed message session is unset.

Conclusion :-

In conclusion we are able to know how unset session using php.

First we need to start our xampp server then we load this program on browser we can see result of displayed session name then in next line we can see message ‘session is unset & its name is’ with warning because now session had nothing so if your try print it will definitely throw some warning.

We can use this concept on login forms or register forms like because there we can save logged in user details even they are redirect to another page and we can use those details on that navigated page also it’s really useful we will see about them in later.

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

Author Image About Riya

A recent graduate with a Bachelor of Technology (B.Tech) in Computer Science from India. She is passionate about leveraging technology to solve real-world problems. With a strong foundation and experience in programming languages such as Python, Django, HTML, CSS, and JavaScript, java, php and have honed her skills through hands-on projects and coursework.

Follow Riya On Linkedin 🡪