PHP 獲取當前url 路徑的函數及服務器變量
PHP 獲取當前url 路徑的函數及服務器變量:QUERY_STRING 、 REQUEST_URI 、 SCRIPT…
1, $_SERVER[" QUERY_STRING “]
說明:查詢 (query) 的字符串
2, $_SERVER[" REQUEST_URI “]
說明:訪問此頁面所需的 URI
3, $_SERVER[" SCRIPT_NAME “]
說明:包含當前腳本的路徑
4, $_SERVER[" PHP_SELF “]
說明:當前正在執行腳本的文件名
實例:
1, http://www.biuuu.com / (直接打開主頁)
結果:
$_SERVER[" QUERY_STRING “] = “"
$_SERVER[" REQUEST_URI “] = “/"
$_SERVER[" SCRIPT_NAME “] = “/index.php"
$_SERVER[" PHP_SELF “] = “/index.php"
2, http://www.biuuu.com /?p=222 ( 附帶查詢)
結果:
$_SERVER[" QUERY_STRING “] = “p=222″
$_SERVER[" REQUEST_URI “] = “/?p=222″
$_SERVER[" SCRIPT_NAME “] = “/index.php"
$_SERVER[" PHP_SELF “] = “/index.php"
3, http://www.biuuu.com /index.php?p=222&q=biuuu
結果:
$_SERVER[" QUERY_STRING “] = “p=222&q=biuuu"
$_SERVER[" REQUEST_URI “] = “/index.php?p=222&q=biuuu"
$_SERVER[" SCRIPT_NAME “] = “/index.php"
$_SERVER[" PHP_SELF “] = “/index.php"
$_SERVER[" QUERY_STRING “] 獲取查詢語句,實例中可知,獲取的是? 後面的值
$_SERVER[" REQUEST_URI “] 獲取http://www.biuuu.com 後面的值,包括/
$_SERVER[" SCRIPT_NAME “] 獲取當前腳本的路徑,如: index.php
$_SERVER[" PHP_SELF “] 當前正在執行腳本的文件名
當前url : “http://".$_SERVER[‘HTTP_HOST’].$_SERVER[‘PHP_SELF’]