如何服务器可以检查Ajax请求没有从现场,X-要求,随着 [英] How server can check ajax requests not from site, X-Requested-With

查看:100
本文介绍了如何服务器可以检查Ajax请求没有从现场,X-要求,随着的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我读到检查的X要求,随着Ajax请求的头是一个很好的方式,以确保该请求并没有来自外部的。在服务器端,我如何检查这个头?什么是缺少正确的方式这个头的反应或错误(重定向,抛出异常,其他)?

I read that checking the X-Requested-With header of the ajax request is a good way to make sure the request isn't coming from outside. On the server side, how do I check this header? and what's the right way to react of this header is missing or wrong (redirect, throw exception, else)?

推荐答案

您可以检查它像这样...

You can check it like this...

$isAjax = isset($_SERVER['HTTP_X_REQUESTED_WITH']) AND 
          strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) === 'xmlhttprequest';

如果你的只有的希望通过XHR的访问,那么就退出如果这头不是present。

If you are only expecting access via XHR, then just exit if this header is not present.

注意:这头是微不足道的恶搞。不要依赖于这个任何东西,但它看起来像它来自呐XHR 的。

Note: This header is trivial to spoof. Don't rely on this for anything but it looks like it came from na XHR.

这篇关于如何服务器可以检查Ajax请求没有从现场,X-要求,随着的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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