24

the below one is the link in my php site.. after clicking this button the user's session should be terminated and he should be redirected again to the home page.. i have written the coding for this concept as follows but it shows me only a blank page(it is not redirected to the home page).. please correct my codings

<a href="Logout.php">
click here to log out</a>

codings in the Logout.php a follows

<?
session_start();
session_unset();
session_destroy();
ob_start();
header("location:home.php");
ob_end_flush(); 
include 'home.php';
//include 'home.php';
exit();
?>

6 Answers 6

57

Only this is necessary

session_start();
unset($_SESSION["nome"]);  // where $_SESSION["nome"] is your own variable. if you do not have one use only this as follow **session_unset();**
header("Location: home.php");
Sign up to request clarification or add additional context in comments.

1 Comment

All right, nice it is working for you. The session is something very strange at a first glance, but once you learned how to use you will never forget it. :-)
23

Use this instead:

<?
session_start();
session_unset();
session_destroy();

header("location:home.php");
exit();
?>

4 Comments

it is not working .. i dont know the reason.. otherthings works very well except the logout page
use ini_set('display_errors',1); error_reporting(E_ALL); before sesson_start and see if any errors popup ( tough it shouldn't )
"Fernando Costa's" answer worked for me ..i thank you too for ur immediate response
session_unset() is unnecesary.
6
<?php
session_start();
session_destroy();
header("Location: home.php");
?>

Comments

4
<?php

session_start();

session_unset();

session_destroy();

header("location:home.php");

exit();

?>

Comments

0
<?php //initialize the session if (!isset($_SESSION)) {   session_start(); } 
// ** Logout the current user. **
$logoutAction = $_SERVER['PHP_SELF']."?doLogout=true";
if ((isset($_SERVER['QUERY_STRING'])) && ($_SERVER['QUERY_STRING'] != "")){
    $logoutAction .= "&". htmlentities($_SERVER['QUERY_STRING']); 
}

if ((isset($_GET['doLogout'])) &&($_GET['doLogout']=="true")) {
    // to fully log out a visitor we need to clear the session variables
    $_SESSION['MM_Username'] = NULL;
    $_SESSION['MM_UserGroup'] = NULL;
    $_SESSION['PrevUrl'] = NULL;
    unset($_SESSION['MM_Username']);  
    unset($_SESSION['MM_UserGroup']);
    unset($_SESSION['PrevUrl']);
    $logoutGoTo = "index.php";

    if ($logoutGoTo) {
        header("Location: $logoutGoTo");
        exit;
    }
} ?>

2 Comments

Please don't just post code-only answers. Please explain why your code is the answer.
ahahaha responding to @LeeTaylor by a line of code, I don't know if I want to cry or to laugh
0

The simplest way to log out and redirect back to the login or index:

<?php
    if (!isset($_SESSION)) { session_start(); }
    $_SESSION = array(); 
    session_destroy(); 
    header("Location: login.php"); // Or wherever you want to redirect
    exit();
?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.