Welcome!

My name is Manmohanjit Singh and I’m 15 this year. Sorry you’ll just have to accept it, I may be a kid to you but trust me I’m not. I live in Malaysia and I go to school at SMK USJ 12. Read more...

Twitter

  • Loading...

@manmohanjit

Recent Tracks

  • Loading...

manmohanjit1

# [Snippet] Checking for an Ajax request using PHP

by Manmohanjit Singh on May 7th, 2010 in Code, Tutorial.

Here’s a piece of code that might come in handy while handling Ajax requests through PHP. With this code, you can easily check if there is an Ajax request.

function isAjax() {
	if(!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {
		return true;
	}
	else {
		return false;
	}
}

To check for an Ajax request is as simple as this:

if(isAjax()) {
	//there is an ajax request
}
else {
	//:( no ajax request
}

So now you don’t need to send in a post/get parameter just to check for an Ajax request, just use the code above.

Tags:

2 comments

Leave A Comment

I'm watching you, please be nice and try not to spam. Comments are moderated. You can paste links directly, it will be formatted automatically. Basic XHTML tags are allowed.

Oh, I almost forgot. Comments are powered by Ajax, so don't hit the submit button twice. Thanks!

Wait for 15 seconds...