PHP登录网站文件夹

发布于 2024-10-03 02:36:25 字数 176 浏览 0 评论 0原文

我的网站上有一个名为“admin”的文件夹,即。 www.example.com/admin/,所以我想做的是,当有人访问该地址时,会显示一个登录框,一旦他们输入正确的凭据,他们就可以看到“admin”文件夹中的所有内容,所以我是否需要在每个页面的顶部打勾,或者我可以将支票粘贴在index.php 页面中吗?

这可能吗?

i have a folder on my website called "admin" ie. www.example.com/admin/, so what i want to do is when someone goes to that address a login box is displayed, once they put in the right credentials, they are allowed to see everything in that "admin" folder, so do i need to put in a check at the top of every page or can i just stick the check in the index.php page?

Is this possible?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

︶ ̄淡然 2024-10-10 02:36:25

如果您想通过 PHP 执行此操作,则必须在每个源文件中包含代码,以检查此人是否具有正确的凭据。您确实想通过基于网络服务器的解决方案来解决这个问题。

如果您使用 Apache,则可以使用 .htaccess 文件来设置身份验证。尝试本文,朝着正确方向迈出第一步。

使用基于服务器的身份验证还允许您保护非 PHP 文件。

If you want to do this via PHP, you'll have to include code in every source file checking if the person have proper credentials. You really want to solve this with a web-server based solution.

If you're using Apache, you can use a .htaccess file to set up authentication. Try This article for your first step in the right direction.

Using server based authentication also allows you to protect non-PHP files.

零時差 2024-10-10 02:36:25

当有人访问该文件夹时,会显示index.php。如果没有它,将显示内容(所有文件),这就是我认为您想要的(即列出所有内容)。

但是,可以在 .htaccess 文件中设置权限,以要求对这些文件进行密码访问。

这样,您的文件不需要单独保护,只需文件夹本身即可。

The index.php is displayed when someone goes to that folder. Without it, the contents (all the files) would be displayed, which is I think what you want here (ie list everything)

However, permissions can be set in the .htaccess file to require passworded access to these files.

In this way, your files don't need to be individually protected, just the folder itself.

毁虫ゝ 2024-10-10 02:36:25

如果您计划在 PHP 中使用会话,您应该检查是否在成功登录的用户访问的页面上设置了适当的会话变量。

为了详细说明,您需要使用如下内容:

<?php
session_start();

if($_SESSION['usertype'] != 'admin') //$_SESSION['usertype'] should be set in the index page once the admin guy logs in successfully
header("Location:http://www.example.com/admin/"); //Redirect the user to the login page if the expected session variable is not found
?>

If you are planning to use sessions in PHP, you should check if the appropriate session variables are set on the pages which the successfully logged in user visits.

To elaborate, you'll need to use something like this:

<?php
session_start();

if($_SESSION['usertype'] != 'admin') //$_SESSION['usertype'] should be set in the index page once the admin guy logs in successfully
header("Location:http://www.example.com/admin/"); //Redirect the user to the login page if the expected session variable is not found
?>
抽个烟儿 2024-10-10 02:36:25

也可以根据您的实际要求使用 .htaccess 来实现(某种程度上)。

AuthType Basic
AuthName "Admin Area"
AuthUserFile /path/to/.htpasswd
Require valid-user

.htpasswd

admin:$apr1$7ej2t/..$qlcauURCmChKfwVhnxRLt.

(管理员/密码)

also can be achieved (sort of) with .htaccess depending on your actual requirements.

AuthType Basic
AuthName "Admin Area"
AuthUserFile /path/to/.htpasswd
Require valid-user

.htpasswd

admin:$apr1$7ej2t/..$qlcauURCmChKfwVhnxRLt.

(admin/pass)

困倦 2024-10-10 02:36:25

您的主要入口点可以是index.php,并根据是否设置了SESSION 将用户路由到正确的视图。

至于阻止文件夹中的其他文件:

对于 PHP 文件:在 index.php 中定义一个常量,并在包含的其他 PHP 文件的顶部检查它是否已“定义”。

至于图像和其他类型的文件,您可以将它们存储在根目录之上并通过 PHP 包含它们。

Your main point of entry can be the index.php and route the user to the proper view based on whether or not a SESSION is set.

And as for blocking the other files in the folder:

for PHP files: define a constant in the index.php and at the top of the other PHP files that are included check if it's "defined".

As for images and other types of files you can store them above the root and include them via PHP.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文