How to get Client Real IP address and HostName using PHP
What Will I Learn?
- You will learn how to get the real client IP
- You will learn how how to get hostname by IP
- You will learn how to display the IP and hostname in HTML element
Requirements
- You have basic about HTML
- You hava basic about PHP
- To practice this tutorial need a web server, text editor and a browser
Difficulty
- Basic
Tutorial Contents
PHP provide a function to get informantion client like IP and HostName. This allow us to know the detail information of our website visitor. The main information here is IP addres, when we know IP addres, then we can track the client location easyly. In this tutorial I will just explain how to get IP and hostname client.
Open your text editor and create a new file then save in your php sever as php extention, ex :
ip.php
. If you use XAMPP save it at xampp/htdoc. While you use Linux Apache Server save it at var/www.Start to code php
<?php
modify your code here . . . .
?>
- In PHP,
$_SERVER['REMOTE_ADDR']
is used to get client IP. But what happen if any client from Indonesia access you site via proxy server of Singapure. In this case $_SERVER[‘REMOTE_ADDR’] will return ip address of the Singapure. So we can't get the real Ip of our client. To solve this proplem we need do any check by using the condition. Firstly the condition if IP from share internet.
if (!empty($_SERVER["HTTP_CLIENT_IP"]))
{
$ip = $_SERVER["HTTP_CLIENT_IP"];
}
- The second conditon is to check the ip when the client use the proxy.
$_SERVER["HTTP_X_FORWARDED_FOR"]
can check the real IP from client although access from a proxy.
elseif (!empty($_SERVER["HTTP_X_FORWARDED_FOR"]))
{
$ip = $_SERVER["HTTP_X_FORWARDED_FOR"];
}
- the last conditon is when the user not use any proxy.
$_SERVER["REMOTE_ADDR"]
can handle it.
else
{
$ip = $_SERVER["REMOTE_ADDR"];
}
- Print the IP Addres in HTML element. This will print user's real IP Address does't matter if user using proxy or not.
echo "<center>
<h2>YOUR IP ADDRESS is ".$ip." </h2>
</center>";
- To get host name, we can use
gethostbyaddr()
function.
$host = gethostbyaddr($_SERVER['REMOTE_ADDR']);
- Display the host in HTML element
echo "<center>
<p>YOUR HostName is ".$host." </p>
</center>";
Tried also on my Phone
- Finish. We already get the IP address and the host name of client using PHP, The Information that you get you can save in your database if you need, so you have the the data all client who visit your website. Here the Full code :
<?php
if (!empty($_SERVER["HTTP_CLIENT_IP"]))
{
$ip = $_SERVER["HTTP_CLIENT_IP"];
}
elseif (!empty($_SERVER["HTTP_X_FORWARDED_FOR"]))
{
$ip = $_SERVER["HTTP_X_FORWARDED_FOR"];
}
else
{
$ip = $_SERVER["REMOTE_ADDR"];
}
echo "<center>
<h2>YOUR IP ADDRESS is ".$ip." </h2>
</center>";
$host = gethostbyaddr($_SERVER['REMOTE_ADDR']);
echo "<center>
<p>YOUR HostName is ".$host." </p>
</center>";
?>
Download File Here
Curriculum
- PHP Tutorial : Displaying Live STEEM price from bittrex Api on your website using file_get_contents () function
- How to make Steem Calculator Aplication (STEEM-IDR) using PHP and Javascript
Posted on Utopian.io - Rewarding Open Source Contributors
Thank you for the contribution. It has been approved.
You can contact us on Discord.
[utopian-moderator]
mantap bg.. sudah berhasil lewat utopian.io kayaxnya abg ya @sogata..
alhamdulillah dek. Berkat doa dan dukungan kalian semua
Mantap that bg, semoga beujeut ke ngen akrab tanyo, bah ta saleng upvote.
nyan ata droneuh kalheuh lon vote
amin
Assalamualaikum bang..Jet lon meuturi ngon droneh.lon neuk belajar utopian yan bang.
waalaikumsalam, ne dm ret discord. ana akun @sogata
Neubi nomor wa droneh bang
Hey @sogata I am @utopian-io. I have just upvoted you!
Achievements
Suggestions
Get Noticed!
Community-Driven Witness!
I am the first and only Steem Community-Driven Witness. Participate on Discord. Lets GROW TOGETHER!
Up-vote this comment to grow my power and help Open Source contributions like this one. Want to chat? Join me on Discord https://discord.gg/Pc8HG9x