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
}
I think isset() is more appropriate than !empty() in this situation. Still a useful snippet though. :)
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 PHP.net on
empty()
(http://php.net/manual/en/function.empty.php)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
}
You’re missing the close parenthesis on the isset, teehee.
*syntax whore*
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.
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.
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.
@Suresh, that’s PHP code you’re trying to run inside of JavaScript. $_SERVER is only available to PHP so you’re getting ‘undefined’
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 http://codeigniter.com/user_guide/libraries/input.html
By the way, $_SERVER[‘HTTP_X_REQUESTED_WITH’] is best way for detecting ajax request.
You should credit David Walsh for this, ’cause I know you got it from somewhere and didn’t write it yourself, Chris.
http://davidwalsh.name/detect-ajax
There is no need for combining isset() with empty() since empty() does not output any error.
http://php.net/manual/en/function.empty.php
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
}
else{
// Fallback to HTML
}
@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!Thank you Chris! Thats what i was looking for :)
This is a weak method, easy to cheat with cURL