gpt4 book ai didi

php - 使用 cookie 时注销

转载 作者:行者123 更新时间:2023-12-02 10:47:31 24 4
gpt4 key购买 nike

我是使用 php cookie 的初学者,我正在尝试使用 cookie 制作一个简单的登录和注销表单。一切都很好,但是当我按注销链接时,我无法注销。要注销,我必须从浏览器中删除 cookie。

登录页面

<?php
session_start();

if (isset($_COOKIE["Email"])){
header("location: home.php");
}
?>


<form method="post" action="log_in.php">
<font size="6">Sign In</font>

Email Address: </b></font><input type="text" name="Email" id="email" />

password: <input type="password" name="password" id="password" />

<input type="checkbox" name="rememberMe" value="1" id="check"/> Remember Me

<input type="submit" name="Login" id="sign" value="sign in" >

<?php
include 'db.php';
if(isset($_POST['Login'])){
$user_email = $_POST['Email'];
$password = $_POST['password'];

$check_user = "SELECT * FROM user where user_email = '$user_email' AND user_pass = '$password'";
$run = mysql_query($check_user );

if (mysql_num_rows($run) > 0){
$_SESSION['Email']= $user_email;

$_SESSION['start'] = time();
if(isset($_POST['rememberMe'])){
$expire=time()+120;
setcookie("Email", "Email", $expire);
}
else{
$expire=time()+30;
setcookie("Email", "Email", $expire);
}
echo "<script>window.open('home.php','_self')</script>";
}
else {
echo "<script>alert('email or password incorrect!')</script>";
}}
?>

首页

<?php
if (isset($_COOKIE["Email"])){
echo "Welcome " . $_COOKIE["Email"] . "!<br>";
echo '<a href="logoutForm.php">logout</a>';
}
else{
$now = time(); // Checking the time now when home page starts.
if ($now > $expire) {
session_destroy();
header("location: log_in.php");
}}

退出页面

 <?php
session_start();
unset($_SESSION['Email']);
session_destroy();
header("Location: log_in.php");

if(isset($_SESSION['Email'])):
setcookie($_SESSION['Email'],'',time()-7000000,'/');
endif;
?>

最佳答案

您的主页(代码)没有 session_start(); 至少在您发布的内容中没有;使用session_destroy()时需要它;它本身不起作用。

尝试一下:

旁注: $expire 对于 home page 代码未定义,因此您需要使用相同的或与您在其他页面使用的类似方法。

<?php
if (isset($_COOKIE["Email"])){
echo "Welcome " . $_COOKIE["Email"] . "!<br>";
echo '<a href="logoutForm.php">logout</a>';
}
else{
$now = time(); // Checking the time now when home page starts.
if ($now > $expire) { // $expire is undefined
session_start(); // <= required
session_destroy(); // <= does not work on its own
header("location: log_in.php");
}
}

关于php - 使用 cookie 时注销,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25166006/

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