-1

When an admin is logged in , the admin will have a admin page on the nav bar. When the member is logged in, the member will have a profile page on the nav bar.

However, lets say when the admin logged out, it will goes to a logout page hat says logged out and when the administrator goes back to the home page, the administrator is still logged in and the admin page is still on the navbar.

This is my codes for the login and logout.

<?php
//checks if login session variable exist? If it does, display Logout
session_start();
if(isset($_SESSION['login']) && $_SESSION['login']!="") {
    //link to page logout.php and displays the word Logout + username
    echo"<li><a href='logout.php'>Logout ".$_SESSION['login']."</a></li>";
    if(isset($_SESSION['login'])&& $_SESSION['usertype'] !="admin") {
        echo ("<li><a href='profile.php'>Profile</a></li>");                         
    } else {
        echo ("<li><a href='AdminPage.php'>Administrator</a></li>");
    }
    if($_SESSION['login'] == "") {
        header("Location:login.php");
    } 
    if($_SESSION['login'] != "" && $_SESSION['usertype'] =="member") {
        header("location:home.php");
    }
} else {
    //else link to pagelogin.php and display the word Login
    echo("<li><a href ='pagelogin.php'>Login</a></li>");
}
?>

How do I rectify the error so that when the user is logged in the user can log out successfully.

Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
  • "when the administrator goes back to the home page, the administrator is still logged in and the admin page is still on the navbar." Means admin go back from browser back button link? – B. Desai Jan 31 '17 at 11:04
  • this may not be an answer to the question, but `if($_SESSION['login'] == "")` will be never true because it is inside a `if($_SESSION['login']!="")` test – Kaddath Jan 31 '17 at 11:08

3 Answers3

0

in your logout.php file you just need to do the basic:

<?php

session_start();

session_destroy();

header("Location: pagelogin.php"); // or whatever your login file is called
?>
B. Desai
  • 16,414
  • 5
  • 26
  • 47
Option
  • 2,605
  • 2
  • 19
  • 29
  • That is good and it works but how do i still show the text "you have logged out successfully " on the logout page before directing back to the home page? – Yong Jun Run Jan 31 '17 at 11:09
  • You don't have to do it exactly the way I suggest with the header location. You could make logout divert to loggedout.php with a message. But generally you'd make it so if logged in a user cannot see the login screen. – Option Jan 31 '17 at 11:10
  • If you're happy with the answer please mark this as resolved. – Option Jan 31 '17 at 11:18
0

on logout you have to destroy user's session: in your logout page write code

session_destroy();

2nd method is to remove data from your session variables, in your logout page write code

$_SESSION['login'] = '';
$_SESSION['usertype'] = '';
session_destroy();

after destroy session and session variable send him back to login page

header('location:login.php?msg="You Have successfully logout....!!"');
Dani
  • 905
  • 7
  • 14
0

Code for destroying session, logout conditions etc is required but You dont have control over browser's back button, you have to write a code to clear cache and its max-age etc. search on below mentioned details. you might need to add more code in html header / php header.

<?php

header("Cache-Control: no-store, no-cache, must-revalidate, max-age=0");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");
?>

You can check this link might help How to prevent Browser cache for php site

Community
  • 1
  • 1
Mangesh Sathe
  • 1,987
  • 4
  • 21
  • 40