7

How can i detect the visitors IP Address using HTML for my website? I have a contactform.html and a formsent.html. And when formsent.html sends the contact info to my email i also want to see their IP Address!

gman
  • 100,619
  • 31
  • 269
  • 393
  • 2
    You don't get this from the HTML (or JavaScript). You get it from the script that processes contactform.html. – Ben Lee Dec 31 '11 at 10:45

7 Answers7

13

You can't do it through HTML. However, you can find the IP address of a visitor through PHP.

$ip=$_SERVER['REMOTE_ADDR'];
SpruceMoose
  • 9,737
  • 4
  • 39
  • 53
Surya KLSV
  • 1,260
  • 4
  • 18
  • 26
8

The following script may be useful to you. You can copy it and save it as {whateveryouwant}.php

<?php

echo "Your IP is";

echo $_SERVER["REMOTE_ADDR"];

function get_ip_address() {
  // check for shared internet/ISP IP
  if (!empty($_SERVER['HTTP_CLIENT_IP']) && $this->validate_ip($_SERVER['HTTP_CLIENT_IP']))
   return $_SERVER['HTTP_CLIENT_IP'];

  // check for IPs passing through proxies
  if (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
   // check if multiple ips exist in var
    $iplist = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']);
    foreach ($iplist as $ip) {
     if ($this->validate_ip($ip))
      return $ip;
    }
   }

  if (!empty($_SERVER['HTTP_X_FORWARDED']) && $this->validate_ip($_SERVER['HTTP_X_FORWARDED']))
   return $_SERVER['HTTP_X_FORWARDED'];
  if (!empty($_SERVER['HTTP_X_CLUSTER_CLIENT_IP']) && $this->validate_ip($_SERVER['HTTP_X_CLUSTER_CLIENT_IP']))
   return $_SERVER['HTTP_X_CLUSTER_CLIENT_IP'];
  if (!empty($_SERVER['HTTP_FORWARDED_FOR']) && $this->validate_ip($_SERVER['HTTP_FORWARDED_FOR']))
   return $_SERVER['HTTP_FORWARDED_FOR'];
  if (!empty($_SERVER['HTTP_FORWARDED']) && $this->validate_ip($_SERVER['HTTP_FORWARDED']))
   return $_SERVER['HTTP_FORWARDED'];

  // return unreliable ip since all else failed
   return $_SERVER['REMOTE_ADDR'];
 }

function validate_ip($ip) {
     if (filter_var($ip, FILTER_VALIDATE_IP, 
                         FILTER_FLAG_IPV4 | 
                         FILTER_FLAG_IPV6 |
                         FILTER_FLAG_NO_PRIV_RANGE | 
                         FILTER_FLAG_NO_RES_RANGE) === false)
         return false;
     self::$ip = $ip;
     return true;
 }
?>
Troy Alford
  • 26,660
  • 10
  • 64
  • 82
Michael
  • 81
  • 1
  • 1
  • Umm just a small doubt (im kinda new to this), After I created the php file, is there any other extra step to be done like linking the HTML page to the php file or something? or just create the file in the website's directory and your good to go?? – Esvin Joshua May 07 '22 at 12:47
3

You can't.

HTML is a markup language, not a programming language. it doesn't "do" anything - it just structures content.

You need to use a programming language, such as PHP, ASP, etc.

Kae Verens
  • 4,076
  • 3
  • 21
  • 41
2

You can't, not through HTML alone.

You need to use scripting that has the HTTP headers available to it.

See this SO answer using JSONP.

For PHP you would use $_SERVER['REMOTE_HOST'].

Community
  • 1
  • 1
Oded
  • 489,969
  • 99
  • 883
  • 1,009
1

$ip=$_SERVER['REMOTE_ADDR'];

this command shows server ip address not user local ip address because this is php command is complies the code in server because of that reason this command show server ip address

1

HTML is a markup language, so it doesn't have any variables.

If you want to get it using PHP, you'll need to make use of the $_SERVER superglobal variable. A solution could be:

echo $_SERVER["REMOTE_ADDR"];

This actually gets the host ip, which would be your server.

echo $_SERVER["REMOTE_ADDR"];

This is the most basic however, and fails if the user is behind a proxy, as well as allowing them to trivially change it. A much much better method is to using something like:

function get_ip_address() {
  // check for shared internet/ISP IP
  if (!empty($_SERVER['HTTP_CLIENT_IP']) && $this->validate_ip($_SERVER['HTTP_CLIENT_IP']))
   return $_SERVER['HTTP_CLIENT_IP'];

  // check for IPs passing through proxies
  if (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
   // check if multiple ips exist in var
    $iplist = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']);
    foreach ($iplist as $ip) {
     if ($this->validate_ip($ip))
      return $ip;
    }
   }

  if (!empty($_SERVER['HTTP_X_FORWARDED']) && $this->validate_ip($_SERVER['HTTP_X_FORWARDED']))
   return $_SERVER['HTTP_X_FORWARDED'];
  if (!empty($_SERVER['HTTP_X_CLUSTER_CLIENT_IP']) && $this->validate_ip($_SERVER['HTTP_X_CLUSTER_CLIENT_IP']))
   return $_SERVER['HTTP_X_CLUSTER_CLIENT_IP'];
  if (!empty($_SERVER['HTTP_FORWARDED_FOR']) && $this->validate_ip($_SERVER['HTTP_FORWARDED_FOR']))
   return $_SERVER['HTTP_FORWARDED_FOR'];
  if (!empty($_SERVER['HTTP_FORWARDED']) && $this->validate_ip($_SERVER['HTTP_FORWARDED']))
   return $_SERVER['HTTP_FORWARDED'];

  // return unreliable ip since all else failed
  return $_SERVER['REMOTE_ADDR'];
 }

function validate_ip($ip) {
     if (filter_var($ip, FILTER_VALIDATE_IP, 
                         FILTER_FLAG_IPV4 | 
                         FILTER_FLAG_IPV6 |
                         FILTER_FLAG_NO_PRIV_RANGE | 
                         FILTER_FLAG_NO_RES_RANGE) === false)
         return false;
     self::$ip = $ip;
     return true;
 }

(http://stackoverflow.com/questions/1634782/what-is-the-most-accurate-way-to-retrieve-a-users-correct-ip-address-in-php)

This correctly parses the HTTP_X_FORWARDED_FOR field as well as validating the IP to make sure it's of the right format, and not in a private block.

Rann Lifshitz
  • 4,040
  • 4
  • 22
  • 42
Rich Bradshaw
  • 71,795
  • 44
  • 182
  • 241
  • This also covers the possibility of a user trivially being able to spoof their address. Also, REMOTE_ADDR will always be set, so the rest is never used. – deceze Dec 31 '11 at 11:34
  • You are right. I've changed it round, so that it checks the forwarding first, then the others. In fact, this whole answer is crap. I'm rewriting it to be better. – Rich Bradshaw Dec 31 '11 at 11:39
  • Should be the same for most users, but will distinguish between users behind a proxy instead of putting the proxy IP there. That means if you blocked by IP, you wouldn't accidentally block a whole company. – Rich Bradshaw Dec 31 '11 at 11:50
  • But what is the value like before it was $ip what is it now? –  Dec 31 '11 at 11:53
  • OK, have removed the errant }. Try again! – Rich Bradshaw Dec 31 '11 at 12:21
0

If you can use JQuery but have no access to PHP, Ipify might by useful. It is free and there is no limit to the number of requests

<script type="application/javascript">
  function getIP(json) {
    document.write("My public IP address is: ", json.ip);
  }
</script>

<script type="application/javascript" src="https://api.ipify.org?format=jsonp&callback=getIP"></script>
Dharman
  • 30,962
  • 25
  • 85
  • 135
Ansel Lee
  • 43
  • 1
  • 7