2

I need to get the IP of the visitor, but when the cron runs it finds that REMOTE ADDR is an undefined index. In fact there is no remote address when the file runs with the cron.

How do I avoid this issue?

Is there a way in PHP to say " if REMOTE ADDR exists then consider it, if not ( because it is running from the 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/");

}

DiegoP.
  • 45,177
  • 34
  • 89
  • 107

2 Answers2

8

Better method would be to use

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

instead.

Marc B
  • 356,200
  • 43
  • 426
  • 500
2

Try:

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

Your problem might be more complex than how I'm seeing it.

Chris Bornhoft
  • 4,195
  • 4
  • 37
  • 55