Php 如何获取站点的根url?
这可能是一个微不足道的问题,但我正在寻找一种方法来表示获取站点url的根,例如:Php 如何获取站点的根url?,php,Php,这可能是一个微不足道的问题,但我正在寻找一种方法来表示获取站点url的根,例如:http://localhost/some/folder/containing/something/here/or/there应返回http://localhost/ $root = (!empty($_SERVER['HTTPS']) ? 'https' : 'http') . '://' . $_SERVER['HTTP_HOST'] . '/'; 我知道有$\u服务器['DOCUMENT\u ROOT'],但
http://localhost/some/folder/containing/something/here/or/there
应返回http://localhost/
$root = (!empty($_SERVER['HTTPS']) ? 'https' : 'http') . '://' . $_SERVER['HTTP_HOST'] . '/';
我知道有$\u服务器['DOCUMENT\u ROOT']
,但这不是我想要的
我相信这很容易,但我一直在阅读:试图找出我应该使用或调用什么
想法
我的另一个问题,与此相关的是-答案会是什么,在http://subsite.localhost/some/folder/containing/something/here/or/there
所以我的最终结果是http://subsite.localhost/
$root = (!empty($_SERVER['HTTPS']) ? 'https' : 'http') . '://' . $_SERVER['HTTP_HOST'] . '/';
如果您对当前脚本的方案和主机感兴趣
否则,请按照前面的建议解析_url()。e、 g
$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/'
另一个简单的方法:
<?php
$hostname = getenv('HTTP_HOST');
echo $hostname;
您可以preg\u 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){
$urlParts=preg_split('#)(?此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:
使用别名:
对于子目录:
您可以使用Define
save onDefine.php
include,以便下次在其他项目上使用
这是协议
域
端口
站点根
和站点路径
**
* 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);
您可以调试以查看结果如果您的站点只有一个入口点和/或始终包含一个可使用的配置文件,请查看
我不明白如何设置它来满足我的需求。有什么想法吗?对不起。但是谢谢你的帮助。@Adam我已经用parse_url()的例子修改了上面的答案。这真的很简单-它给你一个包含所有URL组件的关联数组,你可以选择你想要的。在你的例子中,scheme和host。@boen_robot:做得好,但不要遗漏用户名、端口号等。可能在那里,看@Wrikken好吧,Adam似乎并不特别想要,但我想是为了其他人正在阅读…我已经修改了上面的答案,以包括一个涵盖您正在谈论的场景的示例。那些收到错误未定义索引的人:HTTPS
请按照下面的链接执行。我删除了您到localhost的链接,其他在不同计算机上访问internet的人无法访问您r localhost框。另外,您的/myweb.com/tests/的其他链接也无法解析。很好,很小,但是它不包含http
位
#config.php
echo pathUrl(__DIR__ . '/../');
//http://example.com/alias-name/shop/
**
* 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);