Skip to content Skip to sidebar Skip to footer

How Can I Log Out Of My Website Using Php?

I just spent a few hours setting up a login system to my site and I finally logged in but I can't log out. I have tried using the session name and it still does not work./

Solution 1:

In your login.php

<?phpif(isset($_GET['logout']))
    {   
       unset($_SESSION['user_id']);
       //use this if you only want the user_id session to be unset.
    }
?>

Solution 2:

Keep in mind that $_SESSION is available after redirecting until refreshing the page. That means if you log out and get redirected, $_SESSION['userId'] is STILL available until you refresh the page one more time. That is why you still can see that. You are sending GET parameter - use it instead of $_SESSION['userId'] on your page:

if( isset($_GET['logout']) && $_GET['logout'] == 'success') {
    // display link to log in
} else {
    // display link to log out
}

In general these samples of code look a bit messy, I would not say you set the login functionality correctly.

Keep session_start() on the top of header or call it when you really need it, you need to start it only once in general.

Beside that, do not use form for log-out - use <a href="<logout path>"...>LOGOUT</a> it will be way better.

Solution 3:

following three line of code will work fine if you don't need any session variable after logout.Please check this out.

session_start();
session_destroy();
header("Location: ../../index.php?logout=success");

Solution 4:

session_start('$_SESSION['userId']');

The first (and only) parameter of session_startmust be an array, not a string.

session_unset('$_SESSION['userId']');

session_unsetdoes not have any parameters.

session_destroy('$_SESSION['userId']');

Neither does session_destroy.

These do not do what you think they do, which is likely the root cause of your troubles.

Post a Comment for "How Can I Log Out Of My Website Using Php?"