0

I am setting up trialpay for my website. The website shows :

Requests originate from the following IP address range: 70.42.249.1 - 70.42.249.255

In PHP, how do I check to see if the bot/person visiting my auth page falls within the above IP range?

1

Since you are dealing with ip addresses, it's probably easier to treat them as integers instead of strings. You can use the ip2long() function to do this.

Here's a working function to help you out:

function ipbetweenrange($needle, $start, $end) {
  if((ip2long($needle) >= ip2long($start)) && (ip2long($needle) <= ip2long($end))) {
    return true;
  }
  return false;
}

$ipstart = '70.42.249.1';
$ipend = '70.42.249.255';

echo ipbetweenrange('70.42.249.1', $ipstart, $ipend); // true
echo ipbetweenrange('70.42.249.255', $ipstart, $ipend); // true
echo ipbetweenrange('70.42.249.128', $ipstart, $ipend); // true
echo ipbetweenrange('192.168.0.1', $ipstart, $ipend); // false
0
foreach( range( 1, 255 ) as $number )
{

    if( $userIp == '70.42.249.' . $number )
        // we have a match, do whatever you want

}

Something along those lines. And yes, this should probably be moved to Stackoverflow.

Your Answer

By clicking "Post Your Answer", you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.