【问题标题】:Codeigniter is_cli_request() stops cpanel cron job from workingCodeigniter is_cli_request() 停止 cpanel cron 作业的工作
【发布时间】:2017-10-24 12:19:58
【问题描述】:

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

我在控制器的构造区域中有以下代码,但它阻止了 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("\r\n");

                    $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();

                }
            }
        }
    }
}

【问题讨论】:

    标签: codeigniter


    【解决方案1】:

    要防止从网页直接访问:

    你需要添加这一行

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

    使用 CI 3.0,您可以

    使您的 cron-jobs 在 URL 中加载时无法访问 检查is_cli() 的返回值。

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

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

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

    参见文档here

    相关post

    【讨论】:

    • 在构造区域的顶行'
    • 仍然是同样的问题使脚本无法运行。
    • 用这一行替换你的 if 子句
    • 查看我之前对你的主题的回答,也许你会在那里找到帮助:stackoverflow.com/a/36190095/2275490
    • 我刚刚更新了**** php-cli /home/myusername/public_html/subdomain/index.php Cron message的路径是这样的
    【解决方案2】:

    现在解决了。 CodeIgniter Cron Job through Cpanel

    这似乎是我使用的路径的问题。在我之前在 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
    

    现在一切正常

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-05-28
      • 2013-03-11
      • 2015-01-21
      • 1970-01-01
      • 1970-01-01
      • 2017-11-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多