首页 > 解决方案 > 经过身份验证的网页(通过 php/html + .htaccess)看不到其他目录(css、JavaScript、图像等)中的资产

问题描述

我希望用 HTML 表单 + PHP 对我的整个站点目录进行密码保护。在这篇文章的帮助下,我能够捕获经过身份验证的会话并在页面之间导航(谢谢!) - 但是浏览器看不到这些页面中链接的任何外部文件。这是参考图片


问题

索引页面呈现但没有任何资产(css、js、图像、字体等;)


目录

一切都在根目录中。

├── pages
|   ├── page-1.html
|   └── page-2.html
|   └── page-3.html
├── assets
|   ├── main.css
|   └── all.js
├── .htaccess
├── login.php
└── index.html

.htaccess

RewriteCond %{REQUEST_FILENAME} !login.php
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule .* login.php?file=$0 [QSA,L] # pass everything thru php

登录.php

 <?php
 $file = 'index.html';
 $password = $_POST['password'];
 $key = 'password';
  if(isset($_POST['submit'])){
    if($password === $key){
      $_SESSION['login'] = true;
        readfile($file);

      die();
    } {
      echo "<div class='alert alert-danger'>wrong password.</div>";
    }
  }
?>
<html>
   <head>
     <title>Login</title>
   </head>
<body>
    <form action="" method="post">
      <label for="pwd">Password</label>
      <input type="password" class="form-control" id="pwd" name="password" required>
      <button type="submit" name="submit" class="btn btn-default">Login</button>
    </form>
</body>
</html>

索引.html

<html>
<head>
    <meta charset="UTF-8">
    <link href="/assets/main.css" type="text/css" rel="stylesheet">
    <title>A title</title>

</head>
<body>
    A list of links
    <ul>
        <li>
            <a href="/pages/page-1.html">page-1</a>
        </li>
        <li>
            <a href="/pages/page-2.html">page-2</a>
        </li>
        <li>
            <a href="/pages/page-3.html">page-3</a>
        </li>
    </ul>
</body>
<script src="/assets/all.js" ></script>
</html>

我怀疑资产无法访问,因为它们仍然是私有的。也许解决方案是允许访问不仅仅是索引文件?

我在编写像 PHP 这样的服务器端脚本方面经验为零,所以如果这个问题的答案真的很明显,请多多包涵。非常感谢你!

标签: phphtml.htaccesspassword-protection

解决方案


推荐阅读