2013-04-16 167 views
0

我遇到了PHP和会话问题。这是我的dictionary.php代码。PHP会话问题

<?php 
if(!isset($_SESSION['auth'])){ 
$_SESSION['auth'] = 0; 
} 
if($_SESSION['auth'] == 0){ 
header("Location: index.php"); 
} 
?> 

因此,有这应该会话“身份验证”设置为0或1的index.php:

<?php 
    $msg = ""; 

//Password handler 
if(!isset($_POST['password'])){ 
    $password = ""; 
}else{ 
    $password = $_POST['password']; 
} 

if(!isset($_SESSION['auth'])){ 
    $_SESSION['auth'] = 0; 
} 

//Username handler 
if(!isset($_POST['username'])){ 
    $username = ""; 
}else{ 
     $username = $_POST['username']; 
    if($username == "potato" && $password == "poteto"){ 
     $_SESSION['auth'] = 1; 
     header("Location: dictionary.php"); 
    }else{ 
     $msg = "<br /> 
<font color=\"#FF0000\">Invalid username or password!</font><br />"; 
    } 
} 

if($_SESSION['auth'] == 0){ 
     header("Location: dictionary.php"); 
} 

?> 

(以上可能是不好的做法或东西,但我只是为了一个学校项目,我不希望其他人看到文件dictionary.php。)

index.php使用POST方法的形式发送$username$password,以防万一你没有'尚未注意到。

当我输入登录信息“马铃薯”和“poteto”时,它将我重定向到dictionary.php,但立即将我引回index.php。我试过调整上面的代码,但没有运气。

如果有人需要我向他们展示此信息,那么我可以提供一个URL。

谢谢。

+3

我在任何地方都看不到'session_start()'。首先解决它。阅读:http://php.net/manual/en/session.examples.basic.php –

+0

“错误310(net :: ERR_TOO_MANY_REDIRECTS):有太多的重定向。” – Raymonf

+0

@RBLXDev:你已经创建了一个重定向循环。这些对于排除故障来说不是那么简单,但是它有什么用处,现在你需要麻烦地拍摄它。 – hakre

回答

2

正如在评论中提到的,设置session_start()。此外...

在最终的index.php您重定向到dictionary.php:

if($_SESSION['auth'] == 0){ 
     header("Location: dictionary.php"); 
} 

在dictionary.php为相同的条件下,你重定向到的index.php:

if($_SESSION['auth'] == 0){ 
    header("Location: index.php"); 
} 

因此,无论何时您的用户处于01​​的情况下,都会导致无限重定向。你需要清理它。

+0

谢谢!那是那个问题?我一直在重定向两次? – Raymonf

0

你应该让你的重定向功能多一点舒适:

​​3210

这不仅将提供更好的重定向响应消息,它也可以让你(用于调试)禁用header(...);线(例如,通过评论它),所以你的浏览器不再进入重定向循环。

只是

site_redirect( “dictionary.php”)取代像

header("Location: dictionary.php"); 

的电话;

,这样只有一个header()呼叫你所有的脚本不再,这是site_redirect()功能,因此您可以在一个中心位置影响重定向。