在PHP中,$_SERVER
是一個(gè)預(yù)定義的全局?jǐn)?shù)組,它包含了有關(guān)頭、路徑和腳本位置等服務(wù)器信息。要獲取服務(wù)器配置信息,您可以使用$_SERVER
數(shù)組中的相關(guān)元素。以下是一些常見的服務(wù)器配置信息及其對(duì)應(yīng)的$_SERVER
元素:
$_SERVER['SERVER_NAME']
$_SERVER['SERVER_PORT']
$_SERVER['DOCUMENT_ROOT']
$_SERVER['SERVER_SOFTWARE']
$_SERVER['SERVER_OS']
$_SERVER['UPLOAD_TMP_DIR']
$_SERVER['SCRIPT_FILENAME']
$_SERVER['RewriteEngine']
(僅在啟用了URL重寫時(shí)才可用)$_SERVER['HTTP_HOST']
$_SERVER['REQUEST_METHOD']
(例如:GET、POST、PUT等)以下是一個(gè)示例,展示了如何使用$_SERVER
數(shù)組獲取服務(wù)器配置信息:
<?php
// 獲取服務(wù)器名稱
$server_name = $_SERVER['SERVER_NAME'];
// 獲取服務(wù)器端口號(hào)
$server_port = $_SERVER['SERVER_PORT'];
// 獲取文檔根目錄
$document_root = $_SERVER['DOCUMENT_ROOT'];
// 獲取服務(wù)器軟件
$server_software = $_SERVER['SERVER_SOFTWARE'];
// 獲取服務(wù)器操作系統(tǒng)
$server_os = $_SERVER['SERVER_OS'];
// 獲取上傳臨時(shí)目錄
$upload_tmp_dir = $_SERVER['UPLOAD_TMP_DIR'];
// 獲取CGI腳本路徑
$script_filename = $_SERVER['SCRIPT_FILENAME'];
// 獲取URL重寫規(guī)則
$rewrite_engine = isset($_SERVER['RewriteEngine']) ? $_SERVER['RewriteEngine'] : 'Off';
// 獲取HTTP主機(jī)
$http_host = $_SERVER['HTTP_HOST'];
// 獲取HTTP方法
$request_method = $_SERVER['REQUEST_METHOD'];
// 輸出服務(wù)器配置信息
echo "服務(wù)器名稱: " . $server_name . "<br>";
echo "服務(wù)器端口號(hào): " . $server_port . "<br>";
echo "文檔根目錄: " . $document_root . "<br>";
echo "服務(wù)器軟件: " . $server_software . "<br>";
echo "服務(wù)器操作系統(tǒng): " . $server_os . "<br>";
echo "上傳臨時(shí)目錄: " . $upload_tmp_dir . "<br>";
echo "CGI腳本路徑: " . $script_filename . "<br>";
echo "URL重寫規(guī)則: " . $rewrite_engine . "<br>";
echo "HTTP主機(jī): " . $http_host . "<br>";
echo "HTTP方法: " . $request_method . "<br>";
?>
請(qǐng)注意,$_SERVER
數(shù)組中的元素可能在不同的服務(wù)器和配置中有所不同。因此,在使用這些值時(shí),請(qǐng)確保根據(jù)您的實(shí)際情況進(jìn)行調(diào)整。