What is the best way within my PHP script to differentiate between a normal browser GUI request and an AJAX request?
Not as such.
You can write your JavaScript in such as way to to leave some sort of identifier in the request headers that you could use though. See the XHR setRequestHeader
method.
A nice use of HTTP would be to modify the Accept
header and then do normal content negotiation. Then (for example), instead of caring if it is Ajax or not, you just care if an HTML response is preferred over a JSON response.
Another convention is to use the non-standard X-Requested-With
header with a value of XMLHttpRequest
. A number of JavaScript libraries will add this by default to any request using XHR.
Either of these techniques will only work with XMLHttpRequest or plugin based Ajax though. You can't set arbitrary HTTP headers for JSON-P or iframe based Ajax.