CodeIgniter Forums
Cron job returns "Undefined index: REQUEST_METHOD" - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24)
+--- Thread: Cron job returns "Undefined index: REQUEST_METHOD" (/showthread.php?tid=73105)



Cron job returns "Undefined index: REQUEST_METHOD" - anturom - 03-19-2019

Hi,

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...


RE: Cron job returns "Undefined index: REQUEST_METHOD" - php_rocs - 03-19-2019

anturom,

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?


RE: Cron job returns "Undefined index: REQUEST_METHOD" - kilishan - 03-19-2019

Sounds like CSRF protection to me. Depending on how your accessing the script for the cronjob.


RE: Cron job returns &quot;Undefined index: REQUEST_METHOD&quot; - anturom - 03-20-2019

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:

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

My relevant controller code:

Code:
class Cronjobs extends CI_Controller
{

   public function __construct()
   {
       parent::__construct();
       $this->load->model('accounts_model');
       $this->load->helper('url');
       $this->load->library('phpmailer_library');
       $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);            
       }
       else
       {
           $this->output->set_status_header(401); // Unauthorized        
       }
   }
}



RE: Cron job returns "Undefined index: REQUEST_METHOD" - php_rocs - 03-20-2019

@anturom,

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


RE: Cron job returns &quot;Undefined index: REQUEST_METHOD&quot; - anturom - 03-20-2019

@php_rocs,

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:

Code:
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:

Code:
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/.../sandbox.mydomain.com/system/core/Security.php 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 "my.subdomain.com", if that matters. So basically the structure is:

Code:
dir
  public_html
      some non-CI website
  sandbox.domain.com (CI Home)
      application
      system
      ....
      index.php

The route http://sandbox.mydomain.com/cronjobs/statistics 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...


RE: Cron job returns "Undefined index: REQUEST_METHOD" - php_rocs - 03-20-2019

@anturom,

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


RE: Cron job returns &quot;Undefined index: REQUEST_METHOD&quot; - anturom - 03-20-2019

@php_rocs,

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

File permissions on index.php are "0644".


RE: Cron job returns &quot;Undefined index: REQUEST_METHOD&quot; - anturom - 03-23-2019

The problem has now been solved, by calling PHP with

Code:
/usr/bin/php-cli

instead of just

Code:
/usr/bin/php

Thank you everyone for interest in this issue!