EllisLab text mark
Advanced Search
     
Bug: PHP errors when using command line (CLI)
Posted: 08 October 2012 11:59 AM   [ Ignore ]
Joined: 2010-04-01
25 posts

Calling an empty function via CLI and getting this notice level error back:

starovichphp index.php utils empty_function
<div>

<
h4>A PHP Error was encountered</h4>

<
p>SeverityNotice</p>
<
p>Message:  Undefined indexREMOTE_ADDR</p>
<
p>Filenamecore/Input.php</p>
<
p>Line Number351</p>

</
div><div>

<
h4>A PHP Error was encountered</h4>

<
p>SeverityWarning</p>
<
p>Message:  Cannot modify header information headers already sent by (output started at /media/sf_www-host/project/system/core/Exceptions.php:185)</p>
<
p>Filenamelibraries/Session.php</p>
<
p>Line Number675</p

Problem stems from assuming that $_SERVER[‘REMOTE_ADDR’] exists, which is not the case in CLI mode.

It can be easily fixed by adding a check in core/Input.php Line 351 (CI 2.1.2):

From:

$this->ip_address $_SERVER['REMOTE_ADDR']

To:

if(isset($_SERVER['REMOTE_ADDR']))
 
$this->ip_address $_SERVER['REMOTE_ADDR'];
else
 
$this->ip_address '0.0.0.0'

This or a similar fix should probably be included in the next release to avoid these messages. Thanks!

 
Posted: 08 October 2012 01:19 PM   [ Ignore ]   [ # 1 ]   [ Rating: 0 ]
Avatar
Joined: 2009-02-19
3819 posts

Good find. Please fill out an official issue so the CI developers will see it and check it out.  Fixing it and adding a pull request would be even better.

https://github.com/EllisLab/CodeIgniter/issues?direction=desc&sort=created&state=open

 Signature 
 
Posted: 08 October 2012 05:51 PM   [ Ignore ]   [ # 2 ]   [ Rating: 0 ]
Joined: 2010-04-01
25 posts
CroNiX - 08 October 2012 01:19 PM

Good find. Please fill out an official issue so the CI developers will see it and check it out.  Fixing it and adding a pull request would be even better.

https://github.com/EllisLab/CodeIgniter/issues?direction=desc&sort=created&state=open

Checked the commit history and it seems like this has been working for several months.

It seems the file in version 2.1.3 is an obsolete version. I opened an issue here: https://github.com/EllisLab/CodeIgniter/issues/1861

 
Posted: 09 October 2012 07:24 AM   [ Ignore ]   [ # 3 ]   [ Rating: 0 ]
Avatar
Joined: 2012-01-09
117 posts

As I’ve already said in a comment on that issue - there’s nothing wrong with system/core/Input.php in CI 2.1.3.

There’s only one way that you could experience this issue and that is if you’re calling CI_Input::ip_address() from somewhere in your CLI application. You haven’t received such notices up until now, because that method used to call CI_Input::server() instead of directly copying from the $_SERVER super-global (and that will again be the case in CodeIgniter’s next version), but still - your app is looking for an IP address in a CLI environment, so you can’t blame the framework.

 
Posted: 09 October 2012 08:12 AM   [ Ignore ]   [ # 4 ]   [ Rating: 0 ]
Joined: 2010-04-01
25 posts
Narf - 09 October 2012 07:24 AM

As I’ve already said in a comment on that issue - there’s nothing wrong with system/core/Input.php in CI 2.1.3.

There’s only one way that you could experience this issue and that is if you’re calling CI_Input::ip_address() from somewhere in your CLI application. You haven’t received such notices up until now, because that method used to call CI_Input::server() instead of directly copying from the $_SERVER super-global (and that will again be the case in CodeIgniter’s next version), but still - your app is looking for an IP address in a CLI environment, so you can’t blame the framework.

I checked this again and the error comes up if you autoload the Session library.

Even though Session isn’t applicable for CLI, it should not make the application generate errors. E.g. many applications autoload Session and use a controller for CRON tasks.

Like you said though this has already been fixed in upcoming versions. Thanks!