开发者

Way to tell if a post came from an ajax call in codeigniter?

开发者 https://www.devze.com 2022-12-12 18:41 出处:网络
I just started using CodeIgniter after using Zend for a while.开发者_如何学GoMy new site has a feature where you register through Ajax.In Zend I could use this to check if the incoming POST was throug

I just started using CodeIgniter after using Zend for a while. 开发者_如何学GoMy new site has a feature where you register through Ajax. In Zend I could use this to check if the incoming POST was through AJAX, and therefore from my site:

if(!$this->getRequest()->isXMLHttpRequest())

Is there a piece of code in CodeIgniter that does the same thing? If I don't make sure it's an AJAX call, someone could theoretically register anything they wanted by creating a form to post to my controller.

Thanks!


Since CodeIgniter 2.0, there is an easier way of checking for an ajax request.

Use: $this->input->is_ajax_request();

Doc: https://codeigniter.com/user_guide/libraries/input.html


if (isset($_SERVER['HTTP_X_REQUESTED_WITH']) && ($_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest')) {}

But since you are using codeigniter, its better to use their input class . See how to do it below.

 if($this->input->is_ajax_request()){
     //Execute Your Code
 }


you can check it using

$this->input->is_ajax_request();
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号