可能重复:
PHP 中的“警告:标头已发送”
我 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");
}
}
}
?>
连接.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);
}
}
?>
如果有人愿意花一些时间来找出我的错误,我会很感激!
解决方案Baba 建议使用 "ini_set("display_errors","On");" 这神奇地解决了这个问题......我想知道为什么大家都喜欢!:-)