我有一个评级脚本,可以记录用户的IP,因此他们无法对文件进行多次评级。但是,每次我对文件进行评级测试时,我的$ ip都会改变ip2long()函数。这是怎么回事?这是我的代码:
<?php
require('connect.php');
$sheetID = $_GET["id"];
$rating = $_GET["rating"];
$ip= $_SERVER['REMOTE_ADDR'];
$ip = ip2long($ip);
$checkIPQuery = mysql_query("SELECT * FROM ratings WHERE sheetID = '$sheetID' and INET_NTOA(ip) = '$ip'");
$doesIPExist = mysql_num_rows($checkIPQuery);
if ($doesIPExist == 0) {
mysql_query("INSERT INTO ratings (sheetID, rating, ip) VALUES ($sheetID, $rating, $ip)");
echo " $ip
<script>setTimeout(function(){location.href = '../sheets.php?id=$sheetID'},1);</script>";
}
else
echo 'You have already rated this image!';
?>
然后检查用户是否对脚本进行了评分:
$id = $_GET['id'];
$ip=$_SERVER['REMOTE_ADDR'];
$ip = ip2long($ip);
$checkIPQuery = mysql_query("SELECT * FROM ratings WHERE sheetID = '$id' and INET_NTOA(ip) = '$ip'");
$doesIPExist = mysql_num_rows($checkIPQuery);
这就是我所做的:
任何帮助将不胜感激。谢谢!
编辑:如果我将$ ip设置为3,这些脚本可以正常工作。
答案 0 :(得分:1)
在阅读了评论和一些研究之后,我找到了答案并且很简单。
在32位系统上ip2long()
返回负整数和正整数,但INET_NTOA()
仅适用于正整数。所以你有两个选择来解决这个问题:
$ip = sprintf('%u', ip2long($_SERVER['REMOTE_ADDR']));
... AND ip = " . ip2long($_SERVER['REMOTE_ADDR']) . " ...
你可以这样验证这个事实:
$ip = $_SERVER['REMOTE_ADDR'];
$int = ip2long($ip);
var_dump($ip, $int, sprintf('%u', $int));
这会产生以下输出:
string '192.168.1.120' (length=13)
int -1062731400
string '3232235896' (length=10)
然后转到MySQL:
mysql> SELECT '192.168.1.120', INET_NTOA(-1062731400), INET_NTOA(3232235896)
+---------------+------------------------+-----------------------+
| 192.168.1.120 | INET_NTOA(-1062731400) | INET_NTOA(3232235896) |
+---------------+------------------------+-----------------------+
| 192.168.1.120 | NULL | 192.168.1.120 |
+---------------+------------------------+-----------------------+
1 row in set (0.00 sec)