-3

Not a PHP expert. I have a snippet here that redirects based on one IP. Assuming that I have a multiple IP list, what would be the best way to rewrite this to accommodate multiple IPs (perhaps in one list). Thanks for your help!

<?php
// Redirect if IP matches
if (strpos($_SERVER['REMOTE_ADDR'], "192.168.1.")!==FALSE)
{    
    header ('Location: redirect.php');
}
?>

EDIT: Let me rephrase, since I'm getting downvotes. Sure, array is great, but I still want to use strpos for ranges.

Kirill Chatrov
  • 822
  • 6
  • 11

1 Answers1

3

Use an array to do this:

if (in_array($_SERVER['REMOTE_ADDR'], ["192.168.1.1", "192.168.1.2", ...]))
Felippe Duarte
  • 14,901
  • 2
  • 25
  • 29
  • But what about using strpos for ranges? – Kirill Chatrov Aug 24 '17 at 19:42
  • 1
    @KirillChatrov Take a look at https://stackoverflow.com/questions/2869893/block-specific-ip-block-from-my-website-in-php/2869931#2869931, you should be able to make an array with the values from ip2long ran on each string and then check if the ip2long of the REMOTE_ADDR is in the array – Charles Aug 24 '17 at 19:49