CodeIgniter Forums

Full Version: Cron job returns "Undefined index: REQUEST_METHOD"
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

I'm attempting to run a cron job using CodeIgniter 3.1.8. The cron job works fine locally from the Windows command line, but when I run it on a Linux server as a CPanel cron, I get this in the output (among various other session related warnings):

<h4>A PHP Error was encountered</h4>
<p>Severity: Notice</p>
<p>Message:  Undefined index: REQUEST_METHOD</p>
<p>Filename: core/Security.php</p>
<p>Line Number: 211</p>

Would be grateful for any help...

You have to give us more information. Can we see the code in the cron job? Are you running the same version of PHP? If one system is Windows and the other is Linux, what is the difference that could be causing the problem?
Sounds like CSRF protection to me. Depending on how your accessing the script for the cronjob.
CSRF protection is enabled. Windows machine is running PHP 7.1.17. Linux server is running PHP 7.1.27. My CPanel cronjob has the form:

/usr/bin/php -f /home/<directory>/<directory>/index.php cronjobs statistics

My relevant controller code:

class Cronjobs extends CI_Controller

   public function __construct()
       $objMail = $this->phpmailer_library->load();
       $params = array($objMail);
       $this->load->library('emailmanager', $params);

    * This function can be run only via cron script.
    * It sends an email with statistics to administrator.
   public function statistics()
       if (is_cli() === TRUE)
           $today = date('Y-m-d');          
           $statistics['total'] = $this->accounts_model->count_accounts();
           $this->emailmanager->SendStatisticsEmail('Statistics ' . $today, $statistics);            
           $this->output->set_status_header(401); // Unauthorized        

How where you able to run the cron job in windows? Did you do it manually? Also, did you check the CI error log?

Yes, to be more accurate, on Windows I can run this job successfully from the command line manually (not actually as a cron), like this:

php index.php cronjobs statistics

From the log on the Linux server where I get the problem I can now see that it falls back for some reason to the default controller, namely:

DEBUG - 2019-03-20 19:32:01 --> UTF-8 Support Enabled
DEBUG - 2019-03-20 19:32:01 --> No URI present. Default controller set.
DEBUG - 2019-03-20 19:32:01 --> Global POST, GET and COOKIE data sanitized
ERROR - 2019-03-20 19:32:01 --> Severity: Notice --> Undefined index: REQUEST_METHOD /home/.../ 211
DEBUG - 2019-03-20 19:32:01 --> Session: "sess_save_path" is empty; using "session.save_path" value from php.ini.
ERROR - 2019-03-20 19:32:01 --> Severity: Warning --> session_start(): Cannot send session cookie - headers already sent /home/.../.../system/libraries/Session/Session.php 143
ERROR - 2019-03-20 19:32:01 --> Severity: Warning --> session_start(): Cannot send session cache limiter - headers already sent /home/.../.../system/libraries/Session/Session.php 143
DEBUG - 2019-03-20 19:32:01 --> Total execution time: 0.1314

I've double checked of course that the path to index.php is correct in my cron job. Maybe it's worth pointing out that the server directory where index.php lives is not public_html, but a directory parallel to public_html. Also, the directory name contains dots, like "", if that matters. So basically the structure is:

      some non-CI website (CI Home)

The route correctly returns 401 when accessed directly from the browser.

I've also tried disabling CSRF protection completely just to be able to narrow down the issue, but that didn't help...

What is the crontab job command used to run it? Also, what is the permissions of the file to be executed?

/usr/bin/php -f /home/<directory>/<directory>/index.php cronjobs statistics

File permissions on index.php are "0644".
The problem has now been solved, by calling PHP with


instead of just


Thank you everyone for interest in this issue!