2011-02-15 137 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'); 退出;