2011-02-15 113 views
1

很久以前,我已经发布了一个问题,但我仍然无法找到答案。基本上,当用户登录账户并且一段时间不活动并且他们返回时,他们会点击一些内容,然后系统将他们注销,他们将不得不重新登录。它可以在90%的时间内工作,但有时会出现如下错误:此页面将以永不完整的方式重定向。重复出现重定向错误

但是,当用户清除cookie时,它工作正常,有时关闭标签并打开一个新的。

下面的代码:

<?php 
$SUBDOMAIN = mysql_real_escape_string($_GET['p_name']); 
$pname = mysql_real_escape_string($_GET['p_name']); 
echo "$p_name"; 
include("db.php"); 
?> 

<?php 
session_start(); 

// Process the POST variables 
$username = $_SESSION["user_name"]; 
//$password = $_POST["password"]; 

// Set up the session variables 
$_SESSION["user_name"] = $username; 

$ugData = $_REQUEST['p_name']; 

if($_POST) 
{ 
    $_SESSION['user_name']=$_POST["user_name"]; 
    $_SESSION['password']=$_POST["password"]; 
} 

$secret = $info['password']; 

//Checks if there is a login cookie 
if(isset($_COOKIE['ID_my_site'])) 
//if there is, it logs you in and directes you to the members page 
{ 
    $username = $_COOKIE['ID_my_site']; 
    $pass = $_COOKIE['Key_my_site']; 
    $check = mysql_query("SELECT user_name, password FROM accounts WHERE user_name = '$username' and p_name='$ugData'")or die(mysql_error()); 
    while($info = mysql_fetch_array($check)) 
    { 
     if (@ $info['password'] != $pass) 
     { 
     } 
     else 
     { 
     header("Location: home.php"); 
     } 
    } 
} 

//if the login form is submitted 
if (isset($_POST['submit'])) 
{ 
    // if form has been submitted 
    // makes sure they filled it in 
    if(!$_POST['user_name'] | !$_POST['password']) 
    { 
     die('You did not fill in a required field.'); 
    } 
    //checks it against the database 

    if (!get_magic_quotes_gpc()) 
    { 
     $_POST['user_name'] = addslashes($_POST['user_name']); 
    } 

    $check = mysql_query("SELECT user_name,password FROM accounts WHERE user_name = '".$_POST['user_name']."' and p_name='".$ugData."'")or die(mysql_error()); 

    //Gives error if user dosen't exist 
    $check2 = mysql_num_rows($check); 

    if ($check2 == 0) 
    { 
     die('That user does not exist in our database. <a href=add.php>Click Here to Register</a>'); 
    } 

    while($info = mysql_fetch_array($check))  
    { 
     $_POST['password'] = md5($_POST['password']); 
     $_POST['password'] = $_POST['password']; 

     //gives error if the password is wrong 
     if (@ $_POST['password'] != $info['password']) 
     { 
     die('Incorrect password, please try again'); 
     } 
     else 
     { 
     // if login is ok then we add a cookie 
     $_POST['user_name'] = stripslashes($_POST['user_name']); 
     $hour = time() + 3600; 
     setcookie(ID_my_site, $_POST['user_name'], $hour); 
     setcookie(Key_my_site, $_POST['password'], $hour); 

     //then redirect them to the members area 
     header("Location: home.php"); 
     } 
    } 

} 
else 
{  
    // if they are not logged in 
?> 
    </table> 
    </form> 
<?php 
} 

?> 
+2

你的代码格式很糟糕。用一致的缩进修复它,并且如果你想让别人真的看到它,摆脱所有多余的空白行。 – 2011-02-15 23:45:21

+1

第一个问题...你的代码被格式化,使得人们不想阅读它来发现问题。删除不必要的部分并修复空白以便于帮助您。 – Kekoa 2011-02-15 23:46:41

回答

1

嘿,你的代码格式实在太差没有阅读的乐趣,你可能想解决这个问题。 :)

我只是快速看过它,错误发生率只有90%或有时难以捕捉。

我看到你在使用header("Location: home.php");而没有任何exit;最后,除非你打算这么做,这通常是一个坏主意。

函数调用header("Location: home.php");不会停止处理脚本。用户可能会得到标题并重定向并停止处理代码(取决于某些php设置),但也许有些cookie会在用户重定向之前设置。所以尝试添加一个退出;在您的重定向标头调用之后。

格式你的代码

1

我敢打赌猜测,这有由于您的会话cookie的不同到期时间以及到期设置时间您ID_my_siteKey_my_site饼干。如果没有被覆盖,默认的会话超时是30分钟(在设置中表示为秒 - 所以是1800)。您的Cookie设置为在一小时后过期。因此,您可能发现自己处于会话过期的情况,但其他Cookie仍然存在。根据您检查内容的顺序/方式,然后重定向,如果用户闲置超过30分钟但少于1小时,则会遇到这种情况。

由于您在此代码示例中执行的唯一重定向是home.php之一,因此在该文件中发生某种检查,即在永无止境的重定向螺旋上发送它们。

另外,该代码示例确实非常混乱。例如,你经常分配和重新分配$username变量(以及看似不同类型的东西 - 虽然我不知道没有看到实际的输入),所以难怪你有神秘的问题。这几行,例如是多余的:

// Process the POST variables 
$username = $_SESSION["user_name"]; 
//$password = $_POST["password"]; 

// Set up the session variables 
$_SESSION["user_name"] = $username; 

你指定从会话$username,并立即指派回来。

从文件的开头:

$SUBDOMAIN = mysql_real_escape_string($_GET['p_name']); 
$pname = mysql_real_escape_string($_GET['p_name']); 

这两个变量分配相同的$_GET值,但它不会出现$SUBDOMAIN被使用过。

而从文件的末尾你两次分配相同的值:

$_POST['password'] = md5($_POST['password']); 
$_POST['password'] = $_POST['password']; 

我真的会鼓励你从你的代码后退一步,看看你的输入,并找出你需要完成的任务并完全重构或重写此代码。有了像这样的东西在它周围浮动,难怪你的系统中有神秘的bug。

1

此外,HTTP位置标头要求U​​RL是绝对的。你应该使用这样的东西:

$ currentServerHost = $ _SERVER ['HTTP_HOST']; $ currentBaseURI = $ currentServerHost。 rtrim(dirname($ _ SERVER ['PHP_SELF']),'/ \');

header('Location:'。'http://'。$ finalURI。'/home.php'); 退出;