PHP获取站点URL协议-http与https [英] PHP Get Site URL Protocol - http vs https

查看:421
本文介绍了PHP获取站点URL协议-http与https的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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?

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下,即使定位标记url使用的是http,服务器也不会自动将url转换为https吗?是否有必要检查协议?

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?

谢谢!

推荐答案

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

It is not automatic. Your top function looks ok.

这篇关于PHP获取站点URL协议-http与https的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆