[英].htaccess: deny direct access to files in specific case
我在我的webroot中有这些文件:index.php(登录页面),login.class.php,styles.css然后是一个目录“accounts”,在该目录中有一些css文件,index.php和formprocess.php
因此,当用户访问网站(index.php)时,系统会要求他登录。如果他输入正确的用户名和密码:他会被重定向到accounts / index.php。 如果他输入错误:他的IP被阻止了。
但是可以通过直接访问accounts / index.php来绕过此登录页面。 所以我想阻止它。
我想要的是:用户只有在登录到网站时才能访问“accounts”目录及其文件。 直接访问应该给403错误。
谁能告诉我如何使用.htaccess或php?
任何帮助,将不胜感激 :)
通过PHP,您可以创建一个会话,在登录过程中,您可以设置一定的值,这是打开accounts/index.php
所需的值accounts/index.php
让我们说值“香蕉”
授权完成后,您的index.php将拥有此代码:
$_SESSION['banana'] = "true";
然后你的accounts/index.php
看起来像这样:
if($_SESSION['banana'] == "true")
{ //authorization went correctly enter code here
}
else
{ //not authorized, return to index
header("location: ../index.php");}
哦,不要忘记session_start();
在两个页面中。
我喜欢老学校的技术。 .htaccess仍然适合我,所以让我提供一个替代解决方案vs上面给出的PHP :)
更多信息:在您的网站根目录创建此.htaccess。
# Protect the htaccess file
<Files .htaccess>
Order Allow,Deny
Deny from all
</Files>
# protect your files by extension
<Files *.php>
Order allow,deny
Deny from all
</Files>
# Deny access to sub directory
<Files accounts/*>
deny from all
</Files>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.