php - 经过身份验证的网页(通过 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 这样的服务器端脚本方面经验为零,所以如果这个问题的答案真的很明显,请多多包涵。非常感谢你!
解决方案
推荐阅读
- matlab - 如何求解马尔可夫转移率矩阵?
- amazon-web-services - AWS Glue 进程记录逐行插入更新删除
- firebase - 通过云功能获取firebase数据,无需任何触发
- javascript - 排序不与 forEach 一起使用,而是与 map 一起使用
- ruby - Rspec,伪类AR属性
- java - Java Runnable 运行的 JUnit 测试用例
- javascript - 最近的 Firefox 破坏了现有的转换后的 Chrome 扩展
- python - Django:如何在从 mysql DB 获取数据时使用 LIKE
- jdo - 如何在没有连接的jdo apache isis中存储字符串集合
- javascript - highchart中的鼠标滚轮事件减少xaxis高度