MyBB Community Forums

Full Version: Add hosted GeoIP support to ModCP IP Lookup
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
So the current (v1.6.3) ModCP supports GeoIP lookups when you do an IP Lookup, but your host needs to support it on the server.

If that is not possible, you can easily add a few lines of code to the modcp.php file to get country and city from a hosted solution.

Open modcp.php and find:
		// gethostbyaddr returns the same ip on failure
		if($ipaddress_host_name == $mybb->input['ipaddress'])
		{
			$ipaddress_host_name = $lang->na;
		}

and add below:
		$api_result = @file_get_contents('http://api.hostip.info/get_html.php?ip='.$mybb->input['ipaddress'].'&position=true');
		$each_line = explode("\n", $api_result);
		for ($i=0;$i<count($each_line);$i++)  
		 {
			$line = explode(':', $each_line[$i]);
			$data[$line[0]] = $line[1];
		}
		$ipaddress_location = $data['Country'].': '.$data['City'];

So when you click "(Information on This IP Address)" when searching IPs you can get GeoIP info when it is available, even if your host does not support GeoIP.

With the above "position=true" it also includes the lat/lon of the location. I have not included that in the above as it would require a template change. As it shows right now, it simply replaces the typical "n/a" output if you don't have GeoIP support on your server.
Thanks pavemen.
Sorry to bump this, but my friend's host dont got geoip extension so he get's :
Host Name: N/A
GeoIP Location: N/A

Found your thread, but API http://api.hostip.info/get_html.php?ip= no longer exist , any updates for this?

I know there is a plugin for such task, but need core edit.
(2016-09-01, 05:26 PM)Donald_Duck Wrote: [ -> ]Sorry to bump this, but my friend's host dont got geoip extension so he get's :
Host Name: N/A
GeoIP Location: N/A

Found your thread, but API http://api.hostip.info/get_html.php?ip= no longer exist , any updates for this?

I know there is a plugin for such task, but need core edit.

Thread was made in 2011. Find a new API such as : http://ip-api.com/
This code works in modcp.php version 1.8.19, placed as described in earlier post.
Drawback is it's a core edit, advantage is you made notes what you changed, so you aren't relying on a plugin that becomes out of date at the next upgrade.

// add
        $api_result = @file_get_contents('http://ip-api.com/line/'.$mybb->input['ipaddress'].'?fields=2613');
	$ipaddress_location = '';
        $each_line = explode("\n", $api_result);
        for ($i=0;$i<count($each_line);$i++)  
        {
            $ipaddress_location .= $each_line[$i] . '<br>';
        }



//        $api_result = @file_get_contents('http://api.hostip.info/get_html.php?ip='.$mybb->input['ipaddress'].'&position=true');
//        $each_line = explode("\n", $api_result);
//        for ($i=0;$i<count($each_line);$i++)  
//         {
//            $line = explode(':', $each_line[$i]);
//            $data[$line[0]] = $line[1];
//        }
//        $ipaddress_location = $data['Country'].': '.$data['City']; 
// end add
You can always use Patches plugin Smile
(2018-12-22, 11:46 PM)HLFadmin Wrote: [ -> ]This code works in modcp.php version 1.8.19, placed as described in earlier post.
Drawback is it's a core edit, advantage is you made notes what you changed, so you aren't relying on a plugin that becomes out of date at the next upgrade.

// add
        $api_result = @file_get_contents('http://ip-api.com/line/'.$mybb->input['ipaddress'].'?fields=2613');
	$ipaddress_location = '';
        $each_line = explode("\n", $api_result);
        for ($i=0;$i<count($each_line);$i++)  
        {
            $ipaddress_location .= $each_line[$i] . '<br>';
        }

Having changed servers at a new hosting provider, and updating to 1.8.21, and ip-api.com no longer online, I've made some changes to this core edit. ip2location.com provides a lookup api with a small allowance for demo purposes. Not sure if it is time limited. Their data seems to be reasonably accurate. The result is json-encoded. I have not gone fancy with decoding, "explode" displays all I want to see.

I haven't discovered what caused it (probably new server or new host limitation), but file_get_contents() no longer works with a URL.
fetch_remote_file is a full-featured MyBB function that uses cURL, so rather than reinvent the wheel....

// add
        $api_result = @fetch_remote_file('https://api.ip2location.com/v2/?ip='.$mybb->input['ipaddress'].'&package=WS7&key=demo');
	$ipaddress_location = '';
        $each_line = explode(":", $api_result);
        for ($i=0;$i<count($each_line);$i++)  
        {
            $ipaddress_location .= $each_line[$i] . '<br>';
        }

// end add
(2019-10-03, 01:47 PM)HLFadmin Wrote: [ -> ]I haven't discovered what caused it (probably new server or new host limitation), but file_get_contents() no longer works with a URL.

PHP.ini on server has allow_url_fopen disabled.
Fixed.