我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「);」並且神奇地解決了這個問題......我想知道爲什麼要這麼做! :-)
您是否有任何錯誤顯示?嘗試評論顯示錯誤行。 – Cups
請記住,任何輸出都會觸發此錯誤。這包括空格(空格,CRLF等)和字節順序標記(確保如果您的文件使用Unicode編碼,則它沒有字節順序標記)。 – Ynhockey
我想你打印你的html代碼後使用session_start,cookie_set或header *。當你打印html代碼時,你發送一個標題爲「200 OK,內容類型爲text/html」的頭文件,並且用這些函數修改這個頭文件,這是不可能的。 – artragis