REMOTE_ADDR is empty, not included in the SERVER array

I had a strange problem after moving to a new server. Cron to receive mail checks for authorized IP addresses, one of which by default is 127.0.0.1

It stops working after moving because the REMOTE_ADDR variable is not populated. It is called from the browser, but not when running from within cron or from the console with php. I reset the $ _SERVER variable and that’s all it has from cron / console

(
   [SHELL] => /bin/sh
   [MAILTO] => *removed*
   [USER] => *removed*
   [PATH] => /usr/bin:/bin
   [PWD] => /home/*removed*
   [SHLVL] => 1
   [HOME] => /home/*removed*
   [LOGNAME] => *removed*
   [_] => /usr/local/bin/php
   [PHP_SELF] => /home/*removed*/public_html/support/cron.php
   [SCRIPT_NAME] => /home/*removed*/public_html/support/cron.php
   [SCRIPT_FILENAME] => /home/*removed*/public_html/support/cron.php
   [PATH_TRANSLATED] => /home/*removed*/public_html/support/cron.php
   [DOCUMENT_ROOT] =>
   [REQUEST_TIME] => 1300522141
   [argv] => Array
       (
           [0] => /home/*removed*/public_html/support/cron.php
       )

   [argc] => 1
)

if(!$cron->isValidIp($_SERVER['REMOTE_ADDR'])) {
    echo sprintf("[ERROR]: Your IP %s is not authorized to run scheduled tasks.  Please notify your administrator.",
        $_SERVER['REMOTE_ADDR']
    );

        // [JAS]: Test all our IPs for a wildcard match
        if(is_array($this->valid_ips))
        foreach($this->valid_ips as $mask) {
            if(empty($mask)) continue;
            if(0 == strcmp(substr($ip,0,strlen($mask)),$mask)) {
                return true;
            }
+3
source share
2 answers

The variable is $_SERVER['REMOTE_ADDR']populated due to Apache launched from the command line; this variable will not be set, like many others.

, , REMOTE_ADDR IP- , cron, .

[]

, php_sapi_name

if(php_sapi_name() === 'cli') {
    // You're running locally from the CLI
} else {
    // You're running remotely, check against list of authorized ip addresses.
}

, :

if(php_sapi_name() != 'cli' && !$cron->isValidIp($_SERVER['REMOTE_ADDR'])) {
    ....
+7

REMOTE_ADDR script, HTTP.

, , script -? , - cron .

+1

All Articles