$_SERVER['REMOTE_ADDR'] 返回值 0

发布于 2024-12-14 06:30:54 字数 785 浏览 2 评论 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

青萝楚歌 2024-12-21 06:30:54

在 32 位 PHP 安装中使用 ip2long 很棘手。对于简单的 MySQL 操作,最好将 IP 作为字符串传递,并使用 MySQL 的本机 inet_ntoa/inet_aton 函数进行转换以进行存储。

$ip = mysql_real_escape_string($_SERVER['REMOTE_ADDR']);
$sql = "INSERT INTO ... VALUES (INET_ATON('$ip'));";

然后

$sql = "SELECT INET_NTOA(ipaddress) AS ip ..."; 

供以后检索。

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.

$ip = mysql_real_escape_string($_SERVER['REMOTE_ADDR']);
$sql = "INSERT INTO ... VALUES (INET_ATON('$ip'));";

and then

$sql = "SELECT INET_NTOA(ipaddress) AS ip ..."; 

for later retrieval.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文