Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
478 views
in Technique[技术] by (71.8m points)

php - GEOIP and getting a computers IP address Location?

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...