How to get Country State City by IP Address in PHP

  • Tech Area
  • October 24, 2023


I am working on a project where I need to show country, state and city of the user by taking his IP address. How do I get in PHP.

Solution

<?php 
$ip_address = $_SERVER['REMOTE_ADDR'];

$ipAPI='http://ip-api.com/php/'.$ip_address;
$resArr = unserialize(file_get_contents($ipAPI));

if($resArr)
{
     echo 'Your Country is ' . $resArr['country'];
     echo '<br />';
     echo 'Your State is ' . $resArr['region'];
     echo '<br />';
     echo 'Your City is ' . $resArr['city'];
}
?>


Subscribe us via Email

Join 10,000+ subscriber


SHORT QUERIES
How to Extract Numbers from String in PHP Last Updated: November 3, 2023
How to Replace Space with Underscore in PHP Last Updated: November 3, 2023
How to Concatenate two or more string in PHP Last Updated: October 24, 2023
How to get Yesterday and Tomorrow Date in PHP Last Updated: October 24, 2023
View all