Check if is ajax request in CI Laravel PHP

2.31K views
no comments
11 Aug 2015 1:22 am

In action url of methods we just only allow user to submit ajax request on specified url or doing another stuff, Here is I am share examples of check ajax request on custom #PHP #CodeIgniter #Laravel.

Check ajax request in core PHP

Here is how to check and its best way to define a function and use it where need instead using the whole code every where.

PHP CORE SIMPLE WAY

[php]
if(strtolower($_SERVER[‘HTTP_X_REQUESTED_WITH’]) == ‘xmlhttprequest’){
//AJAX REQUEST
//DO STUFF
}
[/php]

PHP CORE CHECK WITH FUNCTION NAMED IS_AJAX

[php]
function is_ajax(){
$is_ajax = false;
if(strtolower($_SERVER[‘HTTP_X_REQUESTED_WITH’]) == ‘xmlhttprequest’){
$is_ajax = true;
}
return $is_ajax;
}

if(is_ajax()===true){
//AJAX REQUEST
//DO STUFF
} else {
//NOT AJAX AJAX REQUEST
//DO NON-AJAX STUFF
}
[/php]

CHECK IS AJAX REQUEST IN LARAVEL FRAMEWORK

In frameworks commonly use their defined methods #laravel have own method to check is ajax request
[php]
if(Request::ajax()){
//AJAX REQUEST
//DO STUFF
} else {
//NOT AJAX AJAX REQUEST
//DO NON-AJAX STUFF
}
[/php]

CHECK IS AJAX REQUEST IN CODEIGNITER FRAMEWORK

[php]
if($this->input->is_ajax_request()){
//AJAX REQUEST
//DO STUFF
} else {
//NOT AJAX AJAX REQUEST
//DO NON-AJAX STUFF
}
[/php]

NOTE:Your Email Address will be not shown and please do not add spamming comments because here is REL="NOFOLLOW" on your links and comments also moderated shown.
<code>Put html css or any language code under this tag</code>