Reputation: 63626
I need to deal with these two cases differently,is there a good solution?
Upvotes: 3
Views: 127
Reputation: 162781
I can think of 2 ways to accomplish this:
On the AJAX side you could set a custom HTTP header with XMLHttpRequest.setRequestHeader()
and then check for the presence of that header on the PHP side with getallheaders()
to indicate that the request was made by an AJAX client. If your php script doesn't find the custom header, you can consider it to be a non-AJAX request.
When you create the request in your code, you could simply tack on a querystring variable to indicate the nature of the request. eg. http://example.com/process?ajax=true for an AJAX reqeust or http://example.com/process?ajax=false for a non-AJAX request.
Upvotes: 3
Reputation: 2757
if ($_SERVER['HTTP_X_REQUESTED_WITH'] === 'XMLHttpRequest') { /* ajax request */ }
Upvotes: 6