Codeigniter is_cli_request() 阻止 cpanel cron 作业工作



在我的网站上,我在cpanel上使用cron job。

我在控制器的构造区域中有下面的代码,但它阻止了 cpanel cron 作业的工作。

if (!$this->input->is_cli_request()) {
    show_error('Direct access is not allowed');
}

问题 我需要上面的代码吗? 如果我使用我的 cpanel cron 作业?我只是想让它更安全。

<?php
class Cron extends CI_Controller {
    public function __construct() {
        parent::__construct();
        if (!$this->input->is_cli_request()) {
            show_error('Direct access is not allowed');
        }
        $this->load->library('email');
        $this->load->model('members_model');
    }
    public function message()
    {
        $admin_email = $this->config->item('email_host');
        $admin_email_pass = $this->config->item('email_password');
        $companyname = 'Riwaka';
        $config = array(
            'protocol' => 'smtp',
            'smtp_host' => 'ssl://mail.yourdomain.co.nz',
            'smtp_port' => 465,
            'smtp_user' => $admin_email,
            'smtp_pass' => $admin_email_pass,
            'mailtype'  => 'html', 
            'charset'   => 'iso-8859-1'
        );
        $this->email->initialize($config);
        $members = $this->members_model->get_approved_members_for_cron_job();
        if ($members) {
            foreach ($members as $member) {
                if ($member['approved'] == '1' && $member['approved_email_sent'] == '0')
                {
                    $this->email->set_newline("rn");
                    $this->email->clear();
                    $this->email->from($admin_email, 'Admin');
                    $this->email->to($member['email']);
                    $this->email->subject($companyname .' Account Approved');
                    $this->email->message('test');
                    $update = array(
                        'approved_email_sent' => '1',
                    );
                    $this->members_model->update_approve_email_send($member['email'], $update);
                    $this->email->send();
                }
            }
        }
    }
}

要阻止从网页直接访问,请执行以下操作:

您需要添加此行

/* deny direct call from web browser */
if (isset($_SERVER['REMOTE_ADDR'])) die('Permission denied.');

使用 CI 3.0,您可以

使您的 cron 作业无法通过加载到 URL 中 检查 is_cli() 的返回值。

is_cli()如果应用程序通过命令行运行,则返回 TRUE,如果不是,则返回 FALSE。

根据我的评论,cpanel cron 作业模式是:

/

usr/bin/php/var/www/website/public_html/cli.php 控制器方法

在此处查看文档

相关文章

现已解决。CodeIgniter Cron Job through Cpanel

这似乎是我使用的路径的问题。在我之前在面板上

php --silent http://mysubdomain.mydomain.co.nz/cron/message

如果我想使用此代码来阻止访问,则无法正常工作

if (!$this->input->is_cli_request()) {
   show_error('Direct access is not allowed');
}

所以现在我改成了

php-cli /home/myusername/public_html/mysubdomain/index.php Cron message

现在一切正常

最新更新