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

获取域名

程序员文章站 2022-05-23 22:22:32
...
我在看那个ecmall核心文件ecmall.php,看到这个函数写得比较清晰,拿出来分享下


  1. function get_domain() {
  2. /* 协议 */
  3. $protocol = (isset($_SERVER['HTTPS']) && (strtolower($_SERVER['HTTPS']) != 'off')) ? 'https://' : 'http://';
  4. /* 域名或IP地址 */
  5. if (isset($_SERVER['HTTP_X_FORWARDED_HOST'])) {
  6. $host = $_SERVER['HTTP_X_FORWARDED_HOST'];
  7. } elseif (isset($_SERVER['HTTP_HOST'])) {
  8. $host = $_SERVER['HTTP_HOST'];
  9. } else {
  10. /* 端口 */
  11. if (isset($_SERVER['SERVER_PORT'])) {
  12. $port = ':' . $_SERVER['SERVER_PORT'];
  13. if ((':80' == $port && 'http://' == $protocol) || (':443' == $port && 'https://' == $protocol)) {
  14. $port = '';
  15. }
  16. } else {
  17. $port = '';
  18. }
  19. if (isset($_SERVER['SERVER_NAME'])) {
  20. $host = $_SERVER['SERVER_NAME'] . $port;
  21. } elseif (isset($_SERVER['SERVER_ADDR'])) {
  22. $host = $_SERVER['SERVER_ADDR'] . $port;
  23. }
  24. }
  25. return $protocol . $host;
  26. }
复制代码
相关标签: 获取域名