Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Automatically locate a user's geographical location in PHP

I want to automatically locate the user's geographical location and IP address in PHP upon registration to my website. How do I go about it?

like image 670
Dunco Avatar asked Jun 14 '16 04:06

Dunco


People also ask

How to detect user location in PHP?

The getCurrentPosition() method is used to get the visitor's position and showLocation() method is used to getting the visitor's address from the getLocation. php file using Ajax. HTML Code: After getting the visitor position, the address will be shown on the web page ( #location span).

How to get Geolocation from IP address in PHP?

Prepare API request to get geolocation via PHP cURLThe getipaddress() function builds an if-else-if ladder of the majority of the scenario to get the non-empty IP address using the $_SERVER variable. Once the IP is validated and returns true, the getLocation() function requests the ipwhois API via the PHP cURL.

How do I find the location of a website user?

One of the useful ways of getting your user location is by the use of IP address lookup although it may not be entirely free depending the API. Some very good APIs for this operation are http://ip-api.com/, https://ipinfo.io, geoip-db.com and many others.


2 Answers

ip-api.com provides free xml based api.

To get the ip based location using their api, you may use :

$xml=simplexml_load_file("http://ip-api.com/xml/ipaddress");
echo $xml->RegionName;
like image 164
Amit Verma Avatar answered Oct 04 '22 22:10

Amit Verma


with codeigniter, you simply do $ip = $this->input->ip_address() to display your IP address

like image 39
AmosJ Avatar answered Oct 04 '22 20:10

AmosJ