如何在php(或codeigniter)中区分ajax调用和浏览器请求?

时间:2022-10-07 00:09:21

is there a way to differentiate ajax call and normal browser request in php (or codeigniter to be specific)?

在php中有一种方法可以区分ajax调用和普通浏览器请求(或codeigniter中特定的)吗?

this is my jquery ajax call:

这是我的jquery ajax调用:

$(document).ready(function() {
    $('#container').load('http://localhost/index.php/customer/'); 
});

this is the index method of customer controller in codeigniter:

这是codeigniter中客户控制器的索引方法:

public function index() {
    //if (call == 'ajax request') 
    //  do this if it's an ajax request;
    //else
    //  do that if user directly type the link in the address bar;
    $this->load->view('customer/listview');
}

any help would be appreciated. thanks.

如有任何帮助,我们将不胜感激。谢谢。

5 个解决方案

#1


5  

CodeIgniter way..

CodeIgniter方式. .

$this->input->is_ajax_request()

#2


3  

function getIsAjaxRequest()
{
    return isset($_SERVER['HTTP_X_REQUESTED_WITH']) && $_SERVER['HTTP_X_REQUESTED_WITH']==='XMLHttpRequest';
}

Define this function somewhere then of course use it like this:

在某个地方定义这个函数,然后像这样使用:

if (getIsAjaxRequest())
// do this
else
// do that

But there might be such thing already in CodeIgniter implemented, just global search for HTTP_X_REQUESTED_WITH

但是在CodeIgniter中可能已经实现了这样的东西,即HTTP_X_REQUESTED_WITH的全局搜索

#3


1  

if (strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {}

Should do what you need. Though it can obviously be faked like any other HTTP Header, so don't rely on it for anything major.

应该做你需要的。尽管它很明显可以像任何其他HTTP报头一样被伪造,所以不要把它作为任何主要信息。

#4


0  

This is Codeigniter's implementation of this functionality.

这是Codeigniter实现的这个功能。

if($this->input->isAjax()) {

        }  

#5


0  

Instead of relying on server variables which might get changed eg. if the server is behind a reverse proxy I do all my AJAX calls through a single javascript function in which I add a POST variable: isajax. Then I check for it using something like $this->UI->IsAJAX() which looks for a varible that was initialized while setting up the controller.

而不是依赖服务器变量,这些变量可能会被修改。如果服务器在反向代理的后面,我将通过一个javascript函数进行所有的AJAX调用,在该函数中我添加了一个POST变量:isajax。然后我使用$ >UI->IsAJAX()来检查它,它查找在设置控制器时初始化的变量。

$this->_isAJAX = (empty($_POST['isajax']) ? true : false.

#1


5  

CodeIgniter way..

CodeIgniter方式. .

$this->input->is_ajax_request()

#2


3  

function getIsAjaxRequest()
{
    return isset($_SERVER['HTTP_X_REQUESTED_WITH']) && $_SERVER['HTTP_X_REQUESTED_WITH']==='XMLHttpRequest';
}

Define this function somewhere then of course use it like this:

在某个地方定义这个函数,然后像这样使用:

if (getIsAjaxRequest())
// do this
else
// do that

But there might be such thing already in CodeIgniter implemented, just global search for HTTP_X_REQUESTED_WITH

但是在CodeIgniter中可能已经实现了这样的东西,即HTTP_X_REQUESTED_WITH的全局搜索

#3


1  

if (strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {}

Should do what you need. Though it can obviously be faked like any other HTTP Header, so don't rely on it for anything major.

应该做你需要的。尽管它很明显可以像任何其他HTTP报头一样被伪造,所以不要把它作为任何主要信息。

#4


0  

This is Codeigniter's implementation of this functionality.

这是Codeigniter实现的这个功能。

if($this->input->isAjax()) {

        }  

#5


0  

Instead of relying on server variables which might get changed eg. if the server is behind a reverse proxy I do all my AJAX calls through a single javascript function in which I add a POST variable: isajax. Then I check for it using something like $this->UI->IsAJAX() which looks for a varible that was initialized while setting up the controller.

而不是依赖服务器变量,这些变量可能会被修改。如果服务器在反向代理的后面,我将通过一个javascript函数进行所有的AJAX调用,在该函数中我添加了一个POST变量:isajax。然后我使用$ >UI->IsAJAX()来检查它,它查找在设置控制器时初始化的变量。

$this->_isAJAX = (empty($_POST['isajax']) ? true : false.