gpt4 book ai didi

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

转载 作者:IT老高 更新时间:2023-10-28 11:41:00 25 4
gpt4 key购买 nike

我编写了一个小函数来建立当前站点的 url 协议(protocol),但我没有 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() );

有必要像上面那样做还是我可以像上面那样做?:

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

在SSL下,即使 anchor 标签url使用http,服务器不会自动将url转换为https吗?是否需要检查协议(protocol)?

谢谢!

最佳答案

这对我有用

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://';
}

关于PHP 获取站点 URL 协议(protocol) - http 与 https,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4503135/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com