欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

php的web路径获取

程序员文章站 2022-05-16 10:29:44
...
  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
相关标签: php的web路径获取