简体   繁体   English

目录索引的PHP会话认证

[英]PHP Session Authentication of Directory Index

The context: I'm building a PHP 7 web application, it uses a PHP session to login and check to see if the user is logged in on each page.上下文:我正在构建一个 PHP 7 Web 应用程序,它使用 PHP 会话登录并检查用户是否在每个页面上登录。 Here is the basic makeup of most pages:这是大多数页面的基本构成:

<?php session_start(); ?>
<?php include 'the_header_and_menu.php'; ?>
<p>Page content</p>
<?php include 'the_footer.php'; ?>

At the top of the_header_and_menu.php file is an include to session_check.php which is located outside the site's directory.the_header_and_menu.php文件的顶部是session_check.php的包含,它位于站点目录之外。 This PHP process does five checks, the most basic one included below.这个 PHP 进程会执行五项检查,最基本的一项包括在下面。

if (!isset($_SESSION['loggedin']) || $_SESSION['loggedin'] == 'false') { // If loggedin is not set, or is false, then run this block.
    header('Location: http://example.com/index?eject=noLogin'); // Send the user to the eject page.
    die(); // Exit the process.
}

Process summary: User logs in, which creates a session and its variables.过程摘要:用户登录,这会创建一个会话及其变量。 When the user loads a page, a session check is performed to make sure that the user's account is valid and authorised.当用户加载页面时,会执行会话检查以确保用户的帐户有效并获得授权。 If the account or session is no longer valid/authorised, then the user is redirected to the login page (index).如果帐户或会话不再有效/授权,则用户将被重定向到登录页面(索引)。

The issue: When someone who's not logged in enters http://example.com/dashboard , they are ejected using the first check (featured above).问题:当未登录的人输入http://example.com/dashboard ,他们会使用第一次检查(如上所示)被弹出。 However, if they enter http://example.com/process/ , the checks seem to count for nothing and the user is shown the page.但是,如果他们输入http://example.com/process/ ,则检查似乎没有任何意义,并且会向用户显示该页面。 This page does not just include a directory listing, but calls the http://example.com/process/index.php file to represent it instead.该页面不仅包含目录列表,还调用http://example.com/process/index.php文件来表示它。

The question: How can I apply the same logic that protects individual pages like dashboard.php , to the case of protecting directory indexes?问题:在保护目录索引的情况下,我如何应用保护单个页面(如dashboard.php的相同逻辑?


Own answer:自己的回答:

The issue here was one which was simple, but overlooked.这里的问题是一个简单但被忽视的问题。

At the top of the_header_and_menu.php file is an include to session_check.php which is located outside the site's directory.the_header_and_menu.php文件的顶部是session_check.php的包含,它位于站点目录之外。

Within the header and menu file was the session check include.在标题和菜单文件中包含会话检查。 However, because the session check was located outside the main directory (like much of the back-end), I had referenced to it through a relative path, similar to the one below.但是,因为会话检查位于主目录之外(就像大部分后端一样),所以我通过相对路径引用了它,类似于下面的路径。

include_once '../mainfolder/php/subfolder/sessioncheck.php';

However, because the file was being included to a subdirectory, it should've included a further ../ operator.但是,因为文件被包含在子目录中,所以它应该包含另一个../操作符。

include_once '../../safe/php/users/sessioncheck.php';

The solution: Instead of performing a session check through the header and menu, I am now including it on every page I want to protect.解决方案:我现在不是通过标题和菜单执行会话检查,而是将其包含在我想要保护的每个页面上。 This is by no means a perfect solution and simply acts to get things working again.这绝不是一个完美的解决方案,只是为了让事情恢复正常。

Thank you to Daniel Schmidt , who got me looking in the right direction!感谢Daniel Schmidt ,他让我找到了正确的方向!

Directory indexes don't usually come from PHP - they are served by your webserver (nginx, apache, ..).目录索引通常不是来自 PHP - 它们由您的网络服务器(nginx、apache、..)提供服务。 Today, there is obviously no need to have that indexes enabled.今天,显然没有必要启用这些索引。

It looks like you're not sending each request to you're PHP process(es).看起来您没有将每个请求发送给您的 PHP 进程。 I tend to suggest checking your webserver configuration.我倾向于建议检查您的网络服务器配置。

The issue here was one which was simple, but overlooked.这里的问题是一个简单但被忽视的问题。

At the top of the_header_and_menu.php file is an include to session_check.php which is located outside the site's directory.the_header_and_menu.php文件的顶部是session_check.php的包含,它位于站点目录之外。

Within the header and menu file was the session check include.在标题和菜单文件中包含会话检查。 However, because the session check was located outside the main directory (like much of the back-end), I had referenced to it through a relative path, similar to the one below.但是,因为会话检查位于主目录之外(就像大部分后端一样),所以我通过相对路径引用了它,类似于下面的路径。

include_once '../mainfolder/php/subfolder/sessioncheck.php';

However, because the file was being included to a subdirectory, it should've included a further ../ operator.但是,因为文件被包含在子目录中,所以它应该包含另一个../操作符。

include_once '../../safe/php/users/sessioncheck.php';

The solution: Instead of performing a session check through the header and menu, I am now including it on every page I want to protect.解决方案:我现在不是通过标题和菜单执行会话检查,而是将其包含在我想要保护的每个页面上。 This is by no means a perfect solution and simply acts to get things working again.这绝不是一个完美的解决方案,只是为了让事情恢复正常。

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

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