简体   繁体   English

仅限制已登录用户的内容访问

[英]Restrict content access only to Logged In users

I have a folder named cache . 我有一个名为cache的文件夹。 It has sub-folders and files. 它具有子文件夹和文件。 I need to make cache content accessible only when isset($_SESSION["logged"]) . 我需要使cache内容仅在isset($_SESSION["logged"])

I have routed all requests to cache folder via index.php by placing the following .htaccess file in cache folder : 通过将以下.htaccess文件放置在缓存文件夹中,我已将所有请求通过index.php路由到缓存文件夹:

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteRule ^ index.php [L]
</IfModule>

In index.php following check is written : index.php中,编写了以下检查:

<?php session_start();
if (!isset($_SESSION["logged"])) {
    die();
} else {
    header('Location: ' . $_SERVER['REQUEST_URI']);
    die;
}

But I am geting error :: 但是我得到了错误::

This web page has a redirect loop

ERR_TOO_MANY_REDIRECTS

Could you please debug where I am wrong. 你能调试我错了吗?

The reason why you get this error simply is that you explicitly implemented an endless loop. 出现此错误的原因很简单,就是您明确实现了一个无限循环。 Your header() call redirects back to the same URL originally requested, so the rewriting rule applies again, things start all over again. 您的header()调用将重定向回原始请求的相同URL,因此重写规则再次适用,事情重新开始。

Instead you should output the contents of the requested cache file: 相反,您应该输出所请求的缓存文件的内容:

<?php 
session_start();
$pathToCachedLocation = '/some/path' . $_SERVER['REQUEST_URI'];
if (isset($_SESSION["logged"]) && file_exists($pathToCacheLocation)) {
    readfile($pathToCacheLocation);
}

You will still have to add some additional validation and error handling to make sure the request targets a file actually inside the physical cache location (see realpath() ) and read permission exists (see is_readable() ). 您仍然必须添加一些其他的验证和错误处理,以确保请求的目标实际上是物理高速缓存位置内部的文件(请参阅realpath() )并且存在读取权限(请参见is_readable() )。 Also some http headers probably make sense sending. 另外,一些http标头可能有意义。 The above example is kept simple to demonstrate the approach. 上面的示例保持简单以演示该方法。

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

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