[英]How do I get the root url of the site?
可能是一个微不足道的问题,但我正在寻找一种方法来获取网站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/
$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(),并将“path”作为指针,例如
$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/'
另一个简单方法:
<?php
$hostname = getenv('HTTP_HOST');
echo $hostname;
(PHP 4,PHP 5)
getenv - 获取环境变量的值
此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/
您可以通过Define
节省define.php
包括在其他项目下一次使用这个PROTOCOL
DOMAIN
PORT
SITE_ROOT
和SITE 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);
您可以调试以查看结果
您可以在第一个/
-character中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
的一部分,在第一个/
-character加上尾随/
如果第一部分不为空。 对于以/
(相对URL)开头的URL,将返回空字符串。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.