user3173360
user3173360

Reputation:

Logout function not working

I want to use a logout function when clicking on a menu item.

If I login and click logout it works, but the moment I click on another page, and then click logout it just adds ?logout at the end of the url.

My code.:

<?php
   session_start();
   include '../includes/includes.php';
   require('../includes/functions.php'); 
   //make sure user is logged in, function will redirect use if not logged in
   login_required();

   //if logout has been clicked run the logout function which will destroy any active sessions and redirect to the login page
   if(isset($_GET['logout']))
   {
     logout();
   }
?>

<html>
<title>Admin</title>
<header><link rel="stylesheet" type="text/css" href="../css/style.css"></header>
<body>
    <nav>
     <ul>
       <li><a href="../index.php" target=_blank"">Bekijk site</a></li>
       <li><a href="voegtoe.php">Toevoegen</a></li>
       <li><a href="verwijder.php">Posts</a></li>
       <li><a href="?logout">Uitloggen</a></li>
     </ul>
    </nav>
</body>

</html>

Logout function:

//Logout
function logout(){
    unset($_SESSION['authorized']);
    header('Location: login.php');
    exit();
}

I have the exact same menu items on any other page, but there it just adds ?logout. The only page that is working is index, so I think something is wrong with how the url is put in the a href.

Upvotes: 0

Views: 263

Answers (3)

Krish R
Krish R

Reputation: 22711

Add ob_start() top of the page

 function logout(){        
    unset($_SESSION['authorized']);
    session_destroy();
    header('Location: login.php');
    exit();
 }

Upvotes: 0

Priya jain
Priya jain

Reputation: 703

try this code:

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

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

Upvotes: 0

Fyntasia
Fyntasia

Reputation: 1143

This should do the trick:

<li><a href="../index.php?logout">Uitloggen</a></li>

Better would be:

<li><a href="http://www.link-to-site.nl/index.php?logout">Uitloggen</a></li>

Upvotes: 3

Related Questions