I want to retrieve information like the city, state, and country of a visitor from their IP address, so that I can customize my web page according to their location. Is there a good and reliable way to do this in PHP? I am using JavaScript for client-side scripting, PHP for server-side scripting, and MySQL for the database.
相关问题
- Views base64 encoded blob in HTML with PHP
- Laravel Option Select - Default Issue
- PHP Recursively File Folder Scan Sorted by Modific
- Can php detect if javascript is on or not?
- Using similar_text and strpos together
You need to use an external service... such as http://www.hostip.info/ if you google search for "geo-ip" you can get more results.
The Host-IP API is HTTP based so you can use it either in PHP or JavaScript depending on your needs.
Ok, guys, thanks for your suggestions; While i have 6k+ of IPs, some services will failed my requests due to some limitations; So, you could use them all in fallback mode;
If we have source file with following format:
than you could use this simple expample command (PoC) for Yii:
This is some kind of shitty-code, but it works. usage:
Feel free to use, modify it, and do it better )
Assuming you want to do it yourself and not rely upon other providers, IP2Nation provides a MySQL database of the mappings which are updated as the regional registries change things around.
This question is protected, which I understand. However, I do not see an answer here, what I see is a lot of people showing what they came up with from having the same question.
There are currently five Regional Internet Registries with varying degrees of functionality that serve as the first point of contact with regard to IP ownership. The process is in flux, which is why the various services here work sometimes and don't at other times.
Who Is is (obviously) an ancient TCP protocol, however -- the way it worked originally was by connection to port 43, which makes it problematic getting it routed through leased connections, through firewalls...etc.
At this moment -- most Who Is is done via RESTful HTTP and ARIN, RIPE and APNIC have RESTful services that work. LACNIC's returns a 503 and AfriNIC apparently has no such API. (All have online services, however.)
That will get you -- the address of the IP's registered owner, but -- not your client's location -- you must get that from them and also -- you have to ask for it. Also, proxies are the least of your worries when validating the IP that you think is the originator.
People do not appreciate the notion that they are being tracked, so -- my thoughts are -- get it from your client directly and with their permission and expect a lot to balk at the notion.
I run the service at IPLocate.io, which you can hook into for free with one easy call:
The
$res
object will contain your geolocation fields likecountry
,city
, etc.Check out the docs for more information.
Using Google APIS: