0

I have a IP range php function that responds with 1 if ip is in range.

Php:

<?
$ip = $_SERVER['REMOTE_ADDR'];

$public_ip_ranges = array();

$range = (object) array();
$range->name = 'Barclays';
$range->lower = '141.228.0.0';
$range->upper = '141.228.255.255';
$public_ip_ranges[] = $range;

$range = (object) array();
$range->name = 'Incisive Media';
$range->lower = '10.1.0.0';
$range->upper = '10.1.255.255';
$public_ip_ranges[] = $range;

$range = (object) array();
$range->name = 'Barcap';
$range->lower = '146.127.0.0';
$range->upper = '146.127.255.255';
$public_ip_ranges[] = $range;


if (($lngIP=ip2long($ip)) < 0) $lngIP += 4294967296;  

foreach ($public_ip_ranges as $ip_range) {
   if (($lngLow=ip2long($ip_range->lower)) < 0) $lngLow += 4294967296;
   if (($lngHigh=ip2long($ip_range->upper)) < 0) $lngHigh += 4294967296;
   if($lngIP >= $lngLow and $lngIP <= $lngHigh) {
       echo 1;
    }

}

?>

I have ajax referring to this file but what i would like to do is if PHP function echo's 1 then display x text else display Y text.

Ajax:

   $.ajax( { 
            url: "/microsub.php",
            method: 'GET',
            success: function (data) {
             console.log(data);
             },
             error: function(error) {
                 console.log(error);
             }

        } );

So I would like to take the response from the php (which should echo 1 if in range) and display text alert else if not display different text alert

4
  • And your question is...? This site is for questions, not a place to dump to-do lists. Commented Aug 9, 2016 at 16:25
  • You might want to try something like this in your success: function(data) part: if(data == 1){ // do something because the IP is in range } else { // do another thing because the IP is not in range }. It's like Marc says, though, we need your code to help you with. Commented Aug 9, 2016 at 16:25
  • whatever you echo will be stored in data Commented Aug 9, 2016 at 16:34
  • First of all, what is the output of data (through ajax) ? Commented Aug 9, 2016 at 18:48

1 Answer 1

1

you can return an error:

$isPublic = true;
foreach ($public_ip_ranges as $ip_range) {
   if (($lngLow=ip2long($ip_range->lower)) < 0) $lngLow += 4294967296;
   if (($lngHigh=ip2long($ip_range->upper)) < 0) $lngHigh +=4294967296;

   $isPublic = ($lngIP >= $lngLow) && ($lngIP <= $lngHigh) && $isPublic;
}

if(!$isPublic) {
   header("HTTP/1.0 404 Not Found");
}

echo $isPublic;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.