Thread Rating:
  • 2 Vote(s) - 5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Add hosted GeoIP support to ModCP IP Lookup
#1
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.
Lost interest, sold my sites, will browse here once in a while. It's been fun.
Reply
#2
Thanks pavemen.
[Image: Jn29h.png]
DONATE TO PINGUY OS or show some love on Flattr.com
Reply
#3
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.
Reply
#4
(09-01-2016, 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/
Reply
#5
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
Don't do it for me, teach me how to do it myself.
Reply
#6
You can always use Patches plugin Smile
[MyBB 1.8 Czech translation] [MyBB 1.8 plugins]: Prune old PMs + optimize DB plugin --- Thank you/like system
Reply
#7
(12-22-2018, 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
Don't do it for me, teach me how to do it myself.
Reply
#8
(10-03-2019, 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.
Don't do it for me, teach me how to do it myself.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)