【问题标题】:Disable url access of files禁用文件的url访问
【发布时间】:2012-09-19 09:01:43
【问题描述】:

我正在使用 WAMP 并在本地托管 PHP 文件。

假设我有这两个文件。

customer.php- 添加客户详细信息的表单

recordCustomer.php- 连接到数据库并将信息存储在数据库中(所有处理完成的地方)

如何通过在地址栏中输入文件名来阻止用户访问文件 recordCustomer.php

http://localhost/testing/recordCustomer.php has to be redirected and given an error message

然而

http://localhost/testing/customer.php is allowed

谢谢

【问题讨论】:

  • 您的用户是否登录到您的页面?那你用会话吗?
  • 是的。系统有不同的用户级别,用户需要登录。如果尝试访问完成数据处理的文件,我只需要给他们一条错误消息。

标签: php mysql url redirect


【解决方案1】:

问题不在于对recordCustomer.php 的访问,而是如果访问是直接的,则执行代码这一事实。 例如,阻止通过 htaccess 访问会使您的表单无法提交。

您应该在customer.php 表单中使用随机令牌。令牌被保存到会话中并插入到带有隐藏输入的表单中。

recordCustomer.php 上,您只需要检查是否给出了令牌以及它是否与会话中的令牌匹配。

如果匹配 => 这是对recordCustomer.php的合法调用
else => 尝试绕过你的表单,拒绝请求。

这是一个 CSRF 攻击的学校案例;)

here 为例

【讨论】:

  • 在隐藏的输入字段中使用令牌,像你说的那样检查是有效的,但是我想我必须在我的所有表单中使用它:(.. 感谢您的帮助,非常感谢。:)
【解决方案2】:

您不必阻止对整个 recordCustomer.php 的访问,您只需要在 recordCustomer.php 的开头检查调用page 有权修改他在表单中发送的记录。如果没有,则显示错误。

我贴一些代码让你明白。

recordCustomer.php的伪/php代码:

if (! isset($_SESSION['user_logged'])) {
    echo 'You have to be logged in to access this page (this incident will be logged)';
    exit;
}

$user = $_SESSION['user_logged']; //this was saved when the user logged in
$record = $_POST['record_to_modify'];

if (!user_can_modify_record($user, $record)) {
    echo 'You dont have permission to access that record (this incident will be logged)';
    exit;
}

//HERE THE REST OF recordCustomer.php

function user_can_modify_record($user, $record) {
   $set = db_query("select user from table where user = '". $user. "' and record_id = ". $record);
   return db_count_rows($set) > 0;
}

【讨论】:

    【解决方案3】:

    您可以通过以下方式简单地检查是否从表单请求文件:

    if(isset($_POST['submit']))

    【讨论】:

    • 这很容易被恶意和精通技术的用户伪造。
    • 问题询问“如果直接访问页面,如何显示错误”。我认为这是实现这一目标的简单方法。此外,接受的解决方案也很容易被恶意和精通技术的用户伪造。
    猜你喜欢
    • 2011-03-23
    • 2012-04-28
    • 1970-01-01
    • 2012-03-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-22
    • 1970-01-01
    相关资源
    最近更新 更多