Skip to Content
PHPCODE
get location from ip address javascript using php
oops code / September 9, 2021

Using geolocation, you can find out where a user is in relation to the world. The geolocation service, in particular, uses the IP address to determine the location of the user’s device. The IP address of the visitor is the first thing needed to track their location. We can determine the visitor’s location based on their IP address. An IP address can be obtained by using PHP’s $_SERVER variable. Based on the visitor’s IP address, PHP can determine the visitor’s latitude and longitude. Learn how to get a location from an IP address in this PHP tutorial.

By using the Geolocation API, you can find out a user’s location in real-time based on their IP address. It’s possible to get location information from an IP address using a free Geolocation API in PHP. IP Geolocation API is used in this example script in order to obtain the location, country, region, city, latitude, and longitude using PHP and the IP Geolocation API in order to do so.

Php code to get IP address of a user

To get the IP address of the current user in PHP, use the REMOTE ADDR index in $_SERVER.

$userIP = $_SERVER['REMOTE_ADDR'];

Using PHP, find the location of an IP address

The IP Geolocation API in PHP can be used to determine a user’s location from their IP address.

The API response contains a variety of information about geolocation. Many useful details can be found here:

<?php 
// IP address 
$userIP = '162.222.198.75'; 
// API end URL 
$apiURL = 'https://freegeoip.app/json/'.$userIP; 
// Create a new cURL resource with URL 
$ch = curl_init($apiURL); 
// Return response instead of outputting 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
// Execute API request 
$apiResponse = curl_exec($ch); 
// Close cURL resource 
curl_close($ch); 
// Retrieve IP data from API response 
$ipData = json_decode($apiResponse, true); 
if(!empty($ipData)){ 
$country_code = $ipData['country_code']; 
$country_name = $ipData['country_name']; 
$region_code = $ipData['region_code']; 
$region_name = $ipData['region_name']; 
$city = $ipData['city']; 
$zip_code = $ipData['zip_code']; 
$latitude = $ipData['latitude']; 
$longitude = $ipData['longitude']; 
$time_zone = $ipData['time_zone']; 
echo 'Country Name: '.$country_name.'<br/>'; 
echo 'Country Code: '.$country_code.'<br/>'; 
echo 'Region Code: '.$region_code.'<br/>'; 
echo 'Region Name: '.$region_name.'<br/>'; 
echo 'City: '.$city.'<br/>'; 
echo 'Zipcode: '.$zip_code.'<br/>'; 
echo 'Latitude: '.$latitude.'<br/>'; 
echo 'Longitude: '.$longitude.'<br/>'; 
echo 'Time Zone: '.$time_zone; 
}else{ 
echo 'IP data is not found!'; 
}
?>

The code can be organised in a function to make it easier to use.

It returns geolocation data from IP address using the following function: IPtoLocation().

<?php 
function IPtoLocation($ip){ 
$apiURL = 'https://freegeoip.app/json/'.$ip; 
// Make HTTP GET request using cURL 
$ch = curl_init($apiURL); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
$apiResponse = curl_exec($ch); 
if($apiResponse === FALSE) { 
$msg = curl_error($ch); 
curl_close($ch); 
return false; 
} 
curl_close($ch); 
// Retrieve IP data from API response 
$ipData = json_decode($apiResponse, true); 
// Return geolocation data 
return !empty($ipData)?$ipData:false; 
}

Invoke IPtoLocation() with the IP address as the first argument.

$userIP = '162.222.198.75'; 
$locationInfo = IPtoLocation($userIP);
Comments
Joan Styers says:

PLEASE FoRWARD THiS EMAiL To SoME0NE iN Y0UR C0MPANY WH0 iS ALLoWED To MAKE IMPORTANT DECiSi0NS!

We have hacked your website https://phpcodeinformation.com and extracted y0ur databases.

How did this happen?

0ur team has found a vulnerability within y0ur site that we were able to exploit. After finding the vulnerability we were able to get y0ur database credentials and extract y0ur entire database and move the inf0rmation to an 0ffsh0re server.

What does this mean?

We will systematically g0 thr0ugh a series 0f steps of totally damaging y0ur reputati0n. First y0ur database will be leaked or s0ld t0 the highest bidder which they will use with whatever their intentions are. Next if there are e-mails found they will be e-mailed that their information has been sold or leaked and y0ur site https://phpcodeinformation.com was at fault thusly damaging y0ur reputati0n and having angry customers/ass0ciates with whatever angry cust0mers/ass0ciates d0. Lastly any links that you have indexed in the search engines will be de-indexed based off 0f blackhat techniques that we used in the past to de-index our targets.

H0w do i stop this?

We are willing t0 refrain fr0m destroying your site’s reputation for a small fee. The current fee is $3000 in bitcoins (.16 BTC).

Please send the bitcoin to the f0ll0wing Bitcoin address (Make sure to c0py and paste):

3GZ89jpiwKaasUGWZ94dPZFFNhPDGuUDCs

0nce you have paid we will aut0matically get informed that it was your payment. Please note that you have to make payment within 5 days after receiving this e-mail or the database leak, e-mails dispatched, and de-index of your site WiLL start!

H0w d0 i get Bitcoins?

You can easily buy bitcoins via several websites 0r even offline from a Bitcoin-ATM.

What if i d0n’t pay?

if y0u decide n0t t0 pay, we will start the attack at the indicated date and uphold it until y0u do, there’s n0 counter measure to this, you will 0nly end up wasting more m0ney trying to find a s0lution. We will completely destr0y y0ur reputati0n amongst google and y0ur customers.

This is n0t a h0ax, do not reply to this email, don’t try to reason 0r neg0tiate, we will n0t read any replies. once you have paid we will st0p what we were doing and you will never hear from us again!

Please n0te that Bitcoin is an0nymous and no one will find out that you have complied.

Leave a Reply

Your email address will not be published.

PHPCODE © 2023