在我的項目中,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');
這工作OK!
然而,在文件$ PROJECT_ROOT /模塊/ text.php:
<?php
define('authorized',TRUE);
require_once('../lib/functions.php');
這是不正確的重定向,因爲它是不位於$ PROJECT_ROOT目錄。
從this question我知道無法獲取「functions.php」的URI文件夾。
但是,我能做些什麼來解決這個問題?
顯然我可以嘗試重定向絕對路徑,但這會損害我的項目的可移植性。