如何使用 HTML 检测访问者的 IP 地址?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8687642/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to detect the visitor's IP address using HTML?
提问by Surya KLSV
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!
如何使用 HTML 为我的网站检测访问者的 IP 地址?我有一个contactform.html 和一个formsent.html。当 formsent.html 将联系信息发送到我的电子邮件时,我也想查看他们的 IP 地址!
回答by Surya KLSV
You can't do it through HTML. However, you can find the IP address of a visitor through PHP.
你不能通过 HTML 来做到这一点。但是,您可以通过 PHP 找到访问者的 IP 地址。
$ip=$_SERVER['REMOTE_ADDR'];
回答by Michael
The following script may be useful to you. You can copy it and save it as {whateveryouwant}.php
以下脚本可能对您有用。您可以复制并另存为{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;
}
?>
回答by Kae Verens
You can't.
你不能。
HTML is a markup language, not a programming language. it doesn't "do" anything - it just structures content.
HTML 是一种标记语言,而不是一种编程语言。它不“做”任何事情——它只是构建内容。
You need to use a programming language, such as PHP, ASP, etc.
您需要使用一种编程语言,例如 PHP、ASP 等。
回答by Oded
You can't, not through HTML alone.
你不能,不能单独通过 HTML。
You need to use scripting that has the HTTP headers available to it.
您需要使用具有可用 HTTP 标头的脚本。
See this SO answerusing JSONP.
使用 JSONP查看此 SO 答案。
For PHP you would use $_SERVER['REMOTE_HOST']
.
对于 PHP,您将使用$_SERVER['REMOTE_HOST']
.
回答by Rich Bradshaw
HTML is a markup language, so it doesn't have any variables.
HTML 是一种标记语言,因此它没有任何变量。
If you want to get it using PHP, you'll need to make use of the $_SERVER superglobal variable. A solution could be:
如果您想使用 PHP 获取它,则需要使用 $_SERVER 超全局变量。一个解决方案可能是:
echo $_SERVER["REMOTE_ADDR"];
This actually gets the host ip, which would be your server.
echo $_SERVER["REMOTE_ADDR"];
这实际上获取主机 ip,这将是您的服务器。
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;
?}
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.
这会正确解析 HTTP_X_FORWARDED_FOR 字段并验证 IP 以确保其格式正确,而不是在私有块中。
回答by Naga Kotesh
$ip=$_SERVER['REMOTE_ADDR'];
$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
此命令显示服务器 ip 地址而不是用户本地 ip 地址,因为这是 php 命令符合服务器中的代码,因此此命令显示服务器 ip 地址