-1

I have a logout button on my site's navigation bar (which appears on every page on the site). It only seems to logout the user when the user is already on the home page. If they are on any other page of the site, the logout button only redirects to the homepage but all the $_SESSION variables are still set.

HTML (from navigation bar)

<form class = "dropdown-item "action="includes/logout.inc.php" method="POST">
    <button class="btn btn-success navbar-btn" type="submit" name ="submit" >Logout</button>
</form>

PHP (/logout.inc.php)

if (isset($_POST['submit'])) {
    session_start();
    session_destroy();
    header("Location: ../home.php");
}

I also tried adding $_SESSION = array(); to the php script but this made no difference.

karras166
  • 1
  • 1
  • I solved this. I did not find the "duplicate" answers helpful as in fact the problem was not with sessions or the logout script.I needed to make my form action link root-relative. I changed it to `action="/includes/logout.inc.php"` and now it works fine. – karras166 Apr 22 '20 at 16:48

1 Answers1

-1

I used the following approach.

  1. In my HTML file:

    <a href="logout.php" class="btn btn-danger">Sign Out</a>

  2. And in the logout.php file

    <?php session_start(); $_SESSION = array(); session_destroy(); header("location:../home.php"); ?>

It works like a charm. Good Luck

user7319461
  • 11
  • 1
  • 6