2012-11-10 45 views
-1

可能重複:
「Warning: Headers already sent」 in PHPPHP,OOP:頭已經發出已

我100%肯定,我不會在輸出這個腳本設置標頭之前的任何文字,但我得到這個錯誤。

warning: Cannot modify header information - headers already sent by (output started at /home/x/public_html/index.php:4) in /home/x/public_html/core/functions.php on line 25 

頂部的index.php

<?php 
require_once "core/functions.php"; 
$actions = new Actions(); 
$actions->isUserLogged($_SERVER['REMOTE_ADDR'], $_COOKIE['cData']); 
?> 
<!DOCTYPE html>... 

核心/ functions.php中

<?php 

include_once('connection.php'); 

class Actions { 

private $db; 

public function __construct() { 

    $this->db = new Connection(); 
    $this->db = $this->db->dbConnect(mysql info); 

} 

public function isUserLogged($currentIP, $cData) { 
    if(!empty($cData)){ //if cookie is not empty 
     $getData = $this->db->query("SELECT loggedIP FROM registery WHERE session='".$cData."'")->fetchColumn(); //finding entry in registery. 
     $loggedIP = $getData['loggedIP']; 
     if($currentIP == $loggedIP) {return true;} //if that loggedIP and currentIP matces -> proceed. 
     else { return setcookie('cData','',time()-31536000); //this is where the error points. 
      return header("location: login.php");} // else unset cookie and redirect to login. 

    } else {  
     return header("location: login.php"); 
    } 


    } 
} 
?> 

connection.php

<?php 
error_reporting(E_ALL); 

class Connection { 

public function dbConnect($server, $username, $password, $db){ 

    return new PDO('mysql:host='.$server.';dbname='.$db, $username, $password); 

} 

} 


?> 

如果有人會這麼好心採取一些時間來找我的錯誤,我會appreicate它!

SOLUTION Baba建議使用「ini_set(」display_errors「,」On「);」並且神奇地解決了這個問題......我想知道爲什麼要這麼做! :-)

+1

您是否有任何錯誤顯示?嘗試評論顯示錯誤行。 – Cups

+1

請記住,任何輸出都會觸發此錯誤。這包括空格(空格,CRLF等)和字節順序標記(確保如果您的文件使用Unicode編碼,則它沒有字節順序標記)。 – Ynhockey

+0

我想你打印你的html代碼後使用session_start,cookie_set或header *。當你打印html代碼時,你發送一個標題爲「200 OK,內容類型爲text/html」的頭文件,並且用這些函數修改這個頭文件,這是不可能的。 – artragis

回答

1

不知道,但你是在connection.php

處理空白見

<?php 
^--------------------------- Most likely the cause 
error_reporting(E_ALL); 

你也應該儘量顯示情況下的PHP錯誤它被關掉

error_reporting(E_ALL); 
ini_set("display_errors","On"); 
+0

不,我不這麼認爲,這可能只是Stackoverflow與結構混雜。 –

+0

看看你的行號,確保新行在'<?php'和'?>'標記之前或之後......我確信這是問題 – Baba

+0

字節順序標記? –

0

這可能是UTF-8 byte order mark。如果是這種情況,請將編碼更改爲「無BOM的UTF-8」。

編輯: 怎麼這裏的空間(connection.php,最後一行):

?> 
    --^------- there 
+0

不幸的是,這並沒有工作 –

+0

好吧,這個怎麼樣? (編輯) – cypher

+0

最後沒有空格,我也試圖刪除結束標誌也沒有幫助。 –

0

這有時是相當有問題。我有幾次,並決定使用JavaScript代替。例如:

echo '<script type="text/javascript">window.location ="login.php";</script>'; 

也許這也適用於你,雖然它不是你正在尋找的解決方案。

希望這會有所幫助。