Welcome Guest, Not a member yet? Register   Sign In
How to protect a cron controller?
#1

[eluser]Jakobud[/eluser]
I need to make a controller that is run once a day using cron. The controller accessed the database and does some stuff. What is the best way to protect this controller so that it is inaccessible from the browser?

My approach so far is creating a separate cron.php outside the CI directory, then using .htaccess to make it inaccessible from the browser. cron.php then call the cron controller (http://mysite.com/cron/), but then the controller is still accessible from the browser if I type in the address.

What is the best approach?
#2

[eluser]Eric Barnes[/eluser]
There are tons of ways of doing this. One that I am thinking of is keeping it public and doing something like:
Code:
public function cron($key = '')
{
    if ($key != 'mysupersecretkey')
    {
        redirect('home');
    }
    
    // do my cron stuff
}

Then call it via yoursite.com/index.php/cron/mysupersecretkey
#3

[eluser]Jakobud[/eluser]
Excellent approach! That's what I will do. Thanks.




Theme © iAndrew 2016 - Forum software by © MyBB