【问题标题】:How do I prevent the ability to change the URL and see other users folders如何阻止更改 URL 和查看其他用户文件夹的能力
【发布时间】:2015-05-11 19:23:43
【问题描述】:

我有一个文件夹目录,我想阻止名为“x”的用户使用以下文件:reports/x/2015/04/ 将 x 更改为 y 并查看 y 中的所有这些文件。我有会话工作,因此您需要登录才能查看任何文件夹,但如果您以 x 身份登录,您可以通过更改 URL 来查看 y 文件夹。这是我的 index.php。

<?php

session_start();

if(!isset($_SESSION['username'])){
   header("Location:../../../../login/login.php");
}

require_once('../../../config.php');
require_once('../../../boilerplate.php');

global $smarty;

$smarty->display('general-report.tpl');

【问题讨论】:

    标签: php url limit restrict


    【解决方案1】:

    这样做有一些本质上不好的地方,但为了简单起见,快速修复将是检查用户名是否与文件夹名称匹配。

    所以,看看你的代码,你可以做这样的事情。

    if ($username == $dir_name) { $smarty->display('general-report.tpl'); } else { $smarty->display('error.tpl'); }

    现在,至于为什么你不应该这样做......

    登录的用户名不应该在 URL 中真正可见。

    您不希望人们开始通过 URL 分享他们的用户名,然后让恶作剧的人开始强行进入您的登录系统,因为他们知道各种用户名。

    如果是我,我会让报告的 URL 都相同,只需让登录的用户名确定要显示哪个用户的报告。

    这样你就知道它只对那个人可见,即使他们在某处分享 URL,他们的用户名也不会泄露出去。

    【讨论】:

    • 之所以这样,是为了维护每个月的维护报告。 l.我现在有这个: display('general-report.tpl'); } else{ echo "没有你的页面"; } 这仍然不起作用
    • 在您发布的示例中,$directory 并未实际设置,因此它永远不会匹配。您需要弄清楚用户试图访问的目录。看看$_SERVER['REQUEST_URI']。然后,您可以在/ 上执行explode 并找出文件夹是什么。然后将其与用户名进行比较。您可能还想在用户名上使用strtolower,以防其中包含大写字母。
    • 我在想,在会话中使用目录名称,然后检查它是否等于我们在数据库中的名称。有什么想法怎么写吗?
    • 在会话中存储目录名称并不是一个很好的理由。您已经获得了用户名。只需检查文件夹的 URI,就可以了。
    【解决方案2】:

    您可以在$_SESSION 中存储一个代表用户家的值。如果用户在他的主目录之外,他将被重定向到他的主目录。值 `$_SESSION['home'] 可以是存储在数据库中的值,也可以是用户名本身。

    你可以这样做:

    preg_match('/reports\/([a-zA-Z0-9]*)\//',$_SERVER['REQUEST_URI'],$matches);
    if($_SESSION['home'] != $matches[0]){
        header('location: reports/' . $_SESSION['home']);
    }
    

    随意调整正则表达式。

    【讨论】:

      【解决方案3】:

      谢谢大家,我得到它的工作:

      <?php
      /*===== Start sesstion and include config and boilerplate=====*/ 
      session_start();
      require_once('../../../config.php');
      require_once('../../../boilerplate.php');
      
      global $smarty;
      
      /*=====Prevents seeing any pages unless logged in =====*/
      if(!isset($_SESSION['username'])){
         header("Location:../../../../login/login.php");
       exit;
       }
      /*=====Allows only logged in users to see their profiles in path_report=====*/
      preg_match('/maintenance_new\/([a-zA-Z0-9]*)\//',$_SERVER['REQUEST_URI'],$matches);
      $path_report = explode("/", $_SESSION['path_report']);
      if($path_report[0] != $matches[1]){
      header('Location: /maintenance_new/' . $_SESSION['path_report'] );
      exit;
      }
       /*=====Renders out page=====*/
       $smarty->display('general-report.tpl');
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-03-12
        • 1970-01-01
        • 1970-01-01
        • 2017-10-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-07-12
        相关资源
        最近更新 更多