2011-11-02 106 views
-1

這是我的代碼頭信息......我想不通爲什麼它口口聲聲說不能修改與MySQL和PHP

Warning: Cannot modify header information - headers already sent by (output started at /opt/apache/htdocs/save/header.php:10) in /opt/apache/htdocs/****/attendance.php on line 41 

Warning: Cannot modify header information - headers already sent by (output started at /opt/apache/htdocs/save/header.php:10) in /opt/apache/htdocs/****/attendance.php on line 42 

我只是想登錄頁面的工作。我也想將$ _SERVER [PHP_SELF()]更改爲實際的url,以便我的CSS等仍然工作。

include('header.php'); 
$server = "serverName"; 
$dUsername = "username"; 
$dPass = "password"; 
$username = "username2"; 
$password = "password2"; 
$randomword = "randomword"; 

if(isset($_COOKIE['MyLoginPage'])) { 
    if($_COOKIE['MyLoginPage'] == md5($password.$randomword)) { 
     $conn = mysql_connect($server, $dUsername, $dPass) or die("error connecting to MySQL database"); 
     mysql_select_db("w3_save", $conn); 
     $query = "SELECT DISTINCT MemberName FROM attendance;"; 
     $result = mysql_query($query, $conn) or die(mysql_error()); 
     echo "<div id='rightColumn'><div id='title'><h1>Attendance</h1></div><div id='content'>"; 
     while($row = mysql_fetch_array($result, MYSQL_ASSOC)) { 
      echo "Name: $row[MemberName] <br />"; 
     } 
     echo "</div></div></body></html>"; 

     mysql_close($conn); 
     exit; 
    } 
    else { 
     echo "<p>Bad Cookie. Please clear them out and try again</p>"; 
     exit; 
    } 
} 

if(isset($_GET['p']) && $_GET['p'] == 'login') { 
    if($_POST['name'] != $username) { 
     echo "<p>Sorry the username you entered is incorrect.</p>"; 
     exit; 
    } 
    else if($_POST['pass'] != $password) { 
     echo "<p>Sorry the password you entered is incorrect.</p>"; 
     exit; 
    } 
    else if($_POST['name'] == $username && $_POST['pass'] == $password) { 
     **setcookie('MyLoginPage', md5($_POST['pass'].$randomword)); LINE 41 
     header("Location: $_SERVER[PHP_SELF]");** LINE 42 
    } else { 
     echo "<p>Sorry you could not be logged in at this time please try again</p>"; 
    } 
} 


<div id='rightColumn'> 
      <div id='title'> 
       <h1>Attendance</h1> 
      </div> 
      <div id='content'> 
       <form action="``<?php echo $_SERVER['PHP_SELF']; ?>?p=login" method='post'> 
       <label>Username: <input type='text' name='name' id='name' /></label><br /> 
       <label>Password: <input type='password' name='pass' id='pass' /></label><br /> 
       <input type='submit' id='submit' value='Login' /> 
       </form> 
      </div> 
     </div> 
    </body> 
</html> 
+7

查看右側「相關」問題的長列表?選一個。 – animuson

+0

看看attendance.php:第41行,錯誤信息應該是自我解釋的。 –

+0

是的,我已經檢查過它之前的回聲,我試過轉移我的代碼,我試過一些隨機的其他東西,並沒有找到我的問題。我知道有相關的問題,但我想更詳細地瞭解我的代碼。 另外我想要一些幫助將$ _SERVER ['PHP_SELF']更改爲我想要的網址。我不確定這是否與將聲明更改爲我想要的一樣簡單。 – eric

回答

3

更多信息正如在評論中指出,該錯誤信息是非常自我解釋。顯然header.php正在啓動輸出。你應該追蹤並找出原因。解決方法是使用輸出緩衝:

ob_start(); 
include('header.php'); 
... 
ob_end_flush(); 
?> 

這會阻止在調用flush函數之前發送任何輸出; Documenation

至於$_SERVER['PHP_SELF']:你不應該編輯superglobals。而在這種情況下,您所處的信息位於不同的索引中:$_SERVER['REQUEST_URI']

+0

美麗的謝謝你sooo很完美!解決了我所有的問題 – eric

1

您可以嘗試轉產42類似:

header("Location: $_SERVER[PHP_SELF]",TRUE,302); 

重定向到一個新的頁面和解析HTML之前使用sendcookies。

我建議使用ob_start和最後階段,如:

ob_start(); 
//your html 
$content = ob_get_contents(); 
ob_end_clean(); 

發現有 http://php.net/manual/pt_BR/function.ob-start.php