0

I have setup cronjob in codeigniter. Cron job is working fine but i wanna restrict direct access to cron url.

I have tried below code but it was not working.

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

Thanks in advance.

krutssss
  • 934
  • 1
  • 10
  • 23

1 Answers1

0

You don't include/require the framework like this for cron jobs.

Here's what you should be doing

Imaging we have a cron controller

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}";
    }
}

Now to run this through cron, You'll have to do something like this:

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

For the example given above it should look like this

0 */2 * * * /usr/local/bin/php /path/to/index.php cron process_something p1 p2
ahmad
  • 2,709
  • 1
  • 23
  • 27