Detecting if a request is Ajax

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Detecting if a request is Ajax

Post by Ollie Saunders »

I'm writing the controller part of MVC for a home-grown framework at the moment. I've decided I want to have separate controller actions for handling Ajax request than to normal ones.

I have a request builder object that constructs the request object, this object has to set a property "isAjax" on the request. What can I check for to set such a property?
User avatar
CoderGoblin
DevNet Resident
Posts: 1425
Joined: Tue Mar 16, 2004 10:03 am
Location: Aachen, Germany

Post by CoderGoblin »

I think the Jscript and Prototype libraries both set a header but I am not 100% certain. You may want to look at what they do.
nickvd
DevNet Resident
Posts: 1027
Joined: Thu Mar 10, 2005 5:27 pm
Location: Southern Ontario
Contact:

Post by nickvd »

CoderGoblin wrote:I think the Jscript and Prototype libraries both set a header but I am not 100% certain. You may want to look at what they do.

Code: Select all

if ($_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest') {
  define('IS_AJAX',true);
}
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post by Ollie Saunders »

Cool. Thanks guys.
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post by Kieran Huggins »

@nickvd: nice 8)
nickvd
DevNet Resident
Posts: 1027
Joined: Thu Mar 10, 2005 5:27 pm
Location: Southern Ontario
Contact:

Post by nickvd »

8)
Post Reply