我不太了解PHP,但这里我的问题:
如何在php脚本/页面(index.php)获取用户ip,检查它是否已经存在于txt文件(iplist.txt)。如果不存在,请在txt文件(iplist.txt)中插入IP,然后发送到main.html页面。
中的文件"(file)"是一个更容易理解的例子。
我已经编写了获取IP的代码,并尝试在文件上插入IP,但失败了lol。
include 'getip.php';
$user_ip = getUSerIP();
echo "<h2> ".$user_ip." </h2>";
这段代码是我在一个文件上测试是否得到用户id的代码。如果我执行它,它会告诉我页面上的用户IP。我正好可以做这部分。
问好。
您可以像这样获取用户的IP地址:
$ip = $SERVER['REMOTE_ADDR'];
//if user redirected from Proxy server then
if(!empty($_SERVER['HTTP_CLIENT_IP']))
{
$ip=$_SERVER['HTTP_CLIENT_IP'];
}
else if(!empty($_SERVER['HTTP_X_FORWARDED_FOR']))
{
$ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
}
获取IP后,您可以检查它是否存在:
$content=file_get_contents('iplist.txt');
if(strpos($content,$ip) !== false)
{
Header("Location:pay.html");
}
else
{
$content .= $ip."|^^|";
$byte = file_put_contents("iplist.txt",$content);
}