Here’s a piece of code that might come in handy while handling Ajax requests through PHP. With this code, you can easily check if there is an Ajax request.
function isAjax() {
if(!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {
return true;
}
else {
return false;
}
}
To check for an Ajax request is as simple as this:
if(isAjax()) {
//there is an ajax request
}
else {
//:( no ajax request
}
So now you don’t need to send in a post/get parameter just to check for an Ajax request, just use the code above.
2 comments
By [Snippet] Checking for an Ajax request using PHP – Manmohanjit Singh | Programming Blog Imagik.org
124 days ago
[...] original post here: [Snippet] Checking for an Ajax request using PHP – Manmohanjit Singh No [...]
By [Snippet] Checking for an Ajax request using PHP – Manmohanjit Singh « 技術者派遣の技術日誌ブログ
124 days ago
[...] Read the rest here: [Snippet] Checking for an Ajax request using PHP – Manmohanjit Singh [...]