Detect AJAX Request

The HTTP_X_REQUESTED_WITH header is sent by all recent browsers that support AJAX requests.

if ( !empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest' )
       # Ex. check the query and serve requested data


  1. User Avatar
    Permalink to comment#

    I think isset() is more appropriate than !empty() in this situation. Still a useful snippet though. :)

    • User Avatar
      Cameron Spear
      Permalink to comment#

      Pretty much the very first thing empty does is to make sure isset is false. Using isset and empty is redundant in this manner. empty() on an unset var will always return false (and there will be no error…).

      From on empty() (

      Determine whether a variable is considered to be empty. A variable is considered empty if it does not exist or if its value equals FALSE. empty() does not generate a warning if the variable does not exist.

  2. User Avatar
    Permalink to comment#

    So that you don’t get the notice about whether the variable is set or the attribute is set you should use both isset() and !empty().

    if (isset($_SERVER['HTTP_X_REQUESTED_WITH'] && !empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest')
    # Ex. check the query and serve requested data

    • User Avatar
      Cubicle Generation
      Permalink to comment#

      You’re missing the close parenthesis on the isset, teehee.

      *syntax whore*

  3. User Avatar
    Permalink to comment#

    great and practical snip, however, I’d like to know if there is a great or important disadvantage in using this approach when working with synchronized(normal request) and asynchronized(ajax request) requests in the same page.

  4. User Avatar
    Permalink to comment#

    The statement “The HTTP_X_REQUESTED_WITH header is sent by all recent browsers that support AJAX requests.” is 100% false.

    The browser does not send this header, javascript libraries do. Libraries like jquery, prototype and mootools.

  5. User Avatar
    Permalink to comment#

    Hi guys,

    I want to check availability of internet connection before every AJAX request. If the connection is available – proceed else give a pop up message- net not available.

    I wrote the javascript to check net connection.But to check if a request is a AJAX request or a normal request if put following filter in my javaScript code:

    if (isset($_SERVER[‘HTTP_X_REQUESTED_WITH’] && !empty($_SERVER[‘HTTP_X_REQUESTED_WITH’]) && strtolower($_SERVER[‘HTTP_X_REQUESTED_WITH’]) == ‘xmlhttprequest’))

    I got following error

    $_SERVER is not defined

    Can anyone correct me, if I am going wrong.

    • User Avatar
      Permalink to comment#

      @Suresh, that’s PHP code you’re trying to run inside of JavaScript. $_SERVER is only available to PHP so you’re getting ‘undefined’

  6. User Avatar
    Permalink to comment#

    By default, ajax using header: Accept:application/json, text/javascript, */*
    and not ajax have header like Accept:text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
    css header: Accept:text/css,*/*; it depending file your request.

    If you using php framework like CodeIgniter, you can use $this->input->is_ajax_request(). See

    By the way, $_SERVER[‘HTTP_X_REQUESTED_WITH’] is best way for detecting ajax request.

  7. User Avatar
    Mickey Mouse
    Permalink to comment#

    You should credit David Walsh for this, ’cause I know you got it from somewhere and didn’t write it yourself, Chris.

  8. User Avatar
    Permalink to comment#

    There is no need for combining isset() with empty() since empty() does not output any error.

    In my oppinion its not the right way to detect if a request was made by “ajax”. A request is a request and the result differs only in the wanted document type, what Idham mentioned. So if html, xml or json is wanted, output in this type. In PHP you get this information from $_SERVER[“HTTP_ACCEPT”], which exists for every request(type). Maybe this helps in the beginning:

    $t = $_SERVER[“HTTP_ACCEPT”];
    if(strpos($t, ‘application/json’) !== false){
    // JSON is wanted
    elseif(strpos($t, ‘application/xml’) !== false){
    // XML is wanted
    // Fallback to HTML

    • User Avatar

      @kokuswolf thanks, this was a useful breakdown of what’s going on. I’ve always used David Walsh’s version, which is what Chris is reposting here. But I’ve noticed their conditional struggles to recognize some requests from certain Node-based AJAX libraries (namely Axios in my experience.) I think your simpler $_SERVER['HTTP_ACCEPT'] check will solve the issue though!

  9. User Avatar
    Rahil Wazir
    Permalink to comment#

    Thank you Chris! Thats what i was looking for :)

Leave a Comment

Posting Code!

You may write comments in Markdown. This makes code easy to post, as you can write inline code like `<div>this</div>` or multiline blocks of code in triple backtick fences (```) with double new lines before and after.

Code of Conduct

Absolutely anyone is welcome to submit a comment here. But not all comments will be posted. Think of it like writing a letter to the editor. All submitted comments will be read, but not all published. Published comments will be on-topic, helpful, and further the discussion or debate.

Want to tell us something privately?

Feel free to use our contact form. That's a great place to let us know about typos or anything off-topic.