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

Requests originate from the following IP address range: -

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


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 = '';
$ipend = '';

echo ipbetweenrange('', $ipstart, $ipend); // true
echo ipbetweenrange('', $ipstart, $ipend); // true
echo ipbetweenrange('', $ipstart, $ipend); // true
echo ipbetweenrange('', $ipstart, $ipend); // false
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.