【问题标题】:How can I write .htaccess so that it only gives access from the form?如何编写 .htaccess 以便它只提供来自表单的访问权限?
【发布时间】:2013-10-14 09:19:15
【问题描述】:

这是我的代码:

<files contacts.html>
order allow,deny
deny from all
Allow from access.php
</files>

我不希望从 URL 直接访问contacts.html。我在 index.html 页面上有一个表单,其中包含一个带有操作 access.php 的表单。确认后,它会使用 header 函数将其重定向到 contacts.html。

【问题讨论】:

  • 好吧,一种可能的方法是检查引荐来源标头。但这相当弱:首先,可以伪造它(因为可以伪造任何 HTTP 标头),其次,一些代理不通过它。我的建议是读取access.php 文件中的文件内容,然后在正确提交表单时回显它(带有正确的标题)。
  • 当你回显文件而不是重定向时,你打破了重定向后获取模式,这是你可能不想要的。

标签: .htaccess web-development-server


【解决方案1】:

你会想要使用$_SESSION

开启access.php

session_start();

// some code here to authenticate

$_SESSION['hasAccess'] = true;

// redirect

contacts.html 更改为contacts.php 并在此页面顶部

session_start();

if($_SESSION['hasAccess'] !== true){

    header('Location: index.php');
    die;

}

// the rest of your code

【讨论】:

  • 完美!感谢您的快速响应和优雅的响应!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-09-06
  • 1970-01-01
  • 2019-10-24
  • 2021-07-05
相关资源
最近更新 更多