php - Cron PHP 文件 - header 已发送

标签 php linux apache cron cpanel

所以我知道已发送警告的 header 已被讨论过多次,但这个错误有点不同。

所以首先这是我收到的错误:

[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 运行(这是我的客户试图做的,每分钟一次),那么它会抛出上述有关已发送 header 的错误。

通过 cron 运行它与通过浏览器运行它有什么不同的原因吗?

最佳答案

已通过从 Cron 中删除 -q 标志来解决。

关于php - Cron PHP 文件 - header 已发送,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27293457/

相关文章:

apache - 重定向来自特定域的所有请求

php - MySQL 三表连接——用户和好友图片

PHP 关闭 MySQL 连接

php - JSON 转换 URL 错误

c - GCC 不再实现 <varargs.h>

c - 使用 ncurses 调整终端大小和滚动问题

javascript - 在最后一页(或他来自的页面)重定向用户 PHP/JS

c++ - 链接到 Boost 正则表达式库时程序启动期间崩溃

mysql - "fatal error: mysql.h: No such file or directory"编译mod_auth_mysql-3.0.0

perl - 在 OSX Lion Apache 服务器上设置 mod_perl