php 退出按钮php

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12209438/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 03:02:46  来源:igfitidea点击:

Logout button php

phplogout

提问by crusader1223

I have this code and need the code to add a logout button, can anyone write out the code for a log out button that will log out the user, I read something about destroy session but do not know how to write the code out, thank you!

我有这个代码并且需要代码来添加一个注销按钮,任何人都可以写出一个注销按钮的代码来注销用户,我读了一些关于销毁会话但不知道如何写出代码的内容,谢谢你!

<?php
    include 'connection.php';
    //start of checking if user is logged in code
    if (!valid_credentials) {
    header('Location: login.php');
    exit();
    }

$_SESSION['user'] = 'username';


if (!isset($_SESSION['user'])) {
    header('Location: login.php');
    exit();
}
    //end of logged in code and starting a session

$query = "SELECT * FROM people";
$result = mysql_query($query);
While($person = mysql_fetch_array($result)) {
    echo "<h3>" . $person['Name'] . "</h3>";
    echo "<p>" . $person['Description'] . "</p>";
    echo "<a href=\"modify.php?id=" . $person['ID']. "\">Modify User</a>";
    echo "<span> </span>";
    echo "<a href=\"delete.php?id=" . $person['ID']. "\">Delete User</a>";
}
?>
<h1>Create a User</h1>
<form action="create.php" method="post">
    Name<input type ="text" name="inputName" value="" /><br />
    Description<input type ="text" name="inputDesc" value="" />
    <br />
    <input type="submit" name="submit" />
</form>

回答by asprin

Instead of a button, put a link and navigate it to another page

而不是按钮,放置一个链接并将其导航到另一个页面

<a href="logout.php">Logout</a>

Then in logout.phppage, use

然后在logout.php页面中,使用

session_start();
session_destroy();
header('Location: login.php');
exit;

回答by Mic1780

When you want to destroy a session completely, you need to do more then just

当您想完全销毁会话时,您需要做的不仅仅是

session_destroy();

First, you should unset any session variables. Then you should destroy the session followed by closing the write of the session. This can be done by the following:

首先,您应该取消设置任何会话变量。然后你应该销毁会话,然后关闭会话的写入。这可以通过以下方式完成:

<?php
session_start();
unset($_SESSION);
session_destroy();
session_write_close();
header('Location: /');
die;
?>

The reason you want have a separate script for a logout is so that you do not accidently execute it on the page. So make a link to your logout script, then the header will redirect to the root of your site.

您需要一个单独的注销脚本的原因是,您不会意外地在页面上执行它。因此,创建一个指向您的注销脚本的链接,然后标题将重定向到您站点的根目录。

Edit:

编辑:

You need to remove the () from your exit code near the top of your script. it should just be

您需要从脚本顶部附近的退出代码中删除 ()。它应该只是

exit;