在CodeIgniter和Hostgator中运行cron作业

时间:2013-04-02 15:35:54

标签: 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>

我的控制器如下(只需发送电子邮件进行测试)。

<?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,我不知道这是否有事可做。

由于

4 个答案:

答案 0 :(得分:14)

我的回答很长一段时间后,但它可以帮助其他人。 这个错误显然是众所周知的,但只有一个快速解决方法:http://ellislab.com/forums/viewthread/227672/ 它说它发生在你自动加载会话库时,它调用CI_Input :: ip_address(),它没有被初始化或类似的东西。 快速修复在输入类(core / Input.php Line 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';

希望这有帮助。

答案 1 :(得分:0)

你有没有在config / routes.php中设置正确的路由?尝试从浏览器运行脚本(或注释掉执行部分),以确保可以使用当前路径/ htaccess设置访问它。

答案 2 :(得分:0)

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

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

答案 3 :(得分:0)

@mariek的精彩回答

但我们也可以在语法之前使用@符号来完成此操作。

所以刚刚对“core / Input.php”第352行(CI ver.2.2.2)做了一点改动

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

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

及其完成。