Jay
Jay

Reputation: 343

How to destroy php session without log out

Here is the code I'm using to log in a user :

    if($username==$db_username&&$password==$db_password)
    {
        session_start();
        echo 1;
        header("Location: index2.php");
        $_SESSION['username']=$db_username;

Now, this is supposed to assign a session to the username who has logged in. In my application, I don't have a log out option so I go to the log in page and send another username and password. So when these new information are sent, the old session must be destroyed and a new session to the new user should be assigned. Can I do that ?

Upvotes: 1

Views: 2773

Answers (3)

nullpointr
nullpointr

Reputation: 532

You should better use the session destroy method. It destroys all data which were stored into the session.

session_start();

//add some code of your app here

session_destroy();

Upvotes: 0

flowfree
flowfree

Reputation: 16462

Write logout.php and unset() the session.

session_start();
unset($_SESSION['username']);

Upvotes: 2

arijeet
arijeet

Reputation: 1874

What you can do is instead of using session_destroy (logout system) use the session_unset function to clear all session variables. Then use session_regenerate_id() to create a new session id and dump the old one(this is done automatically, you might have to send a 'true' boolean parameter along with that function), and assign new data to $_SESSION['username']

Check the docs for session_regenerate_id()

Upvotes: 2

Related Questions