Skip to content

How to Check for Ajax request in CodeIgniter?

The CodeIgniter Input Class Provides some helper functions to deal with user inputs , this class is initialized automatically by the system so there is no need to do it manually.

I always recommend you to use this class instead using PHP Core Input Variables while dealing with forms and requests.

To check Ajax Request in CodeIgniter, we will use codeIgniter Output Class Method class is_ajax_request() , this function checks to see if the HTTP_X_REQUESTED_WITH server header has been set, if set response will be true otherwise false means it will returns a boolean response.

here is the Code:

 $this->input->is_ajax_request();

you can use something like this.

    // only ajax request
    protected function _is_ajax()
    {
        if (!$this->input->is_ajax_request()) {
            exit('No direct script access allowed');
        }
    }
0 0 votes
Article Rating
Subscribe
Notify of
guest

1 Comment
Most Voted
Newest Oldest
Inline Feedbacks
View all comments