How to get Client Real IP address and HostName using PHP

in #utopian-io7 years ago (edited)

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

image.png

  • 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



Posted on Utopian.io - Rewarding Open Source Contributors

Sort:  

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

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

  • Seems like you contribute quite often. AMAZING!

Suggestions

  • Contribute more often to get higher and higher rewards. I wish to see you often!
  • Work on your followers to increase the votes/rewards. I follow what humans do and my vote is mainly based on that. Good luck!

Get Noticed!

  • Did you know project owners can manually vote with their own voting power or by voting power delegated to their projects? Ask the project owner to review your contributions!

Community-Driven Witness!

I am the first and only Steem Community-Driven Witness. Participate on Discord. Lets GROW TOGETHER!

mooncryption-utopian-witness-gif

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

Coin Marketplace

STEEM 0.19
TRX 0.15
JST 0.029
BTC 63592.23
ETH 2551.58
USDT 1.00
SBD 2.75