I need to run a cron job for my application written in codeigniter.
My main issue is that my codeigniter application uses a hook that checks if there is a valid session, and is $_SESSION['user_id'] is set.
Running my application via terminal it will not work because of the hook.
Does anyone have a solution for this?
Sessions are created for browsers, they won't exist in CLI. You can use:
$this->input->is_cli_request()
which is really just
if (php_sapi_name() == "cli")
to check for CLI, and handle things accordingly. If you needed to simulate running as a user, you could use environment variables that exist in the terminal (how to do so depends on OS), or pass in arguments that would be accepted only by CLI, but this may complicate other code that uses segments.
I have ran into the same issue running Cron jobs.
I set up a unique key which bypasses the need for the Session login
Then I pass it via GET.
So I would have if($this->input->get('auth') == '64 alphanumeric character string')
For added security you could even have two fields. an auth and a confirm for instance. This mimics the way services like twilio work for their API keys.