直接URL访问PHP保护目录 [英] PHP protect directory from direct URL access

查看:192
本文介绍了直接URL访问PHP保护目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个管理脚本。

I have an ADMIN script.

admin/index.php

所有的活动都是通过这个的index.php 文件来完成。
用户登录在获得该程序的功能访问之前。
$ _ SESSION ['user_authenticated'] 创建并设置为真的。照片

All activity is done through this index.php file.
Users are logging in before gaining access to the program functionality.
$_SESSION['user_authenticated'] is created and set to true.

admin/template/..

此文件夹包含图片 CSS 的JavaScript 文件。
他们只是在这个ADMIN使用。 (在后台只)的

This folder contains images, css, javascript files.
They are used only within this ADMIN. (in the backend only)

的问题:

我需要从管理​​/模板/..目录加以保护,防止直接进入。照片应该是可用只有通过身份验证的用户。

I need all the content from admin/template/.. directory to be protected against direct access.
It should be available only to authenticated users.

我想必须有一个的.htaccess 重定向请求 check_session_auth_variable.php ,它看起来如果 $ _ SESSION ['user_authenticated'] 真正错误重定向到请求的文件或< STRONG>抛出一个404错误

I guess there has to be a .htaccess redirecting requests to check_session_auth_variable.php, which looks if $_SESSION['user_authenticated'] is true or false and redirects to requested file or throws a 404 error?

我知道,最好的办法是把Web根目录以外的目录,但对我来说,我需要不断的目录结构,而不做修改。

推荐答案

管理/ htaccess的:

RewriteCond %{REQUEST_FILENAME} !check_auth.php
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule .* check_auth.php?file=$0 [QSA,L] # pass everything thru php

管理/ check_auth.php:

$file = $_GET['file'];
if($_SESSION['user_authenticated']) {
    // please mind you need to add extra security checks here (see comments below)
    readfile($file); // if it's php include it. you may need to extend this code
}else{
   // bad auth error
}

这篇关于直接URL访问PHP保护目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆