-1

我已按照网上众多指南之一使用 PHP 和 MySQL 创建登录系统。它工作正常;您必须登录,然后您将被重定向到/private目录。在其中有一个index.php文件,必须检查您是否已通过身份验证,以防止有人直接浏览该/private目录。如果不是,您将被重定向回登录屏幕。但是,如果您浏览到 /private 中的任何其他文件或目录(例如/private/myprivatefile.html),即使您没有登录,您也不会被重定向。您能帮帮我吗?

4

2 回答 2

2

HTML 文件通常不执行任何脚本并被视为静态页面。如果要保护这些文件,则可能需要使用 Web 服务器来保护目录。

另一种方法是将静态 HTML 文件移动到 Web 服务器无法访问的目录中,并使用 PHP 检索 HTML 文件的竞争。这样,您的 PHP 脚本现在将始终在请求静态内容时运行。例如,您将链接到静态内容,例如:

/private/index.php?file=myprivatefile.html

在 PHP 文件中,您将从 CGI 变量文件中提取文件请求,然后提取其内容并将其返回给请求客户端。你会在 PHP 文件中做这样的事情:

<?php
   if( CheckAuthentication() )
   {
     $fileName = $_REQUEST['file'];
     $filePath = '{path to protected files}';
     if( is_file( $filePath . $fileName ) )
     {
       readfile( filePath . $fileName );
     }// Endif check if file exists on server
   }// Endif Check user authentication
于 2013-10-11T17:12:41.450 回答
1

您可能需要使用 mod_rewrite 将所有请求重定向到 PHP 脚本,然后在该脚本中,根据用户是否经过身份验证来处理请求。

这是一个简化的例子。

.htaccess

RewriteRule (.*) load.php

加载.php

<?php

    // include your auth scripts

    $file = $_SERVER['REQUEST_URI'];

    // parse $file to extract the requested file

    if ($authenticated && file_exists($file)) {
        readfile($file);
    }
于 2013-10-11T17:05:45.540 回答