Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GEOIP and getting a computers IP address Location?

Tags:

php

ip

I want my site to be able to locate a computers location, so if someone is accessing my site from london or manchester etc and to have users from a certain area displayed depending on their computers location. kind of like an online dating site that suggests users in your area.

I have been looking at this GEOIP database that lists all the cities around the world. But i dont know what to do next? Do i need to look at get ip address scripts that pull and compare the information from the GEOip database?

Please can someone point me in the right direction. Thanks.

GeoIP database from: http://dev.maxmind.com/geoip/geolite

like image 895
Eric Matthews Avatar asked Dec 09 '22 20:12

Eric Matthews


1 Answers

Try the below code.

$ip =  $_SERVER['REMOTE_ADDR'];
echo $location = file_get_contents("http://api.hostip.info/country.php?ip=$ip");

Dont try this in your localhost. It will give the ip 127.0.0.1.

echo $location= file_get_contents("http://api.hostip.info/country.php?ip=12.215.42.19");
//outputs US
like image 114
Abhishek Saha Avatar answered Dec 12 '22 13:12

Abhishek Saha