New to our community ?

Discover a world of possibilities! Join us and explore a vibrant community where ideas flourish and connections thrive.

One of Our Valued Members

Thank you for being part of our community. Your presence enriches our shared experiences. Let's continue this journey together!

Home php codes Get user’s current location based on IP

Get user’s current location based on IP

Get user’s current location based on IP

Welcome back to, in this post today we will see how to find user’s location with having their IP address. so most of the time we need to access the user’s location to analysis the location wise client base.
In the below PHP script first of all we will fetch the user’s IP address by using pre-defined PHP function and store the IP address in a $ip_address variable.

so simply we will pass this IP address to our third party platform that will return as a JSON array that having country, country code, state, city , zip, time zone, longitude and latitude as well.
here we are performing this task with the help of curl functionality.

Get user current location based on IP
$ip_address = $_SERVER['REMOTE_ADDR'];
$url = "$ip_address";
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url);
$response = curl_exec($ch);
$arr_result = json_decode($response);

so we got this JSON response.

 "country":"United States",
 "isp":"Leaseweb USA",
 "org":"Cooley, Manion, Moore \u0026 Jones, LLP","as":"AS30633 Leaseweb USA, Inc.",

Also Read
How to Install PHP on CentOS.
How to Send Attachment on mail using PHP.

PHP Login Script With Remember me.
Unable to create a directory a wordpress error

How to integrate Razorpay Payment Gateway using PHP.
Change password using javascript, php and mysqli.
Password and Confirm Password Validation Using JavaScript