XF 2.0 Undefined index: REMOTE_ADDR in SSH...

Jaxel

Well-known member
I use the following code in my config.php:
Code:
$debugs = array('MY-IP-ADDRESS');

if (in_array($_SERVER['REMOTE_ADDR'], $debugs))
{
    $config['designer']['enabled'] = true;
    $config['development']['enabled'] = true;
    $config['development']['throwJobErrors'] = true;
    $config['development']['fullJs'] = true;
}
This makes sure that my dev site only shows developer mode if coming from my IP address.

This works fine. Except when I use putty to SSH, to run cmd.php commands. In that case, I get the following error spamming my logs:
  • ErrorException: [E_NOTICE] Undefined index: REMOTE_ADDR
Is there a way to do what I am doing without the error? I mean, I guess I can do an isset...
 
I guess I can do an isset...
Yep, that would be recommended.

Alternatively, as the IP address check is not important in this context (because it can only be coming from the server itself) you could do this instead:
PHP:
if (PHP_SAPI == 'cli')
{
    $config['designer']['enabled'] = true;
    $config['development']['enabled'] = true;
    $config['development']['throwJobErrors'] = true;
    $config['development']['fullJs'] = true;
}
else
{
    $debugs = array('MY-IP-ADDRESS');

    if (in_array($_SERVER['REMOTE_ADDR'], $debugs))
    {
        $config['designer']['enabled'] = true;
        $config['development']['enabled'] = true;
        $config['development']['throwJobErrors'] = true;
        $config['development']['fullJs'] = true;
    }
}
 
REMOTE_ADDR is filled in by the web server I believe. Of course, there is no remote address when you run in CLI.

Use something like:

Code:
$debugMode = false;

if (php_sapi_name() == 'cli') {
    $debugMode = true;
} else {
    $debugs = array('MY-IP-ADDRESS');
    if (in_array($_SERVER['REMOTE_ADDR'], $debugs)) {
        $debugMode = true;
    }
}

if ($debugMode) {
    $config['designer']['enabled'] = true;
    $config['development']['enabled'] = true;
    $config['development']['throwJobErrors'] = true;
    $config['development']['fullJs'] = true;
}

Ninja'd by @Chris D. I do think my solution is slightly better since it avoids code repetition.
 
Indeed avoiding the repetition is useful but I did it like that for readability and it gives you the option of having different config values for each type :)
 
Seems overly complicated, I just use:

Code:
if($_SERVER['REMOTE_ADDR'] == 'x.x.x.x'){$config['debug'] = true;}

I have no issues with CLI
 
Seems overly complicated, I just use:

Code:
if($_SERVER['REMOTE_ADDR'] == 'x.x.x.x'){$config['debug'] = true;}

I have no issues with CLI

You should have issues with that though since the REMOTE_ADDR index doesn't exist (unless you have notices disabled)
 
Last edited:
Top Bottom