Jump to content

[SOLVED] IP script bugs cont'd


darkfreaks

Recommended Posts

ok so im trying to view the site with an ip of 70.112.156.140

 

but it tells me i am banned even though its a united states address. maybe the lack of range of adresses in the database?

 

i would need  the range of adresses for this IP

Example from database:

 

0000000000,0000000000, UNITED STATES

 

Link to comment
Share on other sites

how do i take that curl script and create something that willcompare the person's IP to the GEO IP database and return the country United States and if it matches to let them enter the site else put up a message if it doesnt match United states?

Link to comment
Share on other sites

How about this...

 

<?php
<?php

   /**
    * Geolocation API access
    *
    * @param    string  $ip         IP address to query
    * @param    string  $format     output format of response
    *
    * @return   string  XML, JSON or CSV string
    */
    function get_ip_location($ip, $format="xml") {

        /* Set allowed output formats */
        $formats_allowed = array("json", "xml", "raw");

        /* IP location query url */
        $query_url = "http://iplocationtools.com/ip_query.php?ip=";

        /* Male sure that the format is one of json, xml, raw.
           Or else default to xml */
        if(!in_array($format, $formats_allowed)) {
            $format = "xml";
        }

        $query_url = $query_url . "{$ip}&output={$format}";

        /* Init CURL and its options*/
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $query_url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_TIMEOUT, 15);

        /* Execute CURL and get the response */
        return curl_exec($ch);

    }


    $location_data = get_ip_location("70.112.156.140");

    //here you could use a regular expression or something along those lines to extract the information that is between "<CountryName>" and "</CountryName>" (I'll let you figure that one out on your own or see if someone else here can do it for you.

//from there just use your previous code to check and see if the country name matches your if statements

?>

 

REMEMBER to trim your data (we had that big issue last time ;-) )

Link to comment
Share on other sites

Hello,

 

I have extended the code a bit in order to get the <CountryName></CountryName> node from xml string.

 

<?php

   /**
    * Geolocation API access
    *
    * @param    string  $ip         IP address to query
    * @param    string  $format     output format of response
    *
    * @return   string  XML, JSON or CSV string
    */
    function get_ip_location($ip, $format="xml") {

        /* Set allowed output formats */
        $formats_allowed = array("json", "xml", "raw");

        /* IP location query url */
        $query_url = "http://iplocationtools.com/ip_query.php?ip=";

        /* Male sure that the format is one of json, xml, raw.
           Or else default to xml */
        if(!in_array($format, $formats_allowed)) {
            $format = "xml";
        }

        $query_url = $query_url . "{$ip}&output={$format}";

        /* Init CURL and its options*/
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $query_url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_TIMEOUT, 15);

        /* Execute CURL and get the response */
        $response = curl_exec($ch);

        if ( $format == 'xml' ) {
             $xmlObj = simplexml_load_string($response);
             return $xmlObj->CountryName;
        } 
    }


    $location_data = get_ip_location("70.112.156.140");

    //here you could use a regular expression or something along those lines to extract the information that is between "<CountryName>" and "</CountryName>" (I'll let you figure that one out on your own or see if someone else here can do it for you.

//from there just use your previous code to check and see if the country name matches your if statements

?>

 

Hope this will help.

 

Link to comment
Share on other sites

well the function just returns all the strings from the site i just want it to return the country

 

Just use a simpleXML object to grab whatever is between the country elements.

Link to comment
Share on other sites

well the function just returns all the strings from the site i just want it to return the country

 

Try the new extended function as below:

 

<?php

   /**
    * Geolocation API access
    *
    * @param    string  $ip         IP address to query
    * @param    string  $format     output format of response
    *
    * @return   string  XML, JSON or CSV string
    */
    function get_ip_location($ip, $format="xml") {

        /* Set allowed output formats */
        $formats_allowed = array("json", "xml", "raw");

        /* IP location query url */
        $query_url = "http://iplocationtools.com/ip_query.php?ip=";

        /* Male sure that the format is one of json, xml, raw.
           Or else default to xml */
        if(!in_array($format, $formats_allowed)) {
            $format = "xml";
        }

        $query_url = $query_url . "{$ip}&output={$format}";

        /* Init CURL and its options*/
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $query_url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_TIMEOUT, 15);

        /* Execute CURL and get the response */
        $response = curl_exec($ch);
        
        /* Get CountryName */
        if ( $format == 'xml' ) {
             $xmlObj = simplexml_load_string($response);
             return $xmlObj->CountryName;
        } 
    }


    $location_data = get_ip_location("70.112.156.140");
    
    echo $location_data;

?>

 

 

Link to comment
Share on other sites

ok i think i got it do i just do

 

if($xmlObj->Countryname=="United States) {}

 

to compare

 

 

Edit:nope how do i compare the country from the IP?

 

It will be more easier to compare with the country code rather than country name:

 

if ( $xmlObj->CountryCode == 'US' ) {
   // DO YOUR STAFF HERE
}

 

Will it help?

 

Link to comment
Share on other sites

<?php

   /**
    * Geolocation API access
    *
    * @param    string  $ip         IP address to query
    * @param    string  $format     output format of response
    *
    * @return   string  XML, JSON or CSV string
    */
    function get_ip_location($ip, $format="xml") {

        /* Set allowed output formats */
        $formats_allowed = array("json", "xml", "raw");

        /* IP location query url */
        $query_url = "http://iplocationtools.com/ip_query.php?ip=";

        /* Male sure that the format is one of json, xml, raw.
           Or else default to xml */
        if(!in_array($format, $formats_allowed)) {
            $format = "xml";
        }

        $query_url = $query_url . "{$ip}&output={$format}";

        /* Init CURL and its options*/
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $query_url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_TIMEOUT, 15);

        /* Execute CURL and get the response */
        return curl_exec($ch);
    if ( $format == 'xml' ) {
             $xmlObj = simplexml_load_string($response);
             return $xmlObj->CountryCode;
        } 
    }
    


    $location_data = get_ip_location($_SERVER[REMOTE_ADDR]);
    
    if ( $xmlObj->CountryCode == 'US' ) { echo "this is a united states ip";} else { echo "this is not a united states ip";}
?>

Link to comment
Share on other sites

Little BUG fixed in your code. Use this code snippet and test.

 

<?php

   /**
    * Geolocation API access
    *
    * @param    string  $ip         IP address to query
    * @param    string  $format     output format of response
    *
    * @return   string  XML, JSON or CSV string
    */
    function get_ip_location($ip, $format="xml") {

        /* Set allowed output formats */
        $formats_allowed = array("json", "xml", "raw");

        /* IP location query url */
        $query_url = "http://iplocationtools.com/ip_query.php?ip=";

        /* Male sure that the format is one of json, xml, raw.
           Or else default to xml */
        if(!in_array($format, $formats_allowed)) {
            $format = "xml";
        }

        $query_url = $query_url . "{$ip}&output={$format}";

        /* Init CURL and its options*/
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $query_url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_TIMEOUT, 15);

        /* Execute CURL and get the response */
        //return curl_exec($ch);

        $response = curl_exec($ch);

    if ( $format == 'xml' ) {
             $xmlObj = simplexml_load_string($response);
             return $xmlObj->CountryCode;
        } 
    }
    


    $location_data = get_ip_location($_SERVER[REMOTE_ADDR]);
    
    if ( $location_data == 'US' ) { echo "this is a united states ip";} else { echo "this is not a united states ip";}
?>

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.