How to Get the Visitors IP Address And Locations using PHP

get users ip location in php

 

Are you looking for Free PHP script that can retrieve users IP and location? if yes then your search is over. We just done creating the PHP script which can help you to get your visitors IP and location and other data like country code, region code and latitude, longitude and many others.

So in this PHP beginner tutorial you will learn how to get users IP address and locations using PHP. And also use other geolocation websites APIs to build this script its battery easy and it only takes few minutes to integrate with your project.

 It's very helpful when you create a PHP project that require users ip and locations you just need to add this script to get users infroamtion.

Download the full script ,Demo link is not working please try again later.
 
Demo Download

 

Getting IP data using Ipinfo API

To get the users IP location through IpInfo API you need to get the API and request for the location with the users IP. Currently you can send up to 1,000 daily requests which is free.

Ipinfo.io is web application website which helps you to get the user's location by users IP. it also provides monthly paid plan if you need to want more than 1,000 API requests a day you can.

First you need to get users IP by using $_SERVER['REMOTE_ADDR'] array or you can just your IP manually using variable and bind API url in another variable and pass it then you need to get the request IP data by using the file_get_contents() function which is help to read the contents of a file into a string and then you need to decode the json format data using the json_decode which converts the json format data it into a PHP variable.


<?php
// Get User IP
$ip = $_SERVER['REMOTE_ADDR'];
//Send the API request with user Ip
$ipinfoAPI = "http://ipinfo.io/{$ip}/json";
//get the APi requeted data
$load = file_get_contents($ipinfoAPI);
//Convert it to the readable format
$return = json_decode($load);
?>
Usage:
<?php
echo $return->ip;
echo $return->city;
echo $return->country;

print_r($return);
?>

 

Getting IP data using Freegeoip API

In this method we will do the same thing we did with above method. So we will use Freegeoip API to retrieve IP location and other data. freegeoip.net is a geolocation API web application for developers to get the users IP information such as city, country, time zone, latitude and longitude.

And you can send 10,000 queries per hour once this limit is reached, then you will redirected to the 403 error.

so the codes will be same as above codes, but we will change the API url to Freegeoip API and the rest of the code will be same. 


<?php
// Get User IP
$ip = $_SERVER['REMOTE_ADDR'];
//Send the API request with user Ip
$freegeoipAPI = "http://freegeoip.net/json/".$ip;
//get the APi requeted data
$load = file_get_contents($freegeoipAPI);
//Convert it to the readable format
$return = json_decode($load);
?>
Usage:
<?php
echo $return->ip;
echo $return->city;
echo $return->country;

print_r($return);
?>

 

If you like this tutorial and want more awesome free PHP scripts, then make sure to like and follow us on Twitter, Facebook.Twitter, Facebook to get latest updates from us.

  • Recommended: Creating Twitter Like Follow System Using PHP And JQuery
  • Recommended: How to Send Text Messages via PHP For Free


  • Comments

    Post a Comment