dqpu4988 2017-04-14 00:01
浏览 54
已采纳

too long

I'm using PHP in conjunction with AJAX for a lot of functions on my site. I've implemented a sort of rate limiting system on the client-side with Javascript. Basically, disabling a button for 1 sec after it's clicked.

Although this works fine for the more innocent cases, but I feel like I need something on the server-side of things to limit requests as well.

Basically, I want users to have a maximum amount of AJAX calls they can make per second. In fact, one per second seems reasonable.

One way would be somehow logging every request to my AJAX callback, and reading from that table before a new request is made. But this would immensely increase the work load on my server and database.

Are there any alternative methods of doing this?

PHP

function comment_upvote_callback() {    

    // Some sort of rate limit??


    // $_POST data validation

    // Add upvote to database

    // Return success or error

} 
add_action( 'wp_ajax_comment_upvote_callback', 'comment_upvote_callback' );

jQuery

var is_clicked = false;

if ( is_clicked == false ) {
    $('#comments').on('click', '.comment-upvote', function(event) {
        event.preventDefault();

        // Button disabled as long as isClicked == true
        isClicked = true;

        // Data to be sent
        var data = {
            'action': 'comment_upvote_callback',
            'security': nonce,  
            'comment_id': comment_id
        };

        // Send Data
        jQuery.post(ajaxurl, data, function(response) {

            // Callback

            // Client-side rate limit
            setTimeout( function() {
                is_clicked = false;
            }, 1000);
        });
    });
}
  • 写回答

1条回答 默认 最新

  • dongya5893 2017-11-15 09:29
    关注

    As already told above, you should be using rate limiting at server-side I have written a code to implement the same. You can copy the code into a file and simply include in your server-side script at the top. It accepts maximum 3hits/5secs. You can change the rate according to your needs

        session_start();
        const cap = 3;
        $stamp_init = date("Y-m-d H:i:s");
        if( !isset( $_SESSION['FIRST_REQUEST_TIME'] ) ){
                $_SESSION['FIRST_REQUEST_TIME'] = $stamp_init;
        }
        $first_request_time = $_SESSION['FIRST_REQUEST_TIME'];
        $stamp_expire = date( "Y-m-d H:i:s", strtotime( $first_request_time )+( 5 ) );
        if( !isset( $_SESSION['REQ_COUNT'] ) ){
                $_SESSION['REQ_COUNT'] = 0;
        }
        $req_count = $_SESSION['REQ_COUNT'];
        $req_count++;
        if( $stamp_init > $stamp_expire ){//Expired
                $req_count = 1;
                $first_request_time = $stamp_init;
        }
        $_SESSION['REQ_COUNT'] = $req_count;
        $_SESSION['FIRST_REQUEST_TIME'] = $first_request_time;
        header('X-RateLimit-Limit: '.cap);
        header('X-RateLimit-Remaining: ' . ( cap-$req_count ) );
        if( $req_count > cap){//Too many requests
                http_response_code( 429 );
                exit();
        }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP