Friday, 13 February 2015

How to trace User IP Address using PHP & JavaScript?

In the last post of Web development I told you about the WordPress installation But Now Today we will talk about the PHP and JavaScript and you will learn how to trace User IP address using PHP and JavaScript.Here it is very necessary  to know the Computer’s IP address When you are going to secure your application.It is also necessary if you are wanted to keep user’s information for future use. You know it works really great.And today i will tell you about this command in PHP and you can get user’s IP address very easily.But you also have another option to use Javascript in order to get User’s IP address.But it is much better idea to get User’s IP address using PHP.So that you can easily use that Ip address later for verifying the data sent by the user.Especially , you can use that data i mean user’s IP to pass values projects like social Networking and eCommerce sites.But don’t worry today i will teach you both the methods (PHP & JavaScript) to get user’s IP very easily.Getting user ip address using php is very simple.It is also a quick way to get user ip address using php.

Detecting User IP Address using PHP?

If you are really wanted to get User ip address using PHP , there is so many commands that actually you can use to do it . But Now you can use this following code:
$_SERVER['REMOTE_ADDR]
It will just go active and detect the user IP adress in PHP who is visiting your web pages , But if you want some change in above command i mean you want  to echo or print the above listed command then just you can go and simply save the command local variable like $ip , Another command is here:
<?php
$ip = $_SERVER[‘REMOTE_ADDR];
echo $ip;
?>
It will show the simplest IP address if someone is visiting your site.However , you user might use the proxy or come in his/her real identity on your site.If any user come on your site with hiding his/her i mean using proxy then you have to use another command, Here we go with another command:
$ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
The above code that i mentioned in a box , It works on both situations because if anyone come on your site it will show his/her the real identity i mean the it show the real IP address using PHP 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;
}
?>

I will just recommend you very strongly that create a simple a function in php and get access to the real IP address of Users.You can simply use this code for doing so and it will check all the option for you and show you the real IP address of your users.You can see this code below:
<?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 PHP code do very simple job for you and get the real IP address of the users.It will display on the page.You can also check by using yourself.Now we see the javaScript to get the user IP address.

Detecting User IP Address using Jquery?

After getting the user IP address using PHP now we started using Jquery for getting user ip adress . Yes you can easily get access on the User’s IP Address using Jquery and JavaScript.But keep in mind that PHP code was your own function which did the job for you but if you are going to use jquery then it will be a third party service which will work for you.Here i have two website that give you the free service to use Jquery and get the IP address easily.
  • jsonip.com
  • smart-ip.net
The first website that i mentioned uses the Json technology and the second one uses the simple way i mean Jquery for getting the IP address.
Getting the User IP Address using the Jsonip.com: 
<script>
$(document).ready(function () {
$.get(‘http://jsonip.com’, function (res) {
$(‘p’).html(‘IP Address is: ‘ + res.ip);
});
});</script>
Getting the User IP Address using the smat-ip.net: 
<script>
$(document).ready(function () {
$.getJSON(‘http://smart-ip.net/geoip-json?callback=?’,
function(data) {$(‘p’).html
(‘My IP Address is: ‘ + data.host);
});
});
</script>
So i mentioned some method above for getting IP Address i shared that how to get user IP Address using php & JavaScript.we will meet again in new Web development tutorials.Feel free to share this article with your friends and family  that everyone can get benefit from it.If you have any question then ask your question in comment box.Take care!

No comments:

Post a Comment