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等。几乎似乎被抓住了在这里循环,我没有太多经验与这些类型的循环。
当用户点击退出按钮时,我希望他们从站点注销并返回登录页面,以便他们可以再次登录该站点。任何建议或您是否看到我的代码有任何问题?
Logout.php:
<?php
session_start();
unset($_SESSION["STYLE"]);
header("Location: SheetTime.php");
?>
SheetTime.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>";
?>
的login.php
session_start();
$_SESSION['STYLE'] = phpCAS::getUser();
//$_SESSION['STYLE'] = $row['STYLE'];
$URL="http://www.websitetotestfortest.edu/theTimePro/SheetTime.php";
header ("Location: $URL");
?>
答案 0 :(得分:1)
<强> 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.
<强>的login.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>
修改强> 此外,我没有看到您使用会话变量的值声明$ user_name的任何原因。在我的示例中,您可以完美地验证会话变量中的信息,而无需将其声明为另一个变量。如果$ _SESSION [&#39; style&#39;]令人困惑(是的,它是!! :))那么也许$ _SESSION [&#39; user_name&#39;]会更有意义。
<强> EDIT2 强> 我在登录时添加了数据库验证。有关详细信息,请查看PDO http://www.php.net/manual/en/book.pdo.php
至于注销..我假设所有这些文件都位于同一台服务器上,并且确实确保$ _SESSION [&#39; style&#39;]以相同的方式写入。小心帽。
当输入文件时,会话应取消设置。请尝试添加session_destroy();低于未设置以确保。
添加注销链接只是:
<a href="the-path-to-logout.php">Logout</a>
然后注销将终止活动会话并将您重定向到login.php。确保所有路径都正确。
答案 1 :(得分:0)
首先要进行真正的注销,我建议执行this然后重定向。它将杀死会话。你的会话应该像这样测试:
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();
session_unset();
header("Location: index.php");
?>
更新了Spreadsheet.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>";
}
?>
希望这有帮助!
答案 2 :(得分:0)
entiendoNull得到了重点:Login.php包含SheetTime.php的标头 您需要在此位置标头之前添加条件,以便仅在用户被记录时重定向。
答案 3 :(得分:0)
我想你在这段代码之前有一张桌子
<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>
顺便说一句,如果是网址,为什么你不使用<a href="logout.php"></a>