您好我有以下代码,我正在尝试调整以确保当登录的用户点击了Like时,在预定的时间段内只能发送一个可以调整的请求,即可以单击并且每5分钟仅发送一次请求。必须有一个我可以使用的javascript函数,但我无法弄明白。
的index.php:
<?php
include 'init.php';
include 'connect.php';
?>
<!doctype html>
<html>
<body>
<?php
$userid = $_SESSION['user_id'];
echo '<a class="like" href="#" onclick="like_add(', $userid,');">Like</a>';
?>
<script type ="text/javascript" src="jquery-1.11.1.min.js"></script>
<script type ="text/javascript" src="like.js"></script>
</body>
</html>
connect.php:
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "DB";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
?>
的init.php:
<?php
session_start();
$_SESSION['user_id']='1';
$userid = $_SESSION['user_id'];
include 'connect.php';
include 'like.php';
?>
like.js:
function like_add(userid) {
$.post('like_add.php', {userid:userid}, function(data) {
if (data == 'success'){
add_like($userid);
} else {
alert(data);
}
});
}
like.php:
<?php
function add_like($userid) {
include 'connect.php';
$stmt = $conn->prepare("INSERT INTO clicks (user) VALUES (?)");
$stmt->bind_param("s", $userid);
$stmt->execute();
$stmt = $conn->prepare("SELECT max(id) FROM clicks WHERE user=?");
$stmt->bind_param("s", $userid);
$stmt->execute();
$stmt->bind_result($click);
$stmt->fetch();
echo $click;
$stmt->close();
}
?
like_add.php
<?php
include 'init.php';
if (isset($userid)) {
$userid = $userid;
add_like($userid);
}
?>
答案 0 :(得分:0)
如果你将a标签传递给like_add,就像这样:
<?php
$userid = $_SESSION['user_id'];
echo '<a class="like" href="#" onclick="like_add(this, '.$userid.');">Like</a>';
?>
您可以在javascript函数中将其禁用一段时间:
function like_add(a, userid) {
a.disabled = true;
setTimeout(function(){
a.disabled = false;
), 5000});//Code will execute in 5 seconds to enable the a tag
$.post('like_add.php', {userid:userid}, function(data) {
if (data == 'success'){
add_like($userid);
}else{
alert(data);
}
});
}
这是否符合您所寻找的内容?
答案 1 :(得分:0)
以完全不推荐的方式,您可以定义
var isLikeable = true;
function likeStatus(secs) {
isLikeable = false;
window.setTimeout("isLikeable = true;", (secs*60));
}
然后,当点击&#34;喜欢&#34;你会检查
if (isLikeable) { // do like and call likeStatus(300); }
答案 2 :(得分:0)
您是否需要在后端强制执行此操作,或者通过UI禁用它?理论上,你可以使用开发人员工具或Firebug来攻击它,但这对于临时用户来说似乎不太可能。
在前端,我会使用jQuery在click事件上添加代码,禁用&#34; Like&#34;超时按钮,如下所示:
// First, move your click handler to your script file
$('.like').click( function(event) {
// Make sure clicking the button doesn't submit any form context that might be present
event.preventDefault();
var DURATION_IN_MINUTES = 5;
// Grab the user id that will be added as an attribute of your element: data-user-id="$userid"
var user_id = $(this).data('userID');
// Run your function
like_add(user_id);
// Disable clicking like (it's easiest to do this with a button, by far
$(this).prop({'disabled':true,'title':'Disabled for ' + DURATION_IN_MINUTES + ' minutes');
// Set a timer to re-enable the like button in 5 minutes
setTimeout( function() { $(this).prop({'disabled':false,'title':''}), DURATION_IN_MINUTES * 60 * 1000}
});
然后在你的HTML中:
echo '<a class="like" href="#" onclick="like_add(', $userid, ');">Like</a>';
变为
echo '<button class="like" data-user-id="' + $userid + '">Like</button>';
我没有PHP环境来测试这一点(说实话,我还没有测试过JavaScript本身),但这应该足以让你开始。
如果您想阻止后端的多个提交,您需要为喜欢的时间戳添加时间戳,然后查看上一次给定用户&#34;喜欢&#34;那个链接。