Home  >  Article  >  Backend Development  >  php怎么判断是ajax请求

php怎么判断是ajax请求

WBOY
WBOYOriginal
2016-06-13 13:06:20908browse

php如何判断是ajax请求
如题,网上是说使用$_SERVER['HTTP_X_REQUEST_WITH'],但我发现似乎不行。

------解决方案--------------------
在请求头里加上个唯一标识作为ajax发送的http请求的标志,服务器端读发送请求头,当发现有ajax标志则判定为ajax请求.
------解决方案--------------------

探讨
在Symfony中是这样判断的:

PHP code
public function isXmlHttpRequest()
{
return ($this->getHttpHeader('X_REQUESTED_WITH') == 'XMLHttpRequest');
}



我的站点中用到这个函数,经实践是可以用的。

不推荐类似hack的方法,如果HTTP本身已经提供了这个功能,就应该用。
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn