0

I am using CI hooks to prevent access to application without logging in, But i am also using cron jobs to do some schedule task, now i don't need login to schedule task,

below is my code for controller used for hook,

public function checkSession()
{
    if($this->CI->router->fetch_class() == "cron")
    {
         //How should i continue with Cron class??
    }
    elseif($this->CI->router->fetch_class() != "login")
    {
         // session check logic here...change this accordingly
         if($this->CI->session->userdata['user_id'] == '' )
         {
             redirect('login');
         }
    }
}

Now i want cron class to run without checking user session and login,

How can i continue to cron class in If condition?

Or any other thoughts?

Thanks,

Yazid Erman
  • 1,166
  • 1
  • 13
  • 24
rjcode
  • 1,193
  • 1
  • 25
  • 56
  • What is being executed to trigger the cronjob? You have a cronjob defined in your crontab on the server? – Clay Dec 30 '15 at 05:28
  • @Clayton, Yes its on server, wget http:/my/domain.com/cron – rjcode Dec 30 '15 at 05:30
  • than just call the function of cron class self::index(); – devpro Dec 30 '15 at 05:40
  • Try [this](http://forum.codeigniter.com/archive/index.php?thread-33378.html) or [this](http://stackoverflow.com/questions/29296015/how-to-load-hook-for-particular-controller). [Google](https://www.google.ba/search?q=codeigniter+hooks+exclude+some+controller&oq=codeigniter+hooks+exclude+some+controller&aqs=chrome..69i57.34175j0j1&sourceid=chrome&es_sm=93&ie=UTF-8) gave those solutions. – Tpojka Dec 31 '15 at 15:55

2 Answers2

0

The Secure behavior is to login even if you are running the script using a cronjob, and this is doable using cURL for example, where you can use your credentials to create a session, login normally and execute the scripts according to the privileges you have.

You may refer to the following thread for more details:
Using curl with a username and password

Community
  • 1
  • 1
Yazid Erman
  • 1,166
  • 1
  • 13
  • 24
0

Other options if you insist on keeping things simple and security is not a big issue for you, You may either pass a parameter when you call the script by cronjob as mentioned in the link: passing a command-line argument

Or you may refer to the PHP basic techniques as mentioned in the link: detect if script is run from a cron job

Community
  • 1
  • 1
Yazid Erman
  • 1,166
  • 1
  • 13
  • 24