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?
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).
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.
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.
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;
with codeigniter, you simply do $ip = $this->input->ip_address()
to display your IP address
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With