Search code examples
phpsessionauthenticationdestroy

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 ?


Solution

  • Write logout.php and unset() the session.

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