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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 04:35:50  来源:igfitidea点击:

What $_SERVER variable provide full URL

phpglobal-variables

回答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'] . "/";