gpt4 book ai didi

php - 如何通过结束 session 从站点注销

转载 作者:太空宇宙 更新时间:2023-11-03 12:35:13 25 4
gpt4 key购买 nike

我在退出网站时遇到问题。到目前为止,我已经登录了,但是,当我创建一个 logout.php 并包含 session_destroy();它似乎不起作用。

这是我的每个页面的代码。

索引.php:

<?php include ("login.php"); ?>

登录.php

   <?php
session_start();
$_SESSION['loggedin']['user']=$_POST['user'];
if(isset($_SESSION['loggedin'])){
echo '<h2>Logged in</h2><p style="font-size:14px;">Welcome '.$_SESSION["user"].' you have sucessfully logged in.</p>';
}
else {
echo '
<h2>Login</h2>
<form action="auth.php" method="POST">
<div class="smallform">
<p><span>Username:</span><br>
<input type="text" name="user"></p>
<p><span>Password:</span><br>
<input type="password" name="pass"></p>
<p style="padding-top: 15px"><input type="submit" value="Submit"></p>
</div>
</form>';
}
?>

授权.php:

   <?php
session_start();
$con = mysqli_connect("HOST", "USER", "PASS", "DBNAME");
$user = $_POST["user"];
$pass = $_POST["pass"];
$sql = "SELECT UserID FROM Customer
WHERE UserID = \"$user\"
AND Password = md5(\"$pass\")";

$res = mysqli_query($con, $sql);
?>
<!DOCTYPE>
<html>

<head>
<title>VeloCity</title>
<link href="_stylesheet.css" rel="stylesheet" type="text/css" />
<?php echo '<meta http-equiv="refresh" content="1;URL=index.php" />'; ?>
</head>

<body>
<div align="center">
<?php
if(mysqli_num_rows($res)==1){
$_SESSION["user"] = $user;
$_SESSION["loggedin"] = True;
echo "You have sucessfully logged in";
}
else{
echo "You have entered an incorrect password. Please try again";
}
?>
</div>
</body>
</html>

注销.php

<?php
session_start();
session_destroy();
?>

在 login.php 中添加的另一件事我有一个 if 语句,它是:

if(isset($_SESSION['loggedin'])){
echo '<h2>Logged in</h2><p style="font-size:14px;">Welcome '.$_SESSION["user"].' you have successfully logged in.</p>';
}

如您所见,问题是它应该显示欢迎“USERNAME”,最初是显示,但现在不是,它只是显示欢迎,您已成功登录。

我想提前感谢大家的帮助。谢谢

最佳答案

一些注意事项:

  1. 您的输入未经过过滤。
  2. 尽量不要使用 mysql_* 函数,因为它们已被弃用。请改用 PDOmysqli_*

logout.php中使用这段代码:

<?php
session_start();
// Because you are checking if(isset($_SESSION['loggedin'])), use the below:
unset($_SESSION['loggedin']);
$_SESSION = array();
session_destroy();
?>

关于php - 如何通过结束 session 从站点注销,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13532063/

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