所以我知道已经多次讨论已发送警告的标题,但这个错误有点不同。
首先,这是我收到的错误:
[04-Dec-2014 11:51:01 UTC] PHP Warning: session_start(): Cannot send session cookie - headers already sent in /home/evaske/public_html/up.php on line 3
[04-Dec-2014 11:51:01 UTC] PHP Warning: session_start(): Cannot send session cache limiter - headers already sent in /home/evaske/public_html/up.php on line 3
以下是导致它的PHP代码:
<?php
if(!isset($_SESSION)){ session_start(); }
function sendSMS($message){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://api.infobip.com/api/v3/sendsms/plain");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "user=MostoSystems&password=Rayidi&sender=MOSTOS&SMSText=".$message."&GSM=917799468478&output=JSON");
$buffer = curl_exec($ch);
curl_close($ch);
}
$url = 'http://googledd.com';
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_NOBODY, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_exec($ch);
$retcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
echo $retcode;
if($retcode == 200) {
if(!empty($_SESSION['downtime'])){
$up_time = time();
$down_time = $_SESSION['downtime'];
$server_down_time = ($up_time-$down_time);
$hours = (($server_down_time/60)/60);
$dt = number_format($hours, 2, '.', '');
$message = "HOST UP: (".gethostbyname('cphone.mosto.in').") ".$dt." HRS: Hello, This is to inform you that your server just came Online!";
//sendSMS($message);
}
}else{
// Host is down, Take down the time
if(empty($_SESSION['downtime'])){
$_SESSION['downtime'] = time();
$message = "HOST DOWN: (".gethostbyname('cphone.mosto.in')."): Hello, This is to inform you that your server was down now!";
// sendSMS($message);
}
}
var_dump($_SESSION);
?>
现在奇怪的是,如果我直接从浏览器运行它,那么它工作正常,并没有输出到error_log。
但是,如果我将它作为cron运行(这是我的客户端尝试执行的操作,每分钟一次),那么它会抛出有关已经发送的标头的上述错误。
通过cron运行它的任何原因都不同于通过浏览器运行它吗?
答案 0 :(得分:7)
通过从Cron中删除-q标志来解决。
答案 1 :(得分:-1)
使用它: -
<?php
if(!isset($_SESSION)){ @session_start(); }
?>