我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