weixin_33713707 2011-04-22 13:12 采纳率: 0%
浏览 34

如何用PHP实现会话同步?

我正在用PHP开发WebApp。用户单击一个按钮,PHP代码就可以调用系统执行程序。由于WebApp使用AJAX,因此用户有可能单击两次按钮,或者实际上单击另一个按钮启动另一个exec进程。 我知道我可以编写一些JavaScript来禁用按钮,直到单个事件完成为止。但是,客户端强制执行很容易被覆盖。 PHP方面是否可以做一些事情来阻止这种事情的发生? 我有一个想法,让会话变量充当“信号量”,并且在执行每个脚本之前检查此变量,并且必须返回0才能继续脚本。 这样做正确吗?
  • 写回答

3条回答 默认 最新

  • weixin_33721344 2011-04-22 13:15
    关注

    You could use jQuery.ajax() and set the option async to false:

    async Default: true By default, all requests are sent asynchronously (i.e. this is set to true by default). If you need synchronous requests, set this option to false. Cross-domain requests and dataType: "jsonp" requests do not support synchronous operation. Note that synchronous requests may temporarily lock the browser, disabling any actions while the request is active

    You could use it like this:

    $.ajax({
        type: "POST",
        url: "file.php",
        async: false,
        data: data,
        success: function(return){
            alert("Success: "+return);
        }
    });
    

    If you want to add a loader just apply it as:

    startLoader();
    $.ajax({
        type: "POST",
        url: "file.php",
        async: false,
        data: data,
        success: function(return){
            alert("Success: "+return);
        }
    });
    endLoader();
    

    However you PHP idea is simply not a good one. Opening a session and do all the process for this kind of thing is just useless and slow your script down. You should be asking yourself: Do I really need to block this?

    If the answer is yes then do this: Create a table called processes in you database. Inside that table create 2 fields: One will be the process identifier: process_id; the second will be the process status: process_status. The first is an integer you will define with sha1(IMAGE). The second will be an integer: 1 for "busy", 0 for "free".

    Then you could do something like this:

    SELECT process_status FROM vps_processes WHERE process_id = sha1(CURRENT_IMAGE);
    

    And check whatever it is 1 or 0. If it is 1 then block the script; If it is 0 then you query:

    UPDATE vps_processes SET process_status = 1 WHERE process_id = sha1(CURRENT_IMAGE);
    

    then run what you have to run and at the end of the script query:

    UPDATE vps_processes SET process_status = 0 WHERE process_id = sha1(CURRENT_IMAGE);
    
    评论

报告相同问题?

悬赏问题

  • ¥30 vmware exsi重置后的密码
  • ¥15 易盾点选的cb参数怎么解啊
  • ¥15 MATLAB运行显示错误,如何解决?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题
  • ¥20 yolov5自定义Prune报错,如何解决?
  • ¥15 电磁场的matlab仿真
  • ¥15 mars2d在vue3中的引入问题
  • ¥50 h5唤醒支付宝并跳转至向小荷包转账界面