IP to Country

This is not exactly a tutorial, but a small trick, to access ip-to-country.wehbosting.info demo as a web-service

ip-to-country.wehbosting.info does provide a nice CSV format of transferring IPs to country .. 🙂 Pretty amazing .
But the problem is you need to have a good dB support at your en to use it.

So for guys like me.. who can’t afford a dB have to rely on other ways, webservices etc…
But it seems that is not much in the line yet from those guys..
So I have written a mischiveous script that fakes a valid HTTP POST and gets back the country 😀

Since this script relies on their HTML page… It is highly likely that this script will fail and you need to modify it to make it working IF THEY CHANGE THE HTML.
Please don’t abuse the server… Its just for fun

<?php
$ip = $_GET['ip'];

function get_country($ip)
{
    $f = fsockopen('ip-to-country.webhosting.info', 80);
    if (!$f)
    {
        return false;
    }

    $postdata = "ip_address=".urlencode($ip)."&submit=".urlencode('Find Country');


    $request = '';
    $request .= "POST /node/view/36 HTTP/1.1\r\n";
    $request .= "Host: ip-to-country.webhosting.info\r\n";
    $request .= "User-Agent: Its me again\r\n";
    $request .= "Content-Length: ".strlen($postdata)."\r\n";
    $request .= "Content-Type: application/x-www-form-urlencoded\r\n";
    $request .= "\r\n";
    $request .= "$postdata\r\n";
    
    fwrite($f, $request);
    $response = '';
    while (!feof($f)) 
    {
           $response .= fgets($f, 128);
    }

    $pos1 = strpos ( $response , '</from>');

    $pos2 = strpos ( $response , '<br><br><img' , $pos1 );

    $parse_from = substr( $response, $pos1+21, ($pos2-$pos1) );
    $pattern = "/<b>([^\/]*)<\/b>/si";
    preg_match_all($pattern, $parse_from, $matches);

    return $matches[1][1];
}

echo (get_country($ip));
 ?>
 

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.