php PHP如何获取基本域/ url?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17201170/
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
PHP how to get the base domain/url?
提问by ZZPLKF
function url(){
if(isset($_SERVER['HTTPS'])){
$protocol = ($_SERVER['HTTPS'] && $_SERVER['HTTPS'] != "off") ? "https" : "http";
}
else{
$protocol = 'http';
}
return $protocol . "://" . $_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.combut it gives me example.com/subif 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']?
例如,对于上面的函数,如果我在同一个目录下工作,它就可以正常工作,但是如果我创建一个子目录并在其中工作,它也会为我提供子目录的位置,例如。我只是想要,example.com但example.com/sub如果我在文件夹中工作,它会给我sub。如果我使用主目录,则该功能可以正常工作。有替代品$_SERVER['HTTP_HOST']吗?
Or how could I fix my function/code to get the main url only? Thanks.
或者如何修复我的函数/代码以仅获取主 URL?谢谢。
回答by Bad Wolf
Use SERVER_NAME.
使用SERVER_NAME.
echo $_SERVER['SERVER_NAME']; //Outputs www.example.com
回答by ZZPLKF
You could use PHP's parse_url()function
你可以使用 PHP 的parse_url()函数
function url($url) {
$result = parse_url($url);
return $result['scheme']."://".$result['host'];
}
回答by barbushin
Shortest solution:
最短的解决方案:
$domain = parse_url('http://google.com', PHP_URL_HOST);
回答by Jasmeen
/**
* Suppose, you are browsing in your localhost
* http://localhost/myproject/index.php?id=8
*/
function getBaseUrl()
{
// output: /myproject/index.php
$currentPath = $_SERVER['PHP_SELF'];
// output: Array ( [dirname] => /myproject [basename] => index.php [extension] => php [filename] => index )
$pathInfo = pathinfo($currentPath);
// output: localhost
$hostName = $_SERVER['HTTP_HOST'];
// output: http://
$protocol = strtolower(substr($_SERVER["SERVER_PROTOCOL"],0,5))=='https'?'https':'http';
// return: http://localhost/myproject/
return $protocol.'://'.$hostName.$pathInfo['dirname']."/";
}
回答by Get Off My Lawn
Use parse_url()like this:
parse_url()像这样使用:
function url(){
if(isset($_SERVER['HTTPS'])){
$protocol = ($_SERVER['HTTPS'] && $_SERVER['HTTPS'] != "off") ? "https" : "http";
}
else{
$protocol = 'http';
}
return $protocol . "://" . parse_url($_SERVER['REQUEST_URI'], PHP_URL_HOST);
}
Here is another shorter option:
这是另一个较短的选项:
function url(){
$pu = parse_url($_SERVER['REQUEST_URI']);
return $pu["scheme"] . "://" . $pu["host"];
}
回答by Dibyendu Mitra Roy
Step-1
第1步
First trim the trailing backslash (/) from the URL. For example, If the URL is http://www.google.com/then the resultant URL will be http://www.google.com
首先修剪 URL 中的尾部反斜杠 (/)。例如,如果 URL 是http://www.google.com/,那么结果 URL 将是http://www.google.com
$url= trim($url, '/');
Step-2
第2步
If scheme not included in the URL, then prepend it. So for example if the URL is www.google.com then the resultant URL will be http://www.google.com
如果 URL 中未包含方案,则将其添加到前面。例如,如果 URL 是 www.google.com,那么结果 URL 将是http://www.google.com
if (!preg_match('#^http(s)?://#', $url)) {
$url = 'http://' . $url;
}
Step-3
步骤 3
Get the parts of the URL.
获取 URL 的部分。
$urlParts = parse_url($url);
Step-4
第四步
Now remove www. from the URL
现在删除www。从网址
$domain = preg_replace('/^www\./', '', $urlParts['host']);
Your final domain without http and www is now stored in $domain variable.
没有 http 和 www 的最终域现在存储在 $domain 变量中。
Examples:
例子:
http://www.google.com=> google.com
http://www.google.com=> google.com
https://www.google.com=> google.com
https://www.google.com=> google.com
www.google.com => google.com
www.google.com => google.com
http://google.com=> google.com
http://google.com=> google.com
回答by Fadi
2 lines to solve it
2行解决它
$actual_link = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http") . "://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
$myDomain = preg_replace('/^www\./', '', parse_url($actual_link, PHP_URL_HOST));
回答by Aasif khan
/* Get sub domain or main domain url
* $url is $_SERVER['SERVER_NAME']
* $index int remove subdomain if acceess from sub domain my current url is https://support.abcd.com ("support" = 7 (char))
* $subDomain string
* $issecure string https or http
* return url
* call like echo getUrl($_SERVER['SERVER_NAME'],7,"payment",true,false);
* out put https://payment.abcd.com
* second call echo getUrl($_SERVER['SERVER_NAME'],7,null,true,true);
*/
function getUrl($url,$index,$subDomain=null,$issecure=false,$www=true) {
//$url=$_SERVER['SERVER_NAME']
$protocol=($issecure==true) ? "https://" : "http://";
$url= substr($url,$index);
$www =($www==true) ? "www": "";
$url= empty($subDomain) ? $protocol.$url :
$protocol.$www.$subDomain.$url;
return $url;
}
回答by Rudolph
This works fine if you want the http protocol also since it could be http or https.
$domainURL = $_SERVER['REQUEST_SCHEME']."://".$_SERVER['SERVER_NAME'];
如果您还想要 http 协议,这可以正常工作,因为它可以是 http 或 https。
$domainURL = $_SERVER['REQUEST_SCHEME']."://".$_SERVER['SERVER_NAME'];

