php 什么 $_SERVER 变量提供完整的 URL
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8389195/
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
What $_SERVER variable provide full URL
提问by user4951
Possible Duplicate:
getting current URL
PHP and invoking url?
可能重复:
获取当前 URL
PHP 并调用 url?
Say somebody is looking for
说有人在找
http://subdomain.domainname.com/somedirectory/somefile.htm
http://subdomain.domainname.com/somedirectory/somefile.htm
What $_SERVER variable contain http://subdomain.domainname.com/somedirectory/somefile.htm
$_SERVER 变量包含http://subdomain.domainname.com/somedirectory/somefile.htm
回答by DaveRandom
// Get HTTP/HTTPS (the possible values for this vary from server to server)
$myUrl = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] && !in_array(strtolower($_SERVER['HTTPS']),array('off','no'))) ? 'https' : 'http';
// Get domain portion
$myUrl .= '://'.$_SERVER['HTTP_HOST'];
// Get path to script
$myUrl .= $_SERVER['REQUEST_URI'];
// Add path info, if any
if (!empty($_SERVER['PATH_INFO'])) $myUrl .= $_SERVER['PATH_INFO'];
// Add query string, if any (some servers include a ?, some don't)
if (!empty($_SERVER['QUERY_STRING'])) $myUrl .= '?'.ltrim($_SERVER['REQUEST_URI'],'?');
echo $myUrl;
...is my most resilient routine for this.
...是我对此最有弹性的例程。
回答by Ian Jamieson
You might like to try:
您可能想尝试:
echo '<pre>';
print_r($_SERVER);
echo '</pre>';
A combination of $_SERVER['HTTP_HOST']
and $_SERVER['SCRIPT_NAME']
or $_SERVER['PHP_SELF']
should be what you are looking for.
的组合$_SERVER['HTTP_HOST']
和$_SERVER['SCRIPT_NAME']
或$_SERVER['PHP_SELF']
应该是你在找什么。
回答by Tak
You need to build it yourself;
您需要自己构建它;
echo $_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
You can also use $_SERVER['HTTPS']
to detect if HTTP or HTTPS.
您还可以使用$_SERVER['HTTPS']
来检测 HTTP 还是 HTTPS。
I recommend using $_SERVER['REQUEST_URI']
as this is exactly what the user is looking for, before any rewrites or anything else, and includes GET variables
我建议使用,$_SERVER['REQUEST_URI']
因为这正是用户正在寻找的,在任何重写或其他任何事情之前,并且包括 GET 变量
回答by virushuo
There is no full url contain in $_SERVER. But you can use this code:
$_SERVER 中不包含完整的 url。但是您可以使用以下代码:
$url = "http://" . $_SERVER['HTTP_HOST'] . "/" . $_SERVER['PHP_SELF'] . "/" . $_SERVER['QUERYSTRING'] . "/";