简体   繁体   English

阻止直接访问文件,但允许通过jquery加载功能进行访问

[英]block direct access to file but allow access through jquerys load function

I'm using jQuery to display a certain page to a user through it's .load() function. 我正在使用jQuery通过.load()函数向用户显示特定页面。 I am doing this to allow user customization to the website, allowing them to fit it to their needs. 我这样做是为了允许用户自定义网站,使他们能够适应他们的需求。

At the moment, I am trying to display the file feed.php inside of a container within main.php ; 目前,我正在尝试将文件feed.php显示在main.php内的容器中;

I have come across a problem where I would like to prevent direct access to the file (ie: going directly to the path of it ( ./feed.php )) , but still allowing it to be served through the .load() function. 我遇到了一个问题,我想阻止直接访问文件(即:直接进入文件的路径( ./feed.php )) ,但仍然允许通过.load()函数提供文件。

If I use the .htaccess deny from all method for this, I get a 403 on that specific part of the page. 如果我通过deny from all方法使用.htaccess deny from all ,则在该页面的特定部分会收到403。 I can't find any other solution to this problem; 对于这个问题,我找不到其他解决方案。 disallowing me to achieve what I want. 不允许我实现我想要的。

This is my current (simplified) script and html : 这是我当前的(简体) scripthtml

<script type="text/javascript">
    $("#dock-left-container").load("feed.php"); // load feed.php into the dock-left-container div
</script>

<div class="dock-leftside" id="dock-left-container"></div> // dock-left-container div

If anyone could suggest a solution through .htaccess , php , or even a completely different way to do this, I'd be very grateful! 如果有人可以通过.htaccessphp或什至是完全不同的方式提出解决方案的建议,我将不胜感激!

Thanks in advance. 提前致谢。

Please follow below steps to achieve: 请按照以下步骤操作:

  1. In the .load function of jquery post a security code. 在jquery的.load函数中发布安全代码。
  2. In the Feed.php page place a PHP condition if the posted security_code params found and match with security_code passed in the .load then only allow to access the page otherwise restrict. 在Feed.php页地方PHP条件,如果张贴的security_code PARAMS发现匹配security_code在通过.load然后只允许访问该页面以其他方式限制。

Please follow below changes in your existing code to achieve it. 请按照以下现有代码中的更改来实现。

JS JS

<?php 
    $_SESSION['security_code'] = randomCode();
?>
<script type="text/javascript">
    $("#dock-left-container").load("feed.php", {
       security_code: '<?= $_SESSION['security_code']; ?>'
   }); // load feed.php into the dock-left-container div
</script>

PHP 的PHP

Place php condition in the top of feed.php 将php条件放在feed.php的顶部

if(isset($_POST['security_code']) && $_POST['security_code'] == $_SESSION['security_code']){
    //Feed.php page's all the stuff will go here
}else{
    echo "No direct access of this page will be allowed.";
}

feed.php : feed.php

if (isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {
    readfile('myfeed.xml');
} else {
    header('HTTP/1.0 403 Forbidden');
}

jQuery sends a HTTP_X_REQUESTED_WITH header by default. jQuery默认情况下发送HTTP_X_REQUESTED_WITH标头。 This is not, by far, anything remotely secure since HTTP headers are easily sent/spoofed. 到目前为止,这并不是远程安全的任何东西,因为HTTP标头很容易发送/欺骗。 But it will stop the occasional user trying to access the feed directly. 但这会阻止偶尔的用户尝试直接访问源。

You can, additionaly, check the $_SERVER['HTTP_REFERER'] header (but, again, this is easily spoofed) and, ofcourse, use your normal session logic to make sure the user is logged on if that's a requirement to access the feed. 另外,您可以检查$_SERVER['HTTP_REFERER']标头(但同样,这很容易被欺骗),并且当然,如果需要访问提要,请使用常规会话逻辑来确保用户已登录。 。

Either way: there's no way to make this 'water tight'. 无论哪种方式:都无法使这种“水密性”。 If your browser can (should be able to) access the feed in some way then it's simply a matter of opening the debugger, having a look at the actual request sent in the network tab and sending the exact same headers/request to get to the file from, say, Curl. 如果您的浏览器可以(应该)以某种方式访问​​提要,则只需打开调试器,查看网络标签中发送的实际请求,然后发送完全相同的标头/请求即可到达文件,例如Curl。 Actually, you will see the response of the request (ie the actual feed) in the debugger as well. 实际上,您还将在调试器中看到请求的响应(即实际的提要)。

Repeat after me: if my (or a user's) browser can access the feed 'from jQuery' (via an AJAX request or whatever) then the feed is accessible to that user if he's even just a little bit more persistent than giving up immediately. 在我之后重复一遍:如果我的(或用户的)浏览器可以(通过AJAX请求或其他方式)“通过jQuery”访问提要,则该提要对于该用户而言是可访问的,即使他比立即放弃要持久得多。 Only using a session will keep out 'unauthorized' users because it relies on being logged in. After having logged in the request is visible no matter what and that request can be 'forged' to be sent from any other application no matter what. 仅使用会话将保留“未授权”用户,因为它依赖于登录。登录后,无论如何都可以看到该请求,无论该请求如何都可以“伪造”以从任何其他应用程序发送。

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

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