拒绝直接访问codeigniter中cronjob的cron文件的权限


denied permission to direct access cron file for cronjob in codeigniter

我在codeigniter中设置了cronjob。Cron作业运行良好,但我想限制直接访问cronurl。

我尝试过下面的代码,但它不起作用。

if (isset($_SERVER['REMOTE_ADDR'])) die('Called from Browser');
$_SERVER['PATH_INFO'] = $_SERVER['REQUEST_URI'] = '/cron/cron_alert'; // Setting the path of controller/method
include(dirname(dirname(__FILE__)).'/index.php'); //Now just call the framework

提前谢谢。

对于cron作业,您不需要包含这样的框架。

以下是你应该做的

成像我们有一个cron控制器

class Cron extends CI_Controller {  
    public function __construct () {
        parent::__construct();
        // All requests to this controller should come through CLI only.
        if( ! $this->input->is_cli_request() ) {
            die("Only CLI Requests Allowed");
        }
    }
    public function process_something ($param1, $param2) {      
        echo "Processing ...";
        sleep(2);
        echo "Successfully Processed {$param1}, {$param2}";
    }
}

现在要通过cron运行这个,你必须做这样的事情:

0 */2 * * * /usr/local/bin/php /path/to/index.php ControllerName Method Param1 Param2

对于上面给出的例子,它应该看起来像这个

0 */2 * * * /usr/local/bin/php /path/to/index.php cron process_something p1 p2