Detecting Ajax in PHP and making sure request was from my own website

前端 未结 7 1288
臣服心动
臣服心动 2020-11-29 23:31

I use my PHP back-end to detect AJAX requests by checking for a value in $_SERVER[\'HTTP_X_REQUESTED_WITH\'].

This gives me a reliable detection, making

相关标签:
7条回答
  • 2020-11-30 00:31

    David Walsh has a good solution

    /* decide what the content should be up here .... */
    $content = get_content(); //generic function;
    
    /* AJAX check  */
    if(!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {
        /* special ajax here */
        die($content);
    }
    
    /* not ajax, do more.... */
    
    0 讨论(0)
提交回复
热议问题