2011-05-31 60 views
1

我的服務器運行在PHP 5.2.9,當我使用刷新和標題函數它無法正常工作。這裏是我的代碼PHP標題和刷新不工作

header("location: index.php"); 

header("Refresh: 0;"); 

以前我在不同的服務器上工作,它工作正常。我怎麼解決這個問題?

這是我的完整代碼

if($_POST['sign_in']) 
{ 
    $email = $_POST['email']; 
    $password = $_POST['password']; 
    $sql = "SELECT * FROM tbl_members WHERE m_email='$email' and m_password='$password'"; 
    $res = mysql_query($sql); 
    $count = mysql_num_rows($res); 
    if($count==1) 
    { 
     session_register("email"); 
     header("location: index.php"); 
    } 
    else 
    { 
     $sql = "SELECT * FROM tbl_temp_members WHERE email='$email'"; 
     $res = mysql_query($sql); 
     $count = mysql_num_rows($res); 
     if($count==1) 
     { 
      echo "Login unsuccessful,Account still not activated"; 
     } 
     else 
     { 
      echo "Login unsccessful"; 
     } 
    } 
} 
+3

在調用'header'之前你有輸出嗎?你有錯誤報告激活?你能檢查錯誤(警告)嗎? – 2011-05-31 11:11:07

+1

SQL注入FTW。 :)不要介意重定向。首先解決它。 – 2011-05-31 11:30:38

+0

我附上了完整的代碼問題。 – 2011-05-31 11:30:41

回答

5

位置和刷新都require an absolute URI(和它的「位置」,而不是「位置」)。

試試這個:

header('Location: http://absolute.uri/file.ext'); 

如果沒有幫助,請檢查my answer for any "strange" PHP problem;)

+0

雖然你是對的語法正確,我懷疑這是問題。除非他使用一個非常模糊的瀏覽器。 – 2011-05-31 11:15:31

+3

這是錯誤的。這不是絕對的必要。 – RRStoyanov 2011-05-31 11:15:55

+0

@RRStoyanov「請引用!」 :) http://tools.ietf.org/html/rfc2616#section-14.30我的消息來源說** **確實需要是絕對的。 – 2011-05-31 11:18:26

1

(1)你不需要刷新頭,如果你有位置的一個

(2)最後加exit;

第二個特例是 「位置:」標題。它不僅 將此標題發送回瀏覽器 ,而且它還向瀏覽器返回REDIRECT(302) 狀態代碼,除非已設置 201或3xx狀態代碼已經設置了 。

<?php 
header("Location: http://www.example.com/"); /* Redirect browser */ 

/* Make sure that code below does not get executed when we redirect. */ 
exit; 
?> 
+0

我不知道什麼是錯的。這在新服務器中不起作用。 :-( – 2011-05-31 11:21:26

+0

再次,雖然你提供了有用的建議,但它並沒有幫助解決他的問題。 – 2011-05-31 11:22:31

+0

@Alin我這次只是指向文檔,這樣他就可以有一些看法。當他沒有提供'header'之前的代碼 – RRStoyanov 2011-05-31 11:31:37

2

取決於你正在嘗試做的。你想要重定向嗎?如果是這樣的話,你可以簡單地使用header('Location: http://www.example.com/ ');,但如果你想有一個一定的時間之後刷新您可以使用:

header("refresh:5;url=wherever.php"); 
echo 'You\'ll be redirected in about 5 secs. '; 
echo 'If not, click <a href="wherever.php">here</a>.'; 

從那裏得到的示例代碼 - http://php.net/manual/en/function.header.php - 也許值得一讀得。