PHP如何获取基本域/URL? [英] PHP how to get the base domain/url?

查看:40
本文介绍了PHP如何获取基本域/URL?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

function url(){
    if(isset($_SERVER['HTTPS'])){
        $protocol = ($_SERVER['HTTPS'] && $_SERVER['HTTPS'] != "off") ? "https" : "http";
    }
    else{
        $protocol = 'http';
    }
    return $protocol . "://" . $_SERVER['HTTP_HOST'];
}

例如,使用上面的功能,如果我使用相同的目录,它可以很好地工作,但是如果我创建一个子目录并在其中工作,它也将为我提供子目录的位置.我只想要example.com,但是如果我在文件夹sub中工作,它会给我example.com/sub.如果我使用的是主目录,则该功能可以正常工作. $_SERVER['HTTP_HOST']可以替代吗?

For example with the function above, it works fine if I work with the same directory, but if I make a sub directory, and work in it, it will give me the location of the sub directory also for example. I just want example.com but it gives me example.com/sub if I'm working in the folder sub. If I'm using the main directory,the function works fine. Is there an alternative to $_SERVER['HTTP_HOST']?

或者我该如何修复我的函数/代码以仅获取主网址?谢谢.

Or how could I fix my function/code to get the main url only? Thanks.

推荐答案

使用SERVER_NAME.

echo $_SERVER['SERVER_NAME']; //Outputs www.example.com

这篇关于PHP如何获取基本域/URL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆