Overview: To obtain the details like country, continent, city, etc of the visitor, first we need to get the IP of the visitor. The IP address can be obtained with the help of superglobal $_SERVER in PHP. Finally, with the use of API geoPlugin, we can obtain information about the IP address i.e. visitor of the website.
Step 1: Getting the visitor’s IP address.
$_SERVER is a PHP Superglobals variable which holds information about headers, IP, script details, etc. Elements like REMOTE_ADDR, HTTP_X_REAL_IP, HTTP_CLIENT_IP and HTTP_X_FORWARDED_FOR can be used to get the IP from this super global.
Example: This example obtain the IP of the visitors.
Note: In case of CloudFlare, one may need to use elements like HTTP_X_REAL_IP to obtain the IP address.
Step 2: Use API to get the details of the visitor’s IP: Here, we are going to use the geoPlugin API to get the details of the visitor. The api will deliver a json object, which can convert to a PHP variable.
Country Name: United States City Name: Boardman Continent Name: North America Latitude: 45.8491 Longitude: -119.7143 Currency Symbol: $ Currency Code: USD Timezone: America/Los_Angeles
- Create a Sticky Social Media Bar using HTML and CSS
- Error Handling in Perl
- Perl | File I/O Functions
- How to reset a form using jQuery with .reset() method?
- Deploy cloud function on Google Cloud Platform
- Bajaj Electricals Interview Experience
- Perl | Line Action Commands in a Debugger
- 8 Ways to Shipwreck Your Next Website Design
- PHP | Regular Expressions
- What is the equivalent of ngShow and ngHide in Angular 2+?
- How to append Angular styles in footer ?
- Get and Post method using Fetch API
- HTML5 | MathML <mspace> Tag
- CSS | zoom Property
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.