在php編程中編程,擷取當前url地址,以及伺服器變數,主要使用如下全域變數:$_server["query_string"],$_server["request_uri"],$_server["script_name"],$_server["php_self"] 1,$_server["query_string"]說明:查詢(query)的字串2,$_server["request_uri"]說明:訪問此頁面所需的uri3,$_server["script_name"]說明:包含當前指令碼的路徑4,$_server["php_self"]說明:當前正在執行指令碼的檔案名稱執行個體:1,http://bbs.it-home.org/ (直接開啟首頁)結果:$_server["query_string"] = ""$_server["request_uri"] = "/"$_server["script_name"] = "/index.php"$_server["php_self"] = "/index.php"2,http://bbs.it-home.org/?p=222 (附帶查詢)結果:$_server["query_string"] = "p=222"$_server["request_uri"] = "/?p=222"$_server["script_name"] = "/index.php"$_server["php_self"] = "/index.php"3,http://bbs.it-home.org/index.php?p=222&q=biuuu結果:
//處理request_uri
- if(!isset($_server['request_uri'])) {
- $_server['request_uri'] = $_server['php_self'];
- if(isset($_server['query_string'])) $_server['request_uri'] .= '?'.$_server['query_string'];
- }
- if($_server['request_uri']) {
- $temp = urldecode($_server['request_uri']);
- if(strexists($temp, '<') || strexists($temp, '"')) {
- $_get = shtmlspecialchars($_get);//xss
- }
- }
echo $_server['document_root']." "; //獲得伺服器文檔根變數
- echo $_server['php_self']."
"; //獲得執行該代碼的檔案伺服器絕對路徑的變數
- echo __file__."
"; //獲得檔案的檔案系統絕對路徑的變數
- echo dirname(__file__); //獲得檔案所在的檔案夾路徑的函數
- ?>
- //server函數
- $_server["http_referer"]=http://localhost/lianxi/
- $_server["http_accept_language"]=zh-cn
- $_server["http_accept_encoding"]=gzip, deflate
- $_server["http_user_agent"]=mozilla/4.0 (compatible; msie 6.0; windows nt 5.2; .net clr 1.1.4322; .net clr 2.0.50727)
- $_server["http_host"]=localhost
- $_server["http_connection"]=keep-alive
- $_server["path"]=c:\windows\system32;c:\windows;c:\windows\system32\wbem;c:\program files\common files\adobe\agl;c:\program files\mysql\mysql server 5.0\bin;c:\php;c:\php\ext
- $_server["systemroot"]=c:\windows
- $_server["comspec"]=c:\windows\system32\cmd.exe
- $_server["pathext"]=.com;.exe;.bat;.cmd;.vbs;.vbe;.js;.jse;.wsf;.wsh
- $_server["windir"]=c:\windows
- $_server["server_signature"]=
- apache/2.0.55 (win32) php/5.1.1 server at localhost port 80 \\使用的何伺服器
- $_server["server_software"]=apache/2.0.55 (win32) php/5.1.1
- $_server["server_name"]=localhost \\伺服器名稱
- $_server["server_addr"]=127.0.0.1
- $_server["server_port"]=80 \\伺服器連接埠
- $_server["remote_addr"]=127.0.0.1
- $_server["document_root"]=d:/lianxi \\網站的主目錄
- $_server["server_admin"]=sss@163.com \\安裝apache時設定的郵箱
- $_server["script_filename"]=d:/lianxi/lianxi/servervalues.php \\當前的網頁的絕對路徑,
- $_server["remote_port"]=1076 \\遠程連接埠
- $_server["gateway_interface"]=cgi/1.1
- $_server["server_protocol"]=http/1.1
- $_server["request_method"]=get
- $_server["query_string"]=\\擷取?號後面的內容
- $_server["request_uri"]=例子:/lianxi/servervalues.php?a=1&b=2
- $_server["script_name"]=例子:/lianxi/servervalues.php
- $_server["php_self"]=/lianxi/servervalues.php \\返回當前網頁的相對路徑.
- $_server["request_time"]=1179190013 \\已耗用時間 單位為十萬分之一毫秒
- $_server["argv"]=array
- $_server["argc"]=0
複製代碼例2:
- /**
- __file__ ,
- getcwd(),
- $_server["request_uri"],
- $_server["script_name"],
- $_server["php_self"],
- $_server["script_filename"],
-
複製代碼這些變數或函數的異同. 假設有一個請求地址為: http://localhost:8080/test.php/age=20 而test.php 的完整路徑是: d:/server/www/example/test.php 1)、getcwd() 將得到瀏覽器請求的分頁檔所在的目錄. 即test.php 檔案所在的目錄: d:/server/www/example/ , 如果在test.php 執行了 require 或 include 語句, 比如 inculde(”test_dir/test2.php”), 那麼在 test2.php 裡 getcwd()函數 返回的也將是 test.php 所在的目錄. 2)、__file__ 一個魔術變數, 用它將得到 __file__ 變數所在檔案的完整路徑, 比如: test.php 裡 __file__ 將得到 d:/server/www/example/test.php , test_dir/test2.php 裡的 __file__ 將得到 d:/server/www/example/test_dir/test2.php 3)、$_server["script_filename"] 將得到瀏覽器請求的分頁檔的完整路徑. test.php 和 test_dir/test2.php 裡用 $_server["script_name"] 都將得到 d:/server/www/example/test.php. 4)、$_server["script_name"] 將得到瀏覽器請求的分頁檔的檔案名稱,注意: 與 $_server["script_name"] 不同, 此變數只得到檔案名稱而不包含路徑, 在test.php 與 test_dir/test2.php 用$_server["script_name"] 得到的都將是 test.php. 當然, 在test.php 與 test_dir/test2.php 執行 basename($_server["script_filename"]) 與 $_server["script_name"] 相同. 執行 在test.php 與 test_dir/test2.php 執行 realpath(”test.php”) 得到的結果與 $_server["script_filename"] 相同. 5)、$_server["php_self"] 將得到瀏覽器請求頁面的檔案名稱, 並剝掉問號 ? 後的內容, 注意:不包含路徑, 比如在用戶端裡請求 http://localhost:8080/test.php?age=20&name=tom, 那麼test.php 和 test_dir/test2.php 的 $_server["php_self"] 都將得到 “test.php”。“age=20&name=tom”被剝掉。 而如果用戶端裡請求 http://localhost:8080/test.php/age=20&name=tom, 那麼test.php 和 test_dir/test2.php 的 $_server["php_self"] 都將得到 “test.php/age=20&name=tom”。 6)、$_server["request_uri"] 將得到瀏覽器請求頁面的檔案名稱, 以及檔案名稱之後的所有內容(注意: 井號 # 之後的內容將被略去), 比如在用戶端裡請求 http://localhost:8080/test.php?age=20&name=tom, 那麼test.php 和 test_dir/test2.php 的 $_server["reuest_uri"] 都將得到 “test.php”。“age=20&name=tom”被剝掉。 而如果用戶端裡請求 http://localhost:8080/test.php/age=20&name=tom, 那麼test.php 和 test_dir/test2.php 的 $_server["request_uri"] 都將得到 “test.php/age=20&name=tom”。 test.php:
- echo “test1.php variables
”;
- echo “getcwd: “, getcwd(), “
”;
- echo “__file__: “, __file__, “
”;
- echo “request_uri: “, $_server["request_uri"], “
”;
- echo “script_name: “, $_server["script_name"], “
”;
- echo “php_self: “, $_server["php_self"], “
”;
- echo “script_filename “, $_server["script_filename"] , “
”;
-
- // 把 test2.php 包含進來, 在 test2.php 裡輸出上面的變數,看有什麼不同:
- include_once(”test2/test2.php”);
- ?>
複製代碼 |