php擷取當前url路徑 php伺服器變數

來源:互聯網
上載者:User

在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結果:

  1. //處理request_uri

  2. if(!isset($_server['request_uri'])) {
  3. $_server['request_uri'] = $_server['php_self'];
  4. if(isset($_server['query_string'])) $_server['request_uri'] .= '?'.$_server['query_string'];
  5. }
  6. if($_server['request_uri']) {
  7. $temp = urldecode($_server['request_uri']);
  8. if(strexists($temp, '<') || strexists($temp, '"')) {
  9. $_get = shtmlspecialchars($_get);//xss
  10. }
  11. }

  12. echo $_server['document_root']."
    "; //獲得伺服器文檔根變數

  13. echo $_server['php_self']."
    "; //獲得執行該代碼的檔案伺服器絕對路徑的變數
  14. echo __file__."
    "; //獲得檔案的檔案系統絕對路徑的變數
  15. echo dirname(__file__); //獲得檔案所在的檔案夾路徑的函數
  16. ?>
  17. //server函數
  18. $_server["http_referer"]=http://localhost/lianxi/
  19. $_server["http_accept_language"]=zh-cn
  20. $_server["http_accept_encoding"]=gzip, deflate
  21. $_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)
  22. $_server["http_host"]=localhost
  23. $_server["http_connection"]=keep-alive
  24. $_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
  25. $_server["systemroot"]=c:\windows
  26. $_server["comspec"]=c:\windows\system32\cmd.exe
  27. $_server["pathext"]=.com;.exe;.bat;.cmd;.vbs;.vbe;.js;.jse;.wsf;.wsh
  28. $_server["windir"]=c:\windows
  29. $_server["server_signature"]=
  30. apache/2.0.55 (win32) php/5.1.1 server at localhost port 80 \\使用的何伺服器
  31. $_server["server_software"]=apache/2.0.55 (win32) php/5.1.1
  32. $_server["server_name"]=localhost \\伺服器名稱
  33. $_server["server_addr"]=127.0.0.1
  34. $_server["server_port"]=80 \\伺服器連接埠
  35. $_server["remote_addr"]=127.0.0.1
  36. $_server["document_root"]=d:/lianxi \\網站的主目錄
  37. $_server["server_admin"]=sss@163.com \\安裝apache時設定的郵箱
  38. $_server["script_filename"]=d:/lianxi/lianxi/servervalues.php \\當前的網頁的絕對路徑,
  39. $_server["remote_port"]=1076 \\遠程連接埠
  40. $_server["gateway_interface"]=cgi/1.1
  41. $_server["server_protocol"]=http/1.1
  42. $_server["request_method"]=get
  43. $_server["query_string"]=\\擷取?號後面的內容
  44. $_server["request_uri"]=例子:/lianxi/servervalues.php?a=1&b=2
  45. $_server["script_name"]=例子:/lianxi/servervalues.php
  46. $_server["php_self"]=/lianxi/servervalues.php \\返回當前網頁的相對路徑.
  47. $_server["request_time"]=1179190013 \\已耗用時間 單位為十萬分之一毫秒
  48. $_server["argv"]=array
  49. $_server["argc"]=0

複製代碼

例2:

  1. /**
  2. __file__ ,
  3. getcwd(),
  4. $_server["request_uri"],
  5. $_server["script_name"],
  6. $_server["php_self"],
  7. $_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:

  1. echo “test1.php variables
    ”;
  2. echo “getcwd: “, getcwd(), “
    ”;
  3. echo “__file__: “, __file__, “
    ”;
  4. echo “request_uri: “, $_server["request_uri"], “
    ”;
  5. echo “script_name: “, $_server["script_name"], “
    ”;
  6. echo “php_self: “, $_server["php_self"], “
    ”;
  7. echo “script_filename “, $_server["script_filename"] , “
    ”;
  8. // 把 test2.php 包含進來, 在 test2.php 裡輸出上面的變數,看有什麼不同:
  9. include_once(”test2/test2.php”);
  10. ?>
複製代碼
  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.