在CodeIgniter和Hostgator中运行cron job


Run cron job in CodeIgniter and Hostgator

我正在尝试在codeigniter和hostgator中运行以下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>

我的控制器如下(简单地发送一个电子邮件来测试)。

<?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();
}
}

我做错了什么吗?我将非常感谢你的帮助。

我改变了。htaccess从URL调用时删除index.php,我不知道这是否有什么关系。

谢谢

我的答案来得迟了很长时间,但它可以帮助其他人。这个错误显然是众所周知的,但只有一个快速修复它:http://ellislab.com/forums/viewthread/227672/它说它发生在你自动加载会话库时,它调用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';

是否在config/routes.php中设置了正确的路由?试着在浏览器中运行这个脚本(或者注释掉执行部分),以确保它可以在你当前的route/htaccess设置下访问。

检查Hostgator是否将Apache运行为Fast-CGI而不是模块。如果是这种情况,PHP的运行路径可能与您使用的不同。

如果您检查phpinfo,您可能会看到相关信息。

@mariek的好答案

但是我们也可以通过在语法前使用@符号来做到这一点。

所以刚刚在"core/Input.php"行#352 (CI版本)做了一点改变。2.2.2)

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

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