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() );
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?
Thank you!
Because testing port number is not a good practice according to me, my solution is:
The
HTTPS
constant returnsTRUE
if$_SERVER['HTTPS']
is set and equals to "1", "true", "on" or "yes". Returns FALSE otherwise.In case of proxy the
SERVER_PORT
may not give the correct value so this is what worked for me -It is not automatic. Your top function looks ok.
For any system except IIS this is quite enough to define site self URL:
or
depends on what you exactly want: HTTP_HOST vs. SERVER_NAME