清除登录,删除会话,然后使用ISPOSTBACK -PHP重定向回到登录页面



我今天花了很多时间研究这个网站以寻求解决方案,但我没有运气。我目前正在尝试学习PHP并从事我的第二个项目。我只能使用PHP。我最初在单独的logout.php文件中进行了删除会话,并重定向。这在起作用,但后来我发现我做不到。我被指示需要"清除登录,删除会话并将其重定向返回登录页面",然后在结果中的ISPostback中执行此操作。经过今天的大量研究,我认为我正在理解如何做到这一点,但我无法正常工作。希望我能得到一些帮助。

<?php
session_start();
//require_once('cookies.php');
$isPostBack = filter_input(INPUT_GET, 'submit');
//this is where I need to do the isPostBack for user clicking "logout".
if ($isPostBack) {
    // clear ALL session data from memory
    // clean up the session and remove the session ID.
    // redirect to index.php
        endSession();
        session_destroy();
        header("Location: index.php");
} else {
   // user did not click logout doNothing();
}
?>
<html lang="en">
<head>
    <title>Results</title>
    <link rel="stylesheet" type="text/css" href="">
</head>
<body>
<form action="results.php">
    <input type="submit" id="submit" name="submit" value="Logout" />
</form>
<section>
    <?php
    foreach($_SESSION['answers'] as $answer){
        echo "<p>$answer</p>";
    }
    ?>
</section>
</body>

尝试提供名称属性

<input type="submit" id="submit" value="Logout" name="logout"/>

并仅使用注销变量代替提交或提供两个不同的字段

$isPostBack = filter_input(INPUT_GET, 'submit');
$isPostBack = filter_input(INPUT_GET, 'logout');

我似乎找到了我的解决方案。我需要给Ispostback变量一个名称,该名称与注销按钮的名称相匹配。我还需要在Ispostback之后包括!== null。我更改了Endessess();到$ _session = array();根据我的研究,Endession();"删除所有会话变量"。它似乎现在应该正常工作。这是我编辑的代码。

<?php
session_start();
$isPostBack = filter_input(INPUT_GET, 'submit')!==NUll;
//this is where I need to do the isPostBack for user clicking "logout".
if ($isPostBack) {
    // clear ALL session data from memory
    // clean up the session and remove the session ID.
    // redirect to index.php
        $_SESSION = array();
        session_destroy();
        header("Location: index.php");
} else {
   // user did not click logout doNothing();
}
?>
<html lang="en">
<head>
    <title>Results</title>
    <link rel="stylesheet" type="text/css" href="">
</head>
<body>
<form action="results.php">
    <input type="submit" id="submit" name="submit" value="Logout" />
</form>
<section>
    <?php
    foreach($_SESSION['answers'] as $answer){
        echo "<p> $answer</p>";
    }
    ?>
</section>
</body>

如果您需要删除特定的会话值,则可以使用unset()

unset ($_SESSION['userid'])

最新更新