10 Mar 2011

Check request is via AJAX in PHP

A simple function to indicate whether a request was made via AJAX or not (note: this is easily spoofed so it's not a secure method).
function isAjax() 
{
   return (isset($_SERVER['HTTP_X_REQUESTED_WITH']) 
   && ($_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest'));
}

No comments: