How Can I Log Out Of My Website Using Php?
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_start
must be an array, not a string.
session_unset('$_SESSION['userId']');
session_unset
does not have any parameters.
session_destroy('$_SESSION['userId']');
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?"