gpt4 book ai didi

php - 注销按钮将用户重定向到同一页面,而不是将用户注销

转载 作者:行者123 更新时间:2023-11-29 03:00:49 25 4
gpt4 key购买 nike

SheetTime.php 是主页。当用户转到 SheetTime.php 时,他们将被重定向到 Login.php;如果他们在那里成功登录,他们将被定向到 SheetTime.php 的实际内容。

问题出在我的注销按钮上。当用户单击注销按钮时,他们总是被重定向到 SheetTime.php(页面似乎只是刷新),而不是实际注销并返回到 Login.php。我试图将 Logout.php 中的位置更改为 Login.php 并检查了 URL,但我得到了相同的结果。

我对为什么会出现此问题的理论是,因为我将 Logout.php 中的位置设置为 SheetTime.php,它会返回到 Login.php,然后返回到 SheetTime.php 等。几乎似乎被捕获了在这里循环,我对这些循环没有太多经验。

当用户单击注销按钮时,我希望他们退出站点并返回登录页面,以便他们可以再次登录该站点。有什么建议或者您看到我的代码有什么问题吗?


注销.php:

<?php
session_start();
unset($_SESSION["STYLE"]);
header("Location: SheetTime.php");
?>

工作表时间.php:

<?php  
session_start();
$user_name = $_SESSION['STYLE'];

//----------- Check if any user is currently logged in ---------------
if (!(isset($user_name) && $user_name!= '')) {
header("Location: http://www.websitetotestfortest.edu/theTimePro/Login.php");
}else{

// (***there is other code in between***)

//------------- Logout Button (Terminates session) ------------------
echo "<br /><form action='Logout.php' method='post'>
<input value='Log out' id='Submit' type='submit' /></td>
</form>";
?>

登录.php

session_start();
$_SESSION['STYLE'] = phpCAS::getUser();
//$_SESSION['STYLE'] = $row['STYLE'];
$URL="http://www.websitetotestfortest.edu/theTimePro/SheetTime.php";
header ("Location: $URL");
?>

最佳答案

Logout.php

<?php
session_start();
unset($_SESSION['style']); //unset will work fine for a specifik session variable. If you want to get rid of all **your** active sessions for your domain use session_destroy();
session_destroy();
header("Location: Login.php");
exit;
?>

SheetTime.php

<?php  
session_start();
if(!isset($_SESSION['style']) || $_SESSION['style'] == '')){
header("Location: http://www.websitetotestfortest.edu/theTimePro/Login.php");
exit;
}

//There's a valid session, so do things here.

登录.php

<?php
session_start();
if(isset($_POST['doLogin'])){
$db = new PDO("..."); //your database details
$statement = $db->prepare("SELECT id FROM your_user_table WHERE username = :username AND password = :password");

$statement->execute(array(':username' => $_POST['userName'], ':password' => $_POST['passwd']));
$count = $statement->rowCount();

if($count <= 0){
header("Location: login.php?error"); //go here if there's an error... receive it with if(isset($_GET['error'])){ echo 'your text';}
exit;
}

$row = $statement->fetch(); // if a result was found, fetchit
$_SESSION['style'] = 'yourValue'; // define the session variable
header("Location: Path-To-SheetTime.php");
exit;
}
?>

<form method="post" action="login.php">
<!-- your login form -->
<input type="text" name="userName"/>
<br/>
<input type="password" name="passwd"/>
<br/>
<input type="submit" name="doLogin" value="Login"/>
</form>

编辑此外,我看不出你用 session 变量的值声明 $user_name 的任何原因。在我的示例中,您可以完美地验证 session 变量中的信息,而无需将其声明为另一个变量。如果 $_SESSION['style'] 令人困惑(是的,确实如此!!:))那么 $_SESSION['user_name'] 可能更有意义。

编辑2我在登录中添加了数据库验证。在此处查看 PDO 以获取更多信息 http://www.php.net/manual/en/book.pdo.php

至于注销.. 我假设所有这些文件都位于同一台服务器上,并且您确实确保 $_SESSION['style'] 以相同的方式编写。小心帽子。

进入文件时,应该取消设置 session 。请尝试添加 session_destroy();低于 unset 以确保。

添加注销链接很简单:

 <a href="the-path-to-logout.php">Logout</a>

然后注销将终止事件 session 并将您重定向到 login.php。确保所有路径都正确。

关于php - 注销按钮将用户重定向到同一页面,而不是将用户注销,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24018365/

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