Php 注销按钮将uesr重定向到同一页面,而不是注销用户
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 注销按钮将uesr重定向到同一页面,而不是注销用户,php,mysql,.htaccess,redirect,login,Php,Mysql,.htaccess,Redirect,Login,php是主页面。当用户转到SheetTime.php时,他们被重定向到Login.php;如果他们在那里登录成功,他们将被引导到SheetTime.php的实际内容 问题在于我的注销按钮。当用户单击注销按钮时,他们总是被重定向到SheetTime.php(页面看起来只是刷新),而不是实际注销并返回Login.php。我尝试将Logout.php中的位置更改为Login.php并检查了URL,但得到了相同的结果 我的理论是,由于我将Logout.php中的位置设置为SheetTime.php,它会
Logout.php:
<?php
session_start();
unset($_SESSION["STYLE"]);
header("Location: SheetTime.php");
?>
<?php
session_start();
session_unset();
header("Location: index.php");
?>
首先,对于真正的注销,我建议执行然后重定向。它将终止会话。您的会话应进行如下测试:
if(isset($_SESSION["STYLE"]))
{
//Do Coding magic
}
else
{
//Redirect to login page
}
index.php
<?php
//Test for valid login
session_start();
if(isset($_SESSION["STYLE"]))
{
echo "<a href='logout.php'>Logout</a>";
}
else
{
echo "<a href='login.php'>Login</a>";
}
?>
login.php:
<?php
session_start();
$_SESSION["STYLE"]="VALIDLOGIN";
header("Location: index.php");
?>
logout.php:
<?php
session_start();
unset($_SESSION["STYLE"]);
header("Location: SheetTime.php");
?>
<?php
session_start();
session_unset();
header("Location: index.php");
?>
更新的电子表格.php:
<?php
session_start();
//----------- Check if any user is currently logged in ---------------
if (!isset($_SESSION['STYLE']))
{
echo "You are not logged in!<br>";
echo "<a href='login.php'>Logout</a>";
}
else
{
echo "You are logged in!<br>";
echo "<a href='logout.php'>Logout</a>";
}
?>
希望这有帮助 EntiendNull抓住了要点:Login.php包含SheetTime.php的头
您需要在此位置标题之前添加一个条件,以便仅在用户已登录时重定向。我想您在此代码之前有一个表
<br /><form action='Logout.php' method='post'>
<input value='Log out' id='Submit' type='submit' /></td>
</form>
换成这个,
<br /><form action='Logout.php' method='post'>
<input value='Log out' id='Submit' type='submit' />
</form>
</td>
顺便说一下,如果是一个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;
?>
<?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
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>
SheetTime.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;
?>
<?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
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>
好吧。。您将用户发送到Login.php,在Login.php中,您将用户发送到SheetTime.php。另外,请尝试在第1行将if更改为if(!isset($user\u name)&&&$user\u name!='')
这一个缺少a)这不起作用:请参阅我更新的示例…我在本地运行了它。如果您需要更多帮助,请告诉我。您的示例和建议非常有用:)但是我忘了提到一些重要的事情,用户的凭据已(成功)从数据库中提取。现在的做法是什么?我也尝试了你为Logout.php发布的内容,但没有成功。谢谢你的时间