$_SERVER['REMOTE_ADDR'] 返回值 0
这是一个让我摸不着头脑的问题。大多数情况下,它会很好地返回 IP 地址,但有时会将其记录为“0”,这会导致我出现问题,因为我使用它进行投票。正如您在下面的代码中看到的,我将其转换为长格式并使用条件 if ip value == 0 then allowed to vote。我用 17 小时的饼干对它进行了双重保护,但如果我可以保持整洁,我想解决这个问题。另外,我认为有一些聪明的人会清除他们的cookie并多次投票。这是我正在使用的代码:
$ip = $_SERVER['REMOTE_ADDR'];
$iplong = ip2long($ip);
$fetch = mysql_query("SELECT * FROM ip WHERE ipaddress='$iplong' AND
url='$urlupdate'");
if(mysql_num_rows($fetch) == 0) {
$query = mysql_query("INSERT INTO ip (url,ipaddress) VALUES ('$urlupdate','$iplong')");
}
$update = "UPDATE pages SET counter = counter +1 WHERE url = '$urlupdate'";
$result = mysql_query($update);
就像我说的,大多数时候这就像一个魅力,并且 cron 作业每天清理一次,没有任何问题。起初我以为这是一个 noscript 问题,但它是通过 onclick 触发的。这意味着代码永远不会被解析,值不会被插入,并且本质上链接只是将它们带回结果页面而无需记录信息。我很困惑到底是什么原因造成的!
This is an issue that's left me scratching my head. Most of the time, it returns the IP address fine, but on occasion it logs it as "0" which causes me issues as I am using this for voting. As you can see in the code below, I switch it over into long form and use the conditional if ip value == 0 then allow to vote. I have it double protected with 17 hour cookies, but I'd like to solve this issue if I can just to be neat and tidy. Plus I think there are a few smart folks who clear their cookies and vote multiple times. Here's the code I'm using:
$ip = $_SERVER['REMOTE_ADDR'];
$iplong = ip2long($ip);
$fetch = mysql_query("SELECT * FROM ip WHERE ipaddress='$iplong' AND
url='$urlupdate'");
if(mysql_num_rows($fetch) == 0) {
$query = mysql_query("INSERT INTO ip (url,ipaddress) VALUES ('$urlupdate','$iplong')");
}
$update = "UPDATE pages SET counter = counter +1 WHERE url = '$urlupdate'";
$result = mysql_query($update);
Like I said, most of the time this works like a charm, and the cron job clears it up once a day with no issues. At first I thought it was a noscript issue, but it's triggered through an onclick. Meaning that code never gets parsed, values don't get inserted, and essentially the link just takes them back to the results page without ever logging info. I'm so stumped as to what could be causing this!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在 32 位 PHP 安装中使用 ip2long 很棘手。对于简单的 MySQL 操作,最好将 IP 作为字符串传递,并使用 MySQL 的本机 inet_ntoa/inet_aton 函数进行转换以进行存储。
然后
供以后检索。
Using ip2long in 32bit PHP installs is tricky. For simple MySQL operations, you're better off passing the IP around as a string and using MySQL's native inet_ntoa/inet_aton functions to convert for storage.
and then
for later retrieval.