php的web路徑擷取

來源:互聯網
上載者:User
  1. class HttpTool
  2. {
  3. /**
  4. * //擷取網域名稱或主機地址
  5. * #測試網址: http://localhost:8081/test/testurl.php?id=5
  6. * 返回 localhost:8081
  7. */
  8. public function getHost()
  9. {
  10. return $_SERVER['HTTP_HOST'];
  11. }
  12. /**
  13. * 當前頁面的url(包括參數)
  14. */
  15. public function getWebUrl()
  16. {
  17. $pageURL = 'http';
  18. if (isset($_SERVER["HTTPS"]) && $_SERVER["HTTPS"] == "on")
  19. {
  20. $pageURL .= "s";
  21. }
  22. $pageURL .= "://";
  23. $pageURL .= $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
  24. return $pageURL;
  25. }
  26. /**
  27. *
  28. * 當前頁面的url(不包括參數)
  29. */
  30. public function getWebPath()
  31. {
  32. $pageURL = 'http';
  33. if (isset($_SERVER["HTTPS"]) && $_SERVER["HTTPS"] == "on")
  34. {
  35. $pageURL .= "s";
  36. }
  37. $pageURL .= "://";
  38. $pageURL .= $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];
  39. return $pageURL;
  40. }
  41. /**
  42. * 當前頁面的父路徑
  43. */
  44. public function getWebParentPath()
  45. {
  46. $pageURL = 'http';
  47. if (isset($_SERVER["HTTPS"]) && $_SERVER["HTTPS"] == "on")
  48. {
  49. $pageURL .= "s";
  50. }
  51. $pageURL .= "://";
  52. $pageURL .= $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];
  53. $pageURL = substr($pageURL, 0, strrpos($pageURL, "/"));
  54. return $pageURL;
  55. }
  56. /**
  57. * 伺服器名稱
  58. */
  59. public function getServerName()
  60. {
  61. return $_SERVER['SERVER_NAME'];
  62. }
  63. /**
  64. * 連接埠
  65. */
  66. public function getServerPort()
  67. {
  68. return $_SERVER["SERVER_PORT"];
  69. }
  70. /**
  71. * 連結參數,問號?後的參數
  72. */
  73. public function getQueryString()
  74. {
  75. return $_SERVER['QUERY_STRING'];
  76. }
  77. /**
  78. * 請求地址,傳回值不host內容
  79. */
  80. public function getRequestUri()
  81. {
  82. return $_SERVER['REQUEST_URI'];
  83. }
  84. }
  85. $http = new HttpTool();
  86. echo "host===============".$http->getHost() . "
    ";
  87. echo "weburl=============".$http->getWebUrl() . "
    ";
  88. echo "webPath============".$http->getWebPath() . "
    ";
  89. echo "getWebParentPath===".$http->getWebParentPath() . "
    ";
  90. echo "getServerName======".$http->getServerName() . "
    ";
  91. echo "getServerPort======".$http->getServerPort() . "
    ";
  92. echo "getQueryString=====".$http->getQueryString() . "
    ";
  93. echo "getRequestUri======".$http->getRequestUri() . "
    ";
  94. ?>
複製代碼
測試地址:http://localhost:8081/test/httptool.php?name=penngo

輸出結果:

host===============localhost:8081
weburl=============http://localhost:8081/test/httptool.php?name=penngo
webPath============http://localhost:8081/test/httptool.php
getWebParentPath===http://localhost:8081/test
getServerName======localhost
getServerPort======8081
getQueryString=====name=penngo
getRequestUri======/test/httptool.php?name=penngo

php, web
  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.