Home  >  Article  >  Backend Development  >  Get the web path of php

Get the web path of php

WBOY
WBOYOriginal
2016-07-25 08:42:511285browse
  1. class HttpTool
  2. {
  3. /**
  4. * //Get the domain name or host address
  5. * #Test URL: http://localhost:8081/test/testurl.php?id=5
  6. * Return to localhost:8081
  7. */
  8. public function getHost()
  9. {
  10. return $_SERVER['HTTP_HOST'];
  11. }
  12. /**
  13. * The url of the current page (including parameters)
  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 of the current page (excluding parameters)
  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. * The parent path of the current page
  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. * Server name
  58. */
  59. public function getServerName()
  60. {
  61. return $_SERVER['SERVER_NAME'];
  62. }
  63. /**
  64. * port
  65. */
  66. public function getServerPort()
  67. {
  68. return $_SERVER["SERVER_PORT"];
  69. }
  70. /**
  71. * Link parameters, parameters after question mark?
  72. */
  73. public function getQueryString()
  74. {
  75. return $_SERVER['QUERY_STRING'];
  76. }
  77. /**
  78. *Request address, return value does not host content
  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


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn