简体   繁体   English

阻止直接访问PHP页面

[英]Prevent direct access to a PHP page

How do I prevent my users from accessing directly pages meant for ajax calls only? 如何阻止我的用户直接访问仅适用于ajax调用的页面?

Passing a key during ajax call seems like a solution, whereas access without the key will not be processed. 在ajax调用期间传递密钥似乎是一种解决方案,而不使用密钥的访问将不会被处理。 But it is also easy to fabricate the key, no? 但是制作钥匙也很容易,不是吗? Curse of View Source... 观点诅咒......

p/s: Using Apache as webserver. p / s:使用Apache作为webserver。

EDIT: To answer why, I have jQuery ui-tabs in my index.php, and inside those tabs are forms with scripts, which won't work if they're accessed directly. 编辑:为了回答原因,我在index.php中有jQuery ui-tabs,并且这些选项卡内部是带有脚本的表单,如果直接访问它们将无法工作。 Why a user would want to do that, I don't know, I just figure I'd be more user friendly by preventing direct access to forms without validation scripts. 为什么用户会想要这样做,我不知道,我只是想通过阻止直接访问没有验证脚本的表单来提高用户友好性。

There is no way of guaranteeing that they're accessing it through AJAX. 没有办法保证他们通过AJAX访问它。 Both direct access and AJAX access come from the client, so it can easily be faked. 直接访问和AJAX访问都来自客户端,因此很容易被伪造。

Why do you want to do this anyways? 你为什么要这样做呢?

If it's because the PHP code isn't very secure, make the PHP code more secure. 如果是因为PHP代码不是很安全,那么使PHP代码更安全。 (For example, if your AJAX passes the user id to the PHP file, write code in the PHP file to make sure that is the correct user id.) (例如,如果您的AJAX将用户ID传递给PHP文件,请在PHP文件中编写代码以确保它是正确的用户ID。)

As others have said, Ajax request can be emulated be creating the proper headers. 正如其他人所说,可以模拟Ajax请求创建正确的标头。 If you want to have a basic check to see if the request is an Ajax request you can use: 如果您想要进行基本检查以查看请求是否是Ajax请求,您可以使用:

 if($_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest') {
     //Request identified as ajax request
 }

However you should never base your security on this check. 但是,您永远不应该根据此检查确定安全性。 It will eliminate direct accesses to the page if that is what you need. 如果这是你需要的,它将消除对页面的直接访问。

It sounds like you might be going about things the wrong way. 听起来你可能会以错误的方式处理事情。 An AJAX call is just like a standard page request, only by convention the response is not intended for display to the user. AJAX调用就像标准页面请求一样,只有按照惯例,响应才不会显示给用户。

It is, however, still a client request, and so you must be happy for the client to be able to see the response. 但是,它仍然是客户端请求,因此您必须很高兴客户能够看到响应。 Obfuscating access using a "key" in this way only serves to complicate things. 以这种方式使用“密钥”来模糊访问只会使事情变得复杂。

I'd actually say the "curse" of view source is a small weapon in the fight against security through obscurity. 我实际上说,观察源的“诅咒”是通过默默无闻来对抗安全的小武器。

So what's your reason for wanting to do this? 那么你想要这样做的原因是什么?

If the browser will call your page, either by normal request or ajax, then someone can call it manually. 如果浏览器会通过普通请求或ajax调用您的页面,那么有人可以手动调用它。 There really isn't a well defined difference between normal and ajax requests as far as the server-client communication goes. 就服务器 - 客户端通信而言,正常请求和ajax请求之间确实没有明确的区别。

Common case is to pass a header to the server that says "this request was done by ajax". 常见的情况是将标头传递给服务器,上面写着“此请求由ajax完成”。 If you're using Prototype, it automatically sets the http header "X-Requested-With" to "XMLHttpRequest" and also some other headers including the prototype version. 如果您正在使用Prototype,它会自动将http标头“X-Requested-With”设置为“XMLHttpRequest”以及其他一些标头,包括原型版本。 (See more at http://www.prototypejs.org/api/ajax/options at "requestHeaders" ) (更多信息请参见http://www.prototypejs.org/api/ajax/options at“requestHeaders”)

Add: In case you're using another AJAX library you can probably add your own header. 添加:如果您正在使用另一个AJAX库,您可以添加自己的标头。 This is useful for knowing what type of request it was on the server side, and for avoiding simple cases when an ajax page would be requested in the browser. 这对于了解服务器端的请求类型以及避免在浏览器中请求ajax页面时的简单情况非常有用。 It does not protect your request from everyone because you can't. 它不能保护您的所有人的请求,因为您不能。

COOKIES are not secure... try the $_SESSION. COOKIES不安全...试试$ _SESSION。 That's pretty much one of the few things that you can actually rely on cross-page that can't be spoofed. 这几乎是你可以真正依赖的跨页面中无法欺骗的东西之一。 Because, of course, it essentially never leaves your control. 因为,当然,它基本上永远不会让你失控。

thanks, albeit I use 谢谢,虽然我用了

define('IS_AJAX', isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest');

if(IS_AJAX) {
    //Request identified as ajax request
}

cheers! 干杯!

这绝对没有用于保护某些内容..但我认为如果你想说一个生成整页的php页面,如果ajax没有请求页面但只生成你需要返回的部分,这可能会有用当ajax被使用时..这将允许你使你的网站非ajax友好所以如果说他们点击一个链接,它应该加载一盒评论,但他们没有ajax它仍然将它们发送到页面是然后生成显示评论的整个页面。

Not sure about this, but possibly check for a referrer header? 不确定这一点,但可能检查引用者标​​题? i think if someone manually typed in your url, it wouldn't have a referrer header, while AJAX calls do (at least in the quickly test I just did on my system). 我想如果有人手动输入你的网址,它就没有引用标头,而AJAX则会这样做(至少在我刚刚在我的系统上进行的快速测试中)。

It's a bad way of checking though. 虽然这是一种糟糕的检查方式。 Referrer can be blank for a lot of reasons. 由于很多原因,推荐人可能是空白的。 Are you trying to stop people from using your web service as a public service or something? 您是否试图阻止人们将您的网络服务用作公共服务或其他什么?

After reading your edit comments, if the forms will be loaded via ajax calls, than you could check window.location to see if the url is your ajax form's url. 阅读完编辑评论后,如果表格将通过ajax调用加载,那么你可以查看window.location以查看url是否是你的ajax表单的url。 if it is, go to the right page via document.location 如果是,请通过document.location转到右侧页面

In the javascript file that calls the script: 在调用脚本的javascript文件中:

var url = "http://website.com/ajax.php?say=hello+world";
xmlHttp.open("GET", url, true);
xmlHttp.setRequestHeader('X-Requested-With', 'XMLHttpRequest');

then in the php file ajax.php: 然后在php文件中ajax.php:

if($_SERVER['HTTP_X_REQUESTED_WITH'] != "XMLHttpRequest") {
    header("Location: http://website.com");
    die();
}

Geeks can still call the ajax.php script by forging the header but the rest of my script requires sessions so execution ends when no valid session is detected. Geeks仍然可以通过伪造头来调用ajax.php脚本,但我的脚本的其余部分需要会话,因此在没有检测到有效会话时执行结束。 I needed this to work in order to redirect people with expired hybridauth sessions to the main site in order to login again because they ended up being redirected to the ajax script. 我需要这个工作,以便将过期的hybridauth会话的人重定向到主站点,以便再次登录,因为他们最终被重定向到ajax脚本。

通过index.php传递您的直接请求,并通过ajax.php传递您的ajax请求,然后不要让用户直接浏览到任何其他源文件 - 确保index.php和ajax.php具有适当的逻辑来包含他们需要的代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM