1//Gets the IP Address from the visitor
2$PublicIP = $_SERVER['REMOTE_ADDR'];
3//Uses ipinfo.io to get the location of the IP Address, you can use another site but it will probably have a different implementation
4$json = file_get_contents("http://ipinfo.io/$PublicIP/geo");
5//Breaks down the JSON object into an array
6$json = json_decode($json, true);
7//This variable is the visitor's county
8$country = $json['country'];
9//This variable is the visitor's region
10$region = $json['region'];
11//This variable is the visitor's city
12$city = $json['city'];