REMOTE ADDR Problem with Cron

I need to get the visitor's IP address, but when cron starts, it detects that REMOTE ADDR is an undefined index. Actually there is no remote address when the file works with cron.

How to avoid this problem?

Is there a way in PHP to say "if REMOTE ADDR exists, then think about it, if not (because it works from cron), then bypass it" ??

$ip=$_SERVER['REMOTE_ADDR']; $allowed_ips = array("82.61.144.100", "82.64.144.100"); if (!in_array($ip, $allowed_ips)) { header("Location: http://pitchmystuff.co.uk/coming_soon/"); 

}

+1
source share
2 answers

Try:

 if (isset($_SERVER['REMOTE_ADDR']) && !empty($_SERVER['REMOTE_ADDR'])) { $ip = $_SERVER['REMOTE_ADDR']; } 

Your problem may be more complex than the way I see it.

+2
source

Best used

 if ((php_sapi_name() == 'cli')) { ... running as commandline/cron script ... } 

instead.

+5
source

Source: https://habr.com/ru/post/926145/


All Articles