简体   繁体   中英

Run a Cron Job on a CodeIgniter controller that needs me to be logged in? Should I use a public/private function in this kind of situation?

I want to run a URL every day on 1PM which checks if some variables have been changed by running a query on an external API, and if have changed - store the new vars and notifies me via email.

Workspace:

  • I am using CodeIgniter MVC Framework.

  • Ubuntu Server 15.10.

More information of the situation:

  • The controller function cron_job() is set to public, although I'm not sure it's safe and relevant for being public, since it's a special function that only the server has to run.

public function cron_job(){

// checks for vars and sends updated using email.

}

  • The function is written in a controller that allows you to to run it only if you have a logged_in session. The logged_in session is created using the enctypted function that CodeIgniter offers. Which is kind of cool and easy to use. and safe. So if I want to run this I'll have to add a session somehow before running the cron_job() function. (Create a new private function that adds a session and call the cron_job() function? I'm really not sure whats the right way doing this)

something like this?

private function add_session() {

$date = array(

'email' => $email, // ??? 'is_logged_in' => TRUE

);

$this->cron_job();

}

am i even allowed to run a private function from the server?

or can I insert a session variable using the encrypted functionality of CI and do this easily along with the cron job functionality of my ubuntu server?

Don't use web-based concepts for CLI functionalities ... And in general, don't mess with authentication and logged-in states.

What you should do is move that function into a separate controller, that doesn't require you to be "logged in" and put in a check to make sure that it can only be run via CLI, like this:

class Cli_only extends CI_Controller {

    public function __construct()
    {
        parent::__construct();
        is_cli() OR show_404();
    }

}

Also, making a controller method non-public in CI will make it inaccessible from the outside world, so that idea couldn't possibly work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM