请问$ _ SERVER ['HTTP_X_REQUESTED_WITH']在PHP或不存在吗? [英] Does $_SERVER['HTTP_X_REQUESTED_WITH'] exist in PHP or not?

查看:469
本文介绍了请问$ _ SERVER ['HTTP_X_REQUESTED_WITH']在PHP或不存在吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在互联网上,甚至包括在这里堆栈溢出,人指出,一个很好的方法来检查请求是否使用AJAX是要做到以下几点:

All over the Internet, included even here at Stack Overflow, people state that a good way to check if a request is AJAX or not is to do the following:

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

不过,我没有看到 $ _ SERVER ['HTTP_X_REQUESTED_WITH'] 中的 PHP官方文档

当我试图做到以下几点:

And when I try to do the following:

echo $_SERVER['HTTP_X_REQUESTED_WITH'];

没有输出。

我是不是做错了什么?因为我真的很想能够使用 $ _ SERVER ['HTTP_X_REQUESTED_WITH'] (如果可用)。

Am I doing something wrong? Because I'd really like to be able to use $_SERVER['HTTP_X_REQUESTED_WITH'] if it's available.

推荐答案

$变量_ SERVER 是不是真的部分PHP的,这就是为什么你不会找到他们的PHP文档。它们是由它们传递到脚本语言在Web服务器ppared $ P $。

The variables in $_SERVER are not really part of PHP, which is why you won't find them in the PHP documentation. They are prepared by the Web server which passes them on to the scripting language.

据我所知, X-要求 - 以是由大多数主要框架的Ajax功能,但不是所有(道场,例如,仅把它添加发送两年前:#5801 )。因此,并考虑到考虑@bobince的意见,这是安全地说这不是一般的100%可靠的方法来确定一个请求是否是一个Ajax请求。

As far as I know, the X-Requested-With is sent by the Ajax functions of most major Frameworks but not all (Dojo, for example, added it only two years ago: #5801). As such, and taking into considerations @bobince' comments, it's safe to say it's not generally a 100% reliable method to determine whether a request is an AJAX request or not.

唯一的100%安全的方法是随请求和接收网页,以检查该标志的presence发送pre定义的标记(如GET变量)。

The only 100% secure way is to send a pre-defined flag (e.g. a GET variable) along with the request and for the receiving page to check for the presence of that flag.

这篇关于请问$ _ SERVER ['HTTP_X_REQUESTED_WITH']在PHP或不存在吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆