0

I'm trying to set up a cron to run a codeigniter controller/method.

I have my application and system file etc outside of the root.

I have created a cli.php file as a hack to get it working, as suggested by someone on the codeigniter forums. the content of the file are:

if (isset($_SERVER['REMOTE_ADDR'])) {
die('Command Line Only!');
}

set_time_limit(0);

$_SERVER['PATH_INFO'] = $_SERVER['REQUEST_URI'] = $argv[1];

require dirname(__FILE__) . '/public_html/index.php';

Now, I can successfully run the controller/method via cli using this termical command:

php cli.php cron/reccuring_checks

But can I get it to run as a cron?? oh dear NO i cannot, been at this for a whole day.

I've tried: (as well as 1000 different other combinations)

php cli.php cron reccuring_checks 
/usr/bin/php -q /home/DOMAIN/cli.php cron/reccuring_checks
/usr/bin/php -q /home/DOMAIN/cli.php cron reccuring_checks

I get the following error

<p>Severity: Notice</p>
<p>Message:  Undefined index: REMOTE_ADDR</p>
<p>Filename: drivers/Session_files_driver.php</p>
<p>Line Number: 130</p>

And a lot of the posts I can find online about this generally relate to CI2 and the method of replacing:

$_SERVER['REMOTE_ADDR'] with $this->server('remote_addr')

In the system/core/input file, but that is already amended in CI3

So has anyone got a clue how I can get this to work?

I assume the issue is that I have the core files above root, but thats what CI suggest you do for security, and I'm way to far down the line to change it now?

frobak
  • 557
  • 2
  • 11
  • 30
  • 1
    in your cli.php file try setting the var `$_SERVER['REMOTE_ADDR'] = '127.0.0.1'` which is not the nicest way but it should work. – mic May 25 '16 at 15:35
  • OK great, thats removed the error, but the script isnt running. What would be the correct cron code? – frobak May 25 '16 at 15:42
  • Om now getting this erro: Message: session_start(): Cannot send session cookie - headers already sent – frobak May 25 '16 at 15:43
  • something must to echoing out something or there is some whitespace somewhere! I would double check to make sure you are not outputting anything inc whitespace. – mic May 25 '16 at 15:47
  • Nope, theres no echo. How would whitespace be output? – frobak May 25 '16 at 15:50
  • if you have random tabs and/or spaces after closing tags in files. Basically that error is saying that something has been sent to the output buffer before the session was created. This usually indicates that you have an echo/print/var_dump etc etc somewhere that is causing output to occur before the session is created. here is a better explanation than I can give you http://stackoverflow.com/questions/8028957/how-to-fix-headers-already-sent-error-in-php – mic May 25 '16 at 15:54

2 Answers2

1

change this:

if (isset($_SERVER['REMOTE_ADDR'])) {
    die('Command Line Only!');
}

with:

if(!$this->input->is_cli_request()){
    die('Command Line Only!')
}

You shoud check codeigniter documentation

input class

https://www.codeigniter.com/user_guide/libraries/input.html

Stack Programmer
  • 679
  • 6
  • 18
  • this won't work because the CI functions are not available at that point. Nothing has been initialised in the core. This would only work if he was using it in a controller / model / library etc... – mic May 25 '16 at 15:45
  • nope its work like a charm, this function is just for this kind of checks ;) – Yordan Boikov May 25 '16 at 15:55
  • how? lol if you create a custom index.php file and put that straight at the top it will never work because it has no clue what $this is!! $this is used in the scope of classes and other objects at this point there are no classes or objects loaded. – mic May 25 '16 at 15:57
1

You don't need to create a special file nor you need any kind of hacks to get it working.

Simply, You create a controller & request it through index.php

Your cron command should look like this

/usr/bin/php -q /path/to/codeigniter/index.php controller_name method

assuming your controller is named 'cron' & your method is 'reccuring_checks' it would be something like this

/usr/bin/php -q /path/to/codeigniter/index.php cron reccuring_checks

Limiting your controller to command line access only is as simple as follows:

class Cron extends CI_Controller {
    public function __construct () {
        parent::__construct();
        // Limit it to CLI requests only using built-in CodeIgniter function
        if ( ! is_cli() ) exit('Only CLI access allowed');
    }
}

If your hosting provider is using cPanel control panel, Your PHP path should be as follows:

/usr/local/bin/php
ahmad
  • 2,709
  • 1
  • 23
  • 27
  • The installation is above ROOT, which is why i cant just use index.php? – frobak May 25 '16 at 15:48
  • Installation location doesn't matter, If your index.php file can find the required controllers & system folder settings is correct it should work... You're using command line not a web-server to process this – ahmad May 25 '16 at 15:49
  • There were extra slashes, You send ``controller_name method_name`` without a slash (separated by a space instead of a slash) – ahmad May 25 '16 at 15:56
  • OK, this worked thank you. It seemed i was missign local from the php path, which is very annoying. Thank you! – frobak May 25 '16 at 15:59