16 December, 2010

Detect Ajax request using PHP

Sometimes, we need to detect ajax request using php. In such case, we can write following code that checks whether the request is ajax or not. It is helpful in conditions such as when i have to load a page depending on condition using a same function in php.

if(isset($_SERVER['HTTP_X_REQUESTED_WITH']) && $_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest')) {
echo "Ajax request";
}
else {
echo "Normal Page Load";
}

And thats it.

No comments: