简体   繁体   English

禁止目录访问并显示禁止的错误

[英]Prevent directory access and show forbidden error

In PHP and apache web server to prevent from listing and browser showing as i know we can use .htaccess 在PHP和Apache Web服务器中,如我所知,我们可以使用.htaccess来阻止列出和显示浏览器

Deny from all

or using index.html file 或使用index.html文件

<html>
<title>403 Forbidden</title>
<body>
Directory access forbidden!
</body>
</html>

which is better then both approaches? 这两种方法哪个更好? in security and speed access to show the error or there is another approach better then both approaches 安全和快速访问以显示错误,或者还有另一种方法比两种方法更好

You can use http_response_code() to dynamic set response header in PHP script. 您可以使用http_response_code()在PHP脚本中动态设置响应头。

For example: 例如:

<?php
$acl = [
    ['path' => '/folder/admin',  'allow' => 'admin'],
    ['path' => '/folder/public', 'allow' => 'guest'],
];

$_SESSION['user_type'] = 'guest';

if ( $_SESSION['user_type'] !== $acl[0]['allow'] ) {
    http_response_code(403);
    exit;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM