34

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

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

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

想法?

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

4

5 回答 5

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

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

否则,如前所述,parse_url()。例如

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

如果您还对路径之前的其他 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/'
于 2013-08-13T23:36:33.820 回答
17

另一种简单的方法:

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

获取环境

(PHP 4,PHP 5)

getenv — 获取环境变量的值

于 2013-08-13T23:42:16.677 回答
13

这个 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;
}

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

#index.php

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

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

#index.php

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

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

#config.php

echo pathUrl(__DIR__ . '/../');
//http://example.com/alias-name/shop/
于 2016-03-19T11:22:15.203 回答
3

您可以使用Definesave on define.phpinclude 以便下次在其他项目上使用 这是PROTOCOL DOMAIN PORT SITE_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);

您可以调试以查看结果

于 2016-03-19T15:30:24.337 回答
1

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

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

// examples:

echo rootUrl('http://user@www.example.com/path/to/file?query=string');
// output: http://user@www.example.com/

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]

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

于 2015-03-17T07:51:52.160 回答