我想在点击菜单项时使用注销功能。
如果我登录并单击注销它可以工作,但是当我点击另一个页面,然后单击注销它只是在URL的末尾添加了?logout。
我的代码。:
<?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(){
unset($_SESSION['authorized']);
header('Location: login.php');
exit();
}
我在任何其他页面上都有完全相同的菜单项,但它只是添加了?logout。唯一有效的页面是索引,所以我认为如何将url放入a href中会出现问题。
答案 0 :(得分:3)
这应该可以解决问题:
<li><a href="../index.php?logout">Uitloggen</a></li>
更好的是:
<li><a href="http://www.link-to-site.nl/index.php?logout">Uitloggen</a></li>
答案 1 :(得分:0)
试试这段代码:
<?
session_start();
session_unset();
session_destroy();
header("location:login.php");
exit();
?>
答案 2 :(得分:0)
在页面顶部添加ob_start()
function logout(){
unset($_SESSION['authorized']);
session_destroy();
header('Location: login.php');
exit();
}