php - 在 CodeIgniter 和 Hostgator 中运行 cron 作业

标签 php codeigniter cron command-line-interface

我正在尝试在 codeigniter 和 hostgator 中运行 follow cron 作业

/usr/bin/php /home/username/public_html/index.php cronjob contact martin

但是没有任何反应,我收到以下邮件:

<h4>A PHP Error was encountered</h4>

<p>Severity: Notice</p>
<p>Message:  Undefined index:  REMOTE_ADDR</p>
<p>Filename: core/Input.php</p>
<p>Line Number: 351</p>

<p>Severity: Warning</p>
<p>Message:  Cannot modify header information - headers already sent by (output started at /home/iglesias/public_html/mysite.com/system/core/Exceptions.php:185)</p>
<p>Filename: libraries/Session.php</p>
<p>Line Number: 675</p>

我的controller如下(简单发邮件测试)

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Cronjob extends CI_Controller {

function __construct()
{
    parent::__construct();
    $this->cron();
}

function cron()
{
    if(!$this->input->is_cli_request())
    {               
        die();
    }
}

function contact($name)
{
    $this->load->library('email');
    $config['mailtype'] = 'html';
    $this->email->initialize($config);

    $this->email->from('test@gmail.com', $name);
    $this->email->to('test2@gmail.com');
    $this->email->subject('test');
    $this->email->message('test sent');             
    $this->email->send();
}
}

我做错了什么吗?我将衷心感谢您的帮助。

当从 URL 调用时,我更改了 .htaccess 以删除 index.php,我不知道这是否与此有关。

谢谢

最佳答案

我的答案是很久以后才出现的,但它可以帮助其他人。 这个错误显然是众所周知的,但只有一个快速修复:http://ellislab.com/forums/viewthread/227672/ 它说当您自动加载 session 库时会发生这种情况,它会调用未初始化的 CI_Input::ip_address() 或类似的东西。 快速修复在输入类中(core/Input.php 第 351 行(CI 2.1.2)):

$this->ip_address = $_SERVER['REMOTE_ADDR'];

成为:

if(isset($_SERVER['REMOTE_ADDR']))
   $this->ip_address = $_SERVER['REMOTE_ADDR'];
else
   $this->ip_address = '0.0.0.0';

希望这对您有所帮助。

关于php - 在 CodeIgniter 和 Hostgator 中运行 cron 作业,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15768231/

相关文章:

php - 如何让 php 在没有 cron 的情况下永远工作?

php - Laravel 框架外的 Artisan::call()

php - 调用未定义的未定义函数?

php - 如何在 codeigniter 查询中使用 FIND_IN_SET?

javascript - 删除 JSON 字符串数组中引号前的转义字符 PHP

javascript - 输入键以提交 Ajax 表单

php - 如果未完成注销,如何存储用户登录持续时间?

shell - 如何限制shell脚本执行时间?

php - 使用媒体查询时如何显示不同尺寸的不同特色图片

PHP 将数组中的重复项与多个字符串数据进行比较