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
- How to create Covid19 Country wise status project using REST API ?
- PHP | Get PHP configuration information using phpinfo()
- PHP | dir() (Get instance of the Directory)
- HTTP GET and POST Methods in PHP
- How to get file name from a path in PHP?
- PHP | Ds\Deque get() Function
- How to get the first element of an array in PHP?
- Get the full URL in PHP
- How to get multiple selected values of select box in php?
- How to get the last n characters of a PHP string?
- How to get current function name in PHP?
- How to get name of calling function/method in PHP ?
- PHP | Ds\Sequence get() Function
- PHP | Ds\Vector get() Function
- How to get first day of week in PHP?
- How to get random value out of an array in PHP?
- Program to get the subdomain of a URL using PHP
- PHP Ds\Set get() Function
- How to send a GET request from PHP?
- How to get numeric index of associative array in PHP?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. 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.