如何使用HTML为我的网站检测访客IP地址? 我有一个contactform.html和一个formsent.html。 当formsent.html将联系信息发送到我的电子邮件时,我也希望看到他们的IP地址!
答案 0 :(得分:12)
你不能通过HTML来实现。但是,您可以通过PHP找到访问者的IP地址。
$ip=$_SERVER['REMOTE_ADDR'];
答案 1 :(得分:8)
以下脚本可能对您有用。您可以将其复制并保存为{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;
}
?>
答案 2 :(得分:3)
你不能。
HTML是一种标记语言,而不是编程语言。它没有“做”任何事情 - 它只是构建内容。
您需要使用编程语言,例如PHP,ASP等。
答案 3 :(得分:2)
答案 4 :(得分:1)
HTML是一种标记语言,因此它没有任何变量。
如果你想使用PHP来获取它,你需要使用$ _SERVER超全局变量。解决方案可能是:
echo $_SERVER["REMOTE_ADDR"];
这实际上是主机ip,这将是你的服务器。
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;
}
这正确解析HTTP_X_FORWARDED_FOR字段以及验证IP以确保其格式正确,而不是在私有块中。
答案 5 :(得分:0)
$ ip = $ _ SERVER ['REMOTE_ADDR'];
此命令显示服务器IP地址而不是用户本地IP地址,因为这是php命令,因此该命令符合服务器中的代码,因此此命令显示服务器IP地址
答案 6 :(得分:0)
如果您可以使用 JQuery 但无法访问 PHP,则 Ipify 可能有用。它是免费的,并且没有请求数量的限制
map = [ ['**', ' ', ' ', 'E0', ' ', '??', 'oo', '??', ' ', 'oo', ' ', 'W5'],
[' ', 'oo', 'oo', 'oo', ' ', 'oo', 'oo', 'oo', ' ', 'oo', ' ', 'oo'],
['E3', 'oo', 'W4', 'oo', ' ', 'W1', 'H1', 'oo', ' ', 'oo', ' ', 'oo'],
[' ', 'H3', ' ', 'oo', ' ', 'oo', 'oo', 'oo', ' ', 'oo', ' ', '??'],
['oo', 'oo', 'oo', 'oo', ' ', 'oo', 'W3', ' ', ' ', 'oo', ' ', 'oo'],
['??', ' ', ' ', ' ', ' ', ' ', 'E2', 'oo', 'E4', ' ', ' ', '??'],
['oo', 'oo', 'oo', ' ', 'oo', 'oo', ' ', 'oo', ' ', 'oo', 'oo', 'oo'],
['E1', ' ', ' ', ' ', 'oo', ' ', ' ', 'oo', ' ', ' ', ' ', ' '],
['H2', 'oo', 'oo', 'oo', 'oo', ' ', 'oo', 'oo', 'oo', 'oo', 'oo', ' '],
[' ', 'oo', ' ', ' ', ' ', ' ', 'oo', ' ', ' ', ' ', ' ', ' '],
[' ', 'oo', ' ', 'oo', ' ', 'oo', 'oo', ' ', 'oo', 'oo', 'oo', 'oo'],
[' ', ' ', ' ', 'W2', '??', 'oo', 'H4', ' ', ' ', ' ', ' ', 'EB'] ]
for x in range(len(map)):
print (map[x]),