gpt4 book ai didi

php - 注销需要点击两次

转载 作者:行者123 更新时间:2023-12-03 00:59:39 33 4
gpt4 key购买 nike

早上好。

构建一个网络应用程序来管理汽车,如您所见 in this image .

您在上图中看到的是文件index.php,它被配置为在用户是否登录时显示不同的内容:

<body>
<div class="container">
<h2>Welcome to the Automobiles Database</h2>
<?php
if ( isset($_SESSION['error']) ) {
echo('<p style="color: red;">'.htmlentities($_SESSION['error'])."</p>\n");
unset($_SESSION['error']);
}
if ( isset($_SESSION['success']) ) {
echo('<p style="color: green;">'.htmlentities($_SESSION['success'])."</p>\n");
unset($_SESSION['success']);
}
?>


<!-- without login -->
<?php if(!isset($_SESSION['name'])) {
echo '<p><a href="login.php">Please log in</a></p>';
echo '<p>Attempt to <a href="add.php">add data</a> without logging in</p>';
} ?>


<!-- with login -->
<?php if(isset($_SESSION['name'])) {
echo '<table border="1"><thead><tr><th>Make</th><th>Model</th><th>Year</th><th>Mileage</th><th>Action</th></tr></thead>';
$smtp = $pdo->query("SELECT autos_id, make, model, year, mileage FROM autos ORDER BY make");
while ($row = $smtp->fetch(PDO::FETCH_ASSOC)) {
echo("<tr><td><b>");
echo($row['make']);
echo("</b></td><td><b>");
echo($row['model']);
echo("</b></td><td><b>");
echo($row['year']);
echo("</b></td><td><b>");
echo($row['mileage']);
echo("</b></td><td><b>");
echo("<a href=\"edit.php?autos_id=".$row["autos_id"]."\">Edit</a> / <a href=\"delete.php?autos_id=".$row["autos_id"]."\">Delete</a>");
echo("</b></td><tr>\n");
}
echo '</table>';
echo '<p><a href="add.php">Add New Entry</a></p> <p><a href="?logout">Logout</a></p>';
if(isset($_GET['logout'])) {
session_unset();
}
} ?>



</div>
</body>

我面临的问题与“注销”链接有关,如下所示:

echo '<p><a href="add.php">Add New Entry</a></p> <p><a href="?logout">Logout</a></p>';

如果我点击一次,this is the result i get .

这会按预期注销用户,但我希望它 reach this page立即(这是没有登录的index.php)并且要实现此目的,我必须在链接中单击两次...

注销.php:

session_start();
unset($_SESSION['name']);
unset($_SESSION['user_id']);
header('Location: index.php');

我该怎么做?

BP

最佳答案

我已将 Logout.php 更改为:

<?php
session_start();
unset($_SESSION['name']);
unset($_SESSION['user_id']);
header('Location: index.php');
?>

以及注销链接:

echo '<p><a href="add.php">Add New Entry</a></p> <p><a href="logout.php">Logout</a></p>';

现在工作正常了!

关于php - 注销需要点击两次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51036922/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com