我是使用AJAX编程的新手,也是PHP编程的初学者。我不确定为什么,但是当用户点击一个箭头反复“快速投票”一个帖子时,PHP login_check决定用户不再登录。如果我以正常速度点击箭头,该程序有效,但是当我快速射击时,它变得很奇怪。
PHP代码:
<?php
include "db_connect.php";
include "functions.php";
sec_session_start();
我想知道这是否是一个确定的竞争条件,以及我可以做些什么来阻止它 -
AJAX代码:
$(document).ready(function() {
$("#upvotearrow").click(function() {
setTimeout(function() { }, 500);
$resdiv=$("#upvotedownvote_resultalert");
$content=$("#upvotedownvote_resultalert_content");
$.ajax({
type: "POST",
url: "../secure/process_upvotedownvote.php",
data: { vote: "upvote", poemid: $("#poemidfield").val() },
dataType:"HTML"
})
.done(function(param) {
if (param=="true_upvote") {
$content.html("Upvote registered!");
$resdiv.css("visibility", "visible");
}
else {
$content.html("Invalid request");
$resdiv.css("visibility", "visible");
}
});
});
$("#downvotearrow").click(function() {
setTimeout(function() { }, 500);
$resdiv=$("#upvotedownvote_resultalert");
$content=$("#upvotedownvote_resultalert_content");
$.ajax({
type: "POST", //POST data
url: "../secure/process_upvotedownvote.php", //Secure upvote/downvote PHP file
data: { vote: "downvote", poemid: $("#poemidfield").val() }, //Get type of vote and poem_id in URL
dataType:"HTML" //Set datatype as HTML to send back params to AJAX function
})
.done(function(param) { //Param- variable returned by PHP file
if (param=="true_downvote") {
$content.html("Downvote registered!");
$resdiv.css("visibility", "visible");
}
else {
$content.html("Invalid request");
$resdiv.css("visibility", "visible");
}
});
});
});
带有现场演示的网站can be viewed here.
要登录,只能使用此电子邮件:asdf@gmail.com和此密码:asdf123
提前感谢任何建议!
答案 0 :(得分:1)
我不知道竞争状况(除非每次都获得regenerated,否则可能但不应该破坏会话。)无论如何,如果我是你,我会在第一次点击后立即禁用upvote / downvote按钮。