How to Get User IP Address Using PHP?

Getting the user computer’s IP address becomes very necessary when you are either securing your application or you want to keep users’ information for future use. In PHP you can use a very simple command to get user’s IP address, but you can also use JavaScript in order to get user’s IP. However, it’s better to get the IP address using PHP so that you can use it later on by verifying other data sent by the user. Especially, you can use user IP to pass values in projects like eCommerce and Social Networking site. But I’ll teach you both the methods (PHP & JavaScript) to get user IP very easily.

user-ip-address

Before reading this tutorial you might also like following: 

Detecting User IP Address Using PHP?

In order to get user IP address in PHP, there is a very simple command you can use to do it with the following code:

$_SERVER[‘REMOTE_ADDR]

This will simply pick up the IP address of the user who is visiting the web page, if you want to echo or print the above command then simply save the command in a local variable something like $ip, you can do something like this:

<?php

$ip = $_SERVER[‘REMOTE_ADDR];

echo $ip;

?>

It will simply show you the IP address on the page who is visiting the page. However, the user might visit your page using a proxy or a connection with “Anonymous” identity, in that case you can use another command to get the REAL IP ADDRESS, here is it:

$ip = $_SERVER[‘HTTP_X_FORWARDED_FOR’];

The above code will get the IP address of a user if that’s behind a proxy, but you don’t know whether the user is using a proxy or not, so simply you need to put both commands together in order to get the REAL IP ADDRESS, so you should add this code:

<?php

$ip = $_SERVER[‘REMOTE_ADDR];

$ip_proxy = $_SERVER[‘HTTP_X_FORWARDED_FOR’];

if(!empty($_SERVER[‘REMOTE_ADDR)){

echo $ip;

}

if(!empty($_SERVER[‘HTTP_X_FORWARD_FOR’])){

echo $ip_proxy;

}

?>

The above code will check for both normal IP and an IP behind a proxy and will show the real IP address on the page.

I’ll recommend you to create a simple function in PHP and get the real ip address of the user, you can use this simple code to do that very easily, it will check all options for real IP address and will display it on the page:

<?php
function getIp() {
$ip = $_SERVER[‘REMOTE_ADDR’];
if (!empty($_SERVER[‘HTTP_CLIENT_IP’])) {
$ip = $_SERVER[‘HTTP_CLIENT_IP’];
} elseif (!empty($_SERVER[‘HTTP_X_FORWARDED_FOR’])) {
$ip = $_SERVER[‘HTTP_X_FORWARDED_FOR’];
}return $ip;
}?>

The above does a very simple job by getting the real ip of the user and it will display that on the page, you can test it yourself. Now after the PHP, we’ll use JavaScript to get user ip.

Getting User IP Address Using jQuery?

You can also get the IP of the user using jQuery and JavaScript, but as in PHP it’s your own function or code which does the job for you, but using jQuery you have to use a third party service which will do the job for you, there are two websites which give you a free service to use jQuery and get the IP address:

  • jsonip.com
  • smart-ip.net

The first website uses the jSon technology to get the IP and the second one simply uses jQuery.

Getting user IP using jsonip.com: 

<script>
$(document).ready(function () {
$.get(‘http://jsonip.com’, function (res) {
$(‘p’).html(‘IP Address is: ‘ + res.ip);
});
});
</script>

And you can get IP address using smart-ip.com: 

<script>
$(document).ready(function () {
$.getJSON(‘http://smart-ip.net/geoip-json?callback=?’, function(data) {
$(‘p’).html(‘My IP Address is: ‘ + data.host);
});
});
</script>
So the above methods were for getting ip address using both JavaScript & PHP, let’s meet you soon with another tutorial on web development, feel free to share this tutorial with all of your friends and ask your questions in comments if you have any.

About Abdul Wali

Abdul Wali is a professional blogger, marketer and web developer working online for last 5 years, he's been deeply involved in PHP, Web Development, SEO and computer video tutorials in different languages i.e English, Urdu and Pashto. You can catch him on and add him on Facebook.

Check Also

web hosting

The Best Web Hosting Providers (Tutorial)

Web hosting is one of the important aspects for establishing a business or website online. If ...

  • That’s good and really useful to store users Ip address and put them on work to check sessions and cookies for the same IP, Anyways nice (Y) Liked it!

    • Thanks buddy for comment, I’m trying my best to publish something really good!

  • yasir inam

    sir ads show nh ho rahin hy mery blog p

  • Using this method Can we get users IP in email form?