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

php怎么判断是ajax请求

WBOY
Release: 2016-06-13 10:57:08
Original
816 people have browsed it

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本身已经提供了这个功能,就应该用。
Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template