【发布时间】:2017-12-13 08:23:12
【问题描述】:
我正在开发一个提供评级的简单网络应用程序。用户只能使用其 IP 地址投票一次。然而,问题是有多个要评分的东西,但是每当用户在一个字段中输入他的评分时,他就不能在其他评分中输入评分。我希望我的 PHP 文件知道我只想为一个评分验证 IP 地址,而用户可以对其他内容进行评分。
这是我的 JQuery 代码:
<h1>Demo 1</h1>
<script>
$(document).ready(function () {
$("#demo1 .stars").click(function () {
var id = 1;
$.post('rating.php', {rate:$(this).val()}, function(d) {
if (d>0) {
alert('You already rated');
} else {
alert('Thanks For Rating');
}
});
$(this).attr("checked");
});
});
</script>
<fieldset id='demo1' class="rating">
<input class="stars" type="radio" id="star10" name="rating" value="10" />
<label class = "full" for="star10" title="Very nice - 10/10"></label>
<input class="stars" type="radio" id="star9" name="rating" value="9" />
<label class = "full" for="star9" title="Great - 9/10"></label>
<input class="stars" type="radio" id="star8" name="rating" value="8" />
<label class = "full" for="star8" title="Great - 8/10"></label>
<input class="stars" type="radio" id="star7" name="rating" value="7" />
<label class = "full" for="star7" title="Good - 7/10"></label>
<input class="stars" type="radio" id="star6" name="rating" value="6" />
<label class = "full" for="star6" title="Good - 6/10"></label>
<input class="stars" type="radio" id="star5" name="rating" value="5" />
<label class = "full" for="star5" title="Meh - 5/10"></label>
<input class="stars" type="radio" id="star4" name="rating" value="4" />
<label class = "full" for="star4" title="Meh - 4/10"></label>
<input class="stars" type="radio" id="star3" name="rating" value="3" />
<label class = "full" for="star3" title="Sucks - 3/10"></label>
<input class="stars" type="radio" id="star2" name="rating" value="2" />
<label class = "full" for="star2" title="Sucks - 2/10"></label>
<input class="stars" type="radio" id="star1" name="rating" value="1" />
<label class = "full" for="star1" title="Sucks - 1/10"></label>
</fieldset>
id = demo2, demo3 等有多个这样的字段集。我想要每个演示的唯一标识符或变量。
这就是我的 PHP 处理请求并将其发送到数据库的方式:
if (isset($_POST['rate']) && !empty($_POST['rate'])) {
$rate = $conn->real_escape_string($_POST['rate']);
$sql = "SELECT `id` FROM `tbl_rating` WHERE `user_id`='" . $ipaddress . "'";
$result = $conn->query($sql);
$row = $result->fetch_assoc();
if ($result->num_rows > 0) {
echo $row['id'];
} else {
$sql = "INSERT INTO `tbl_rating` ( `rate`, `user_id`) VALUES ('" . $rate . "', '" . $ipaddress . "'); ";
if (mysqli_query($conn, $sql)) {
echo "0";
}
}
}
我只能想到一种解决方案,它可以在点击时触发一个标志,1 代表 demo1,2 代表 demo2,3 代表 demo3,等等。
【问题讨论】:
-
使用 IP 作为标识符通常是一种不好的方法,因为通常有很多人在一个 IP 后面(公司/家庭/任何开放的 wifi 等)。无论如何,您需要有一个评级表,其中包含对象(该人正在评级的内容)、评级和标识符。然后,您只需检查此人是否对特定对象进行了评分。
-
嘿马格努斯,感谢您的评论。如果我不希望人们投票两次,我可以使用其他验证器吗?我也不希望他们创建新帐户。另外,我同意一个 IP 地址后面的人投票一次。我只希望使用该 IP 地址对每个字段集进行投票。现在,我的 PHP 使用它来检查用户是否已经投票:
$sql = "SELECTid` FROMtbl_ratingWHEREuser_id='" . $ipaddress . "'";` @MagnusEriksson -
如果你想阻止人们多次评分,你需要一个合适的标识符,而匿名用户根本无法做到这一点。您将需要创建一个帐户(并通过电子邮件或类似方式验证)或使用 facebook connect。这两种方法都不是防弹的,但通常足够好。
-
您可能会接受它,但您的用户会接受吗?
-
我刚刚检查了您编辑的评论,这就是我想做的,但我不知道该怎么做。
标签: javascript php jquery ajax mysqli