当前位置:Gxlcms > PHP教程 > 获取域名

获取域名

时间:2021-07-01 10:21:17 帮助过:26人阅读

我在看那个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. }

人气教程排行