【发布时间】:2017-03-04 20:28:10
【问题描述】:
在我的项目中,PHP 中的一个文件检查身份验证。
$PROJECT_ROOT/lib/functions.php:
<?php
check_authentication();
function check_authentication(){
if (session_status() == PHP_SESSION_NONE) {
session_start();
}
if(defined('authorized') && authorized){
if(!(isset($_SESSION['admin'] && $_SESSION['admin']))){
header('Location: login.php');
exit(0);
}
}
}
check_authentication 函数检查用户是否已通过身份验证,如果没有,则重定向到 $PROJECT_ROOT/login.php 页面。
在 $PROJECT_ROOT/reader.php 文件中:
<?php
define('authorized',TRUE);
require_once('lib/functions.php');
这工作正常!
但是在 $PROJECT_ROOT/modules/text.php 文件中:
<?php
define('authorized',TRUE);
require_once('../lib/functions.php');
这没有正确重定向,因为它不位于 $PROJECT_ROOT 目录中。
来自this question我了解到无法获取“functions.php”的URI文件夹。
但是我能做些什么来解决这个问题呢?
显然我可以尝试使用绝对路径进行重定向,但这会损害我的项目的可移植性。
【问题讨论】: