PHP 获取站点 URL 协议 - http 与 https

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4503135/
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-25 13:13:38  来源:igfitidea点击:

PHP Get Site URL Protocol - http vs https

phpurlssl

提问by

I've written a little function to establish the current site url protocol but I don't have SSL and don't know how to test if it works under https. Can you tell me if this is correct?

我写了一个小函数来建立当前站点的 url 协议,但我没有 SSL,也不知道如何测试它是否在 https 下工作。你能告诉我这是否正确吗?

function siteURL()
{
    $protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' || $_SERVER['SERVER_PORT'] == 443) ? "https://" : "http://";
    $domainName = $_SERVER['HTTP_HOST'].'/';
    return $protocol.$domainName;
}
define( 'SITE_URL', siteURL() );

Is it necessary to do it like above or can I just do it like?:

是否有必要像上面那样做,或者我可以像这样做吗?:

function siteURL()
{
    $protocol = 'http://';
    $domainName = $_SERVER['HTTP_HOST'].'/'
    return $protocol.$domainName;
}
define( 'SITE_URL', siteURL() );

Under SSL, doesn't the server automatically convert the url to https even if the anchor tag url is using http? Is it necessary to check for the protocol?

在 SSL 下,即使锚标记 url 使用的是 http,服务器是否也不会自动将 url 转换为 https?是否有必要检查协议?

Thank you!

谢谢!

采纳答案by profitphp

It is not automatic. Your top function looks ok.

它不是自动的。您的顶级功能看起来不错。

回答by Ivo

I know it's late, although there is a much more convenient way to solve this kind of problem! The other solutions are quite messy; this is how I would do it:

我知道已经晚了,尽管有更方便的方法来解决此类问题!其他解决方案相当混乱;这就是我将如何做到的:

$protocol = stripos($_SERVER['SERVER_PROTOCOL'],'https') === 0 ? 'https://' : 'http://';

...or even without condition if you prefer:

...或者甚至没有条件,如果您愿意:

$protocol = strtolower(substr($_SERVER["SERVER_PROTOCOL"],0,strpos( $_SERVER["SERVER_PROTOCOL"],'/'))).'://';

Have a look at $_SERVER["SERVER_PROTOCOL"]

看一下 $_SERVER["SERVER_PROTOCOL"]

回答by Rid Iculous

This works for me

这对我有用

if (isset($_SERVER['HTTPS']) &&
    ($_SERVER['HTTPS'] == 'on' || $_SERVER['HTTPS'] == 1) ||
    isset($_SERVER['HTTP_X_FORWARDED_PROTO']) &&
    $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https') {
  $protocol = 'https://';
}
else {
  $protocol = 'http://';
}

回答by Anoop K

Some changes:

一些变化:

function siteURL() {
  $protocol = ((!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off') || 
    $_SERVER['SERVER_PORT'] == 443) ? "https://" : "http://";
  $domainName = $_SERVER['HTTP_HOST'];
  return $protocol.$domainName;
}

回答by softcod.com

short way

捷径

$scheme = $_SERVER['REQUEST_SCHEME'] . '://';

回答by alex

Because testing port number is not a good practice according to me, my solution is:

因为在我看来测试端口号不是一个好习惯,我的解决方案是:

define('HTTPS', isset($_SERVER['HTTPS']) && filter_var($_SERVER['HTTPS'], FILTER_VALIDATE_BOOLEAN));

The HTTPSconstant returns TRUEif $_SERVER['HTTPS']is set and equals to "1", "true", "on" or "yes". Returns FALSE otherwise.

HTTPS报酬不变TRUE,如果$_SERVER['HTTPS']设置和“on”或“是”等于“1”,“真实的”。否则返回 FALSE。

回答by Eugene Zakharenko

For any system except IIS this is quite enough to define site self URL:

对于除 IIS 之外的任何系统,这足以定义站点自 URL:

$siteURL='http'.(empty($_SERVER['HTTPS'])?'':'s').'://'.$_SERVER['HTTP_HOST'].'/';

or

或者

$siteURL='http'.(empty($_SERVER['HTTPS'])?'':'s').'://'.$_SERVER['SERVER_NAME'].'/';

depends on what you exactly want: HTTP_HOST vs. SERVER_NAME

取决于你到底想要什么: HTTP_HOST 与 SERVER_NAME

回答by Harshit

In case of proxy the SERVER_PORTmay not give the correct value so this is what worked for me -

在代理的情况下,SERVER_PORT可能不会给出正确的值,所以这对我有用 -

$protocol = ((!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off') || $_SERVER['SERVER_PORT'] == 443 || $_SERVER['HTTP_X_FORWARDED_PORT'] == 443) ? "https://" : "http://"

回答by shashik493

Use this server variable to get the protocol details:

使用此服务器变量获取协议详细信息:

 $scheme = $_SERVER['REQUEST_SCHEME'] . '://';
 echo $scheme; //it gives http:// or https://

Note that this server variable is unreliable. For more information take a look at: Is $_SERVER['REQUEST_SCHEME'] reliable?

请注意,此服务器变量不可靠。有关更多信息,请查看: $_SERVER['REQUEST_SCHEME'] 可靠吗?

回答by Rob Stocki

I know this is an old question but I came across this today since I needed to test for this in my site. It seems the answers above are needlessly complicated. To establish the site protocol, all you have to do is test $_SERVER['HTTPS']

我知道这是一个老问题,但我今天遇到了这个问题,因为我需要在我的网站中对此进行测试。似乎上面的答案是不必要的复杂。要建立站点协议,您所要做的就是测试$_SERVER['HTTPS']

If the protocol is using HTTPS, then $_SERVER['HTTPS']will return 'on'. If not, the variable will remain empty. For example:
// test if HTTPS is being used. If it is, the echo will return '$SSL_test: on'. If not HTTPS, '$SSL_test' will remain empty.

如果协议使用 HTTPS,$_SERVER['HTTPS']则将返回“on”。如果不是,变量将保持为空。例如:
// test if HTTPS is being used. If it is, the echo will return '$SSL_test: on'. If not HTTPS, '$SSL_test' will remain empty.

$SSL_test = $_SERVER['HTTPS'];

echo '<p>$SSL_test: '.$SSL_test.'</p>';

if($SSL_test == true) {
    echo 'You\'re using SSL';
} else {
    echo 'You\'re not using SSL';
} 

$SSL_test = $_SERVER['HTTPS'];

echo '<p>$SSL_test: '.$SSL_test.'</p>';

if($SSL_test == true) {
    echo 'You\'re using SSL';
} else {
    echo 'You\'re not using SSL';
} 

You can use the above to easily and cleanly test for HTTPS and implement accordingly. :)

您可以使用上述内容轻松干净地测试 HTTPS 并相应地实施。:)