PHP获取用户的真实ip地址
本文介绍如何使用PHP获取用户的真实ip,并且写入到txt文档
首先创建一个php文件,将下面代码放到PHP文件中
点击查看代码
<?php
function getIp() {
if (getenv("HTTP_CLIENT_IP") && strcasecmp(getenv("HTTP_CLIENT_IP"), "unknown"))
$ip = getenv("HTTP_CLIENT_IP");
else
if (getenv("HTTP_X_FORWARDED_FOR") && strcasecmp(getenv("HTTP_X_FORWARDED_FOR"), "unknown"))
$ip = getenv("HTTP_X_FORWARDED_FOR");
else
if (getenv("REMOTE_ADDR") && strcasecmp(getenv("REMOTE_ADDR"), "unknown"))
$ip = getenv("REMOTE_ADDR");
else
if (isset ($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] && strcasecmp($_SERVER['REMOTE_ADDR'],
"unknown"))
$ip = $_SERVER['REMOTE_ADDR'];
else
$ip = "unknown";
return ($ip);
}
//输出ip
$myfile = fopen("ip.txt", "a+") or die("Unable to open file!");
$txt = "ip:";
fwrite($myfile, $txt);
$txt = getIp();
fwrite($myfile, $txt);
$txt = "时间:";
fwrite($myfile, $txt);
$txt = date('Y-m-d h:i:s', time());
fwrite($myfile, $txt);
$txt = "\n";
fwrite($myfile, $txt);
fclose($myfile);
?>
然后在连接起来就可以了

浙公网安备 33010602011771号