dongyupen6269 2013-07-07 04:52
浏览 27
已采纳

PHP / SQL错误:可能的竞争条件?

I'm a newbie at programming with AJAX and beginner at PHP programming. I'm not sure why, but when a user clicks an arrow to "Upvote" a post repeatedly and very fast, the PHP login_check decides that the user is no longer logged in. The program works if I click the arrow at a normal speed, but when I rapid fire it gets weird.

PHP code:

<?php

include "db_connect.php";
include "functions.php";

sec_session_start();

I was wondering if this is a definite case of race conditions and what I could do to prevent it--

AJAX code:

$(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");
        }
    });
});

    });

The website with a live demo can be viewed here.

TO LOG IN, JUST USE THIS EMAIL: asdf@gmail.com AND THIS PASSWORD: asdf123

Thanks in advance for any advice!

  • 写回答

1条回答 默认 最新

  • duannue2455 2013-07-07 05:07
    关注

    I don't know about the race condition (which is likely but shouldn't mess with the session unless it is getting regenerated every time). Anyway, if I were you, I would disable the upvote/downvote button right after the first click.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)