[英]PHP how to clone $_SERVER['REQUEST_URI']
編輯:感謝您的回答,我意識到,當我使用FallbackResource從index.php提供所有頁面時,每次加載腳本時_$SERVER['REQUEST_URI']
都會被更新。 我將把根文件夾存儲在一個文本文件或數據庫條目中,以供index.php訪問。
再次感謝
我想知道如何在調用它時更新時從$GLOBALS['rooturi'] = $_SERVER['REQUEST_URI']
獲取固定值。
我在index.php中設置它,以設置用於在另一個腳本中包含圖像的根路徑。 我嘗試使用__DIR__."/images/"
但我不希望文件系統位置(C:\\ xampp \\ htdocs \\ file1 \\ file2 \\ website \\ images)而是URL版本(/ website / images /)
$GLOBALS['rooturi'] = $_SERVER['REQUEST_URI'];
問題是,當我在另一個腳本中調用它時,它將使用新腳本$_SERVER['REQUEST_URI']
即。 (網站/圖像/ 3 /圖像/ 3)
我正在使用.htaccess和FallbackResource index.php
提前致謝
在每個腳本開頭包含的文件中聲明全局變量,例如在配置文件中。
/site
/site/config.php
/site/index.php
/site/subdir
/site/subdir/anotherfile.php
例如:
config.php文件
<?php
$cfg = new stdClass();
$cfg->rooturi = $_SERVER['HTTP_HOST'].'/siteroot';
?>
的index.php
<?php
require_once('config.php');
echo $cfg->rooturi;
function somefunction(){
global $cfg;
echo 'In function '.$cfg->rooturi;
}
?>
子目錄/ anotherfile.php
<?php
require_once('../config.php'); // see '../' to go one level above as this file is in a subdirectory
echo $cfg->rooturi;
function somefunction(){
global $cfg;
echo 'In function '.$cfg->rooturi;
}
?>
..
您可以在index.php
嘗試:
$path = str_replace('index.php', '', $_SERVER['PHP_SELF']);
$baseUrl = 'http://' . $_SERVER['SERVER_NAME'] . $path;
$GLOBALS['rooturi'] = $baseUrl;
然后加載圖像:
<img src="<?php echo $GLOBALS['rooturi'] ?>/images/something.png" />
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.