php 如何获取网站的根网址?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18220977/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 17:11:56  来源:igfitidea点击:

How do I get the root url of the site?

php

提问by Adam

Might be a trivial question, but I am looking for a way to say get the root of a site url, for example: http://localhost/some/folder/containing/something/here/or/thereshould return http://localhost/

可能是一个微不足道的问题,但我正在寻找一种方法来说明获取站点 url 的根目录,例如:http://localhost/some/folder/containing/something/here/or/there应该返回http://localhost/

I know there is $_SERVER['DOCUMENT_ROOT']but that's not what I want.

我知道有,$_SERVER['DOCUMENT_ROOT']但这不是我想要的。

I am sure this is easy, but I have been reading: This posttrying to figure out what i should use or call.

我相信这很容易,但我一直在阅读:这篇文章试图弄清楚我应该使用什么或调用什么。

Ideas?

想法?

The other question I have, which is in relation to this one is - will what ever the answer be, work on sites like http://subsite.localhost/some/folder/containing/something/here/or/thereso my end result is http://subsite.localhost/

我的另一个问题,与这个问题有关 - 无论答案是什么,在像http://subsite.localhost/some/folder/containing/something/here/or/there这样的网站上工作,我的最终结果是http://subsite.localhost/

回答by boen_robot

$root = (!empty($_SERVER['HTTPS']) ? 'https' : 'http') . '://' . $_SERVER['HTTP_HOST'] . '/';

If you're interested in the current script's scheme and host.

如果您对当前脚本的方案和主机感兴趣。

Otherwise, parse_url(), as already suggested. e.g.

否则,按照已经建议的 parse_url()。例如

$parsedUrl = parse_url('http://localhost/some/folder/containing/something/here/or/there');
$root = $parsedUrl['scheme'] . '://' . $parsedUrl['host'] . '/';

If you're also interested in other URL components prior to the path (e.g. credentials), you could also use strstr() on the full URL, with the "path" as the needle, e.g.

如果您还对路径之前的其他 URL 组件(例如凭据)感兴趣,您还可以在完整 URL 上使用 strstr(),以“路径”作为指针,例如

$url = 'http://user:pass@localhost:80/some/folder/containing/something/here/or/there';
$parsedUrl = parse_url($url);
$root = strstr($url, $parsedUrl['path'], true) . '/';//gives 'http://user:pass@localhost:80/'

回答by daniel__

Another simple way:

另一种简单的方法:

<?php
$hostname = getenv('HTTP_HOST');
echo $hostname;

getenv

获取环境

(PHP 4, PHP 5)

(PHP 4, PHP 5)

getenv — Gets the value of an environment variable

getenv — 获取环境变量的值

回答by fallinov

This PHP function returns the real URL of a full path.

这个 PHP 函数返回完整路径的真实 URL。

function pathUrl($dir = __DIR__){

    $root = "";
    $dir = str_replace('\', '/', realpath($dir));

    //HTTPS or HTTP
    $root .= !empty($_SERVER['HTTPS']) ? 'https' : 'http';

    //HOST
    $root .= '://' . $_SERVER['HTTP_HOST'];

    //ALIAS
    if(!empty($_SERVER['CONTEXT_PREFIX'])) {
        $root .= $_SERVER['CONTEXT_PREFIX'];
        $root .= substr($dir, strlen($_SERVER[ 'CONTEXT_DOCUMENT_ROOT' ]));
    } else {
        $root .= substr($dir, strlen($_SERVER[ 'DOCUMENT_ROOT' ]));
    }

    $root .= '/';

    return $root;
}

Call of pathUrl in this file : http://example.com/shop/index.php

此文件中的 pathUrl 调用:http: //example.com/shop/index.php

#index.php

echo pathUrl();
//http://example.com/shop/

Work with alias : http://example.com/alias-name/shop/index.php

使用别名:http: //example.com/alias-name/shop/index.php

#index.php

echo pathUrl();
//http://example.com/alias-name/shop/

For sub directory : http://example.com/alias-name/shop/inc/config.php

对于子目录:http: //example.com/alias-name/shop/inc/config.php

#config.php

echo pathUrl(__DIR__ . '/../');
//http://example.com/alias-name/shop/

回答by Dinh Phong

You can using Definesave on define.phpinclude for next time use on other project This is PROTOCOLDOMAINPORTSITE_ROOTAND SITE PATH

您可以使用Definesave on define.phpinclude 下次在其他项目上使用 这是PROTOCOLDOMAINPORTSITE_ROOTANDSITE PATH

**
 * domain
 * ex: localhost, maskphp.com, demo.maskphp.com,...
 */
define('DOMAIN', isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : $_SERVER['SERVER_NAME']);

/**
 * protocol
 * ex: http, https,...
 */
define('PROTOCOL', isset($_SERVER['HTTPS']) && ($_SERVER['HTTPS'] === 'on' || $_SERVER['HTTPS'] === 1)
    || isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] === 'https' ? 'https' : 'http');

/**
 * port
 * ex: 80, 8080,...
 */
define('PORT', $_SERVER['SERVER_PORT']);

/**
 * site path
 * ex: http://localhost/maskphp/ -> /maskphp/
 */
define('SITE_PATH', preg_replace('/index.php$/i', '', $_SERVER['PHP_SELF']));

/**
 * site root
 * ex: http://maskgroup.com, http://localhost/maskphp/,...
 */
define('SITE_ROOT', PROTOCOL . '://' . DOMAIN . (PORT === '80' ? '' : ':' . PORT) . SITE_PATH);

You can debug to see result

您可以调试以查看结果

回答by x-ray

You can preg_splitthe URL at the first single /-character:

您可以preg_split将 URL 放在第一个单/字符处:

function rootUrl($url) {
  $urlParts = preg_split('#(?<!/)/(?!/)#', $url, 2);
  return $urlParts[0] != '' ? $urlParts[0] . '/' : '';
}

// examples:

echo rootUrl('http://[email protected]/path/to/file?query=string');
// output: http://[email protected]/

echo rootUrl('https://www.example.com/');
// output: https://www.example.com/

echo rootUrl('https://www.example.com');
// output: https://www.example.com/

echo rootUrl('example.com/path/to/file');
// output: example.com/

echo rootUrl('/path/to/file');
// output: [empty]

echo rootUrl('');
// output: [empty]

The function rootUrl($url)returns the part of the given string $urlbefore the first /-character plus a trailing /if that first part is not empty. For URLs starting with a /(relative URLs) an empty string is returned.

该函数rootUrl($url)返回给定字符串$url的第一个字符之前的部分,如果第一部分不为空,则返回/一个尾随/。对于以/(相对 URL)开头的URL,将返回一个空字符串。