Codeigniter is_cli_request()停止cpanel cron作业 [英] Codeigniter is_cli_request() stops cpanel cron job from working

查看:50
本文介绍了Codeigniter is_cli_request()停止cpanel cron作业的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

On my website I use cron job on cpanel.

我在控制器的构造区域中具有以下代码,但它停止了cpanel cron作业的工作。 / p>

I have this code below in the construct area of controller but it stops the cpanel cron job from working.

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




问题我需要上面的代码。如果我使用cpanel cron工作?我只是想使其更加安全。

Question Do I need the code above. if I use my cpanel cron job? I just want to make it more secure.



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

                }
            }
        }
    }
}


推荐答案

要防止从网页直接访问:

To prevent direct access from a webpage:

您需要添加此行

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

使用CI 3.0,您可以

with CI 3.0 you can


通过
检查 is_cli()的返回值,使您的cron-jobs无法访问URL。

Make your cron-jobs inaccessible from being loaded in the URL by checking the return value of is_cli().

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

is_cli() returns TRUE if the application is run through the command line and FALSE if not.

根据我的评论,cpanel cron工作模式为:

as by my comment, the cpanel cron job pattern is:


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

/usr/bin/php /var/www/website/public_html/cli.php controller method

请参阅文档此处

相关帖子

这篇关于Codeigniter is_cli_request()停止cpanel cron作业的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆