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!
made a function using the Rid Iculous's answer which worked on my system.
Hope it helps
I've tested the most voted answer and it didn't work for me, I ended up using:
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:
...or even without condition if you prefer:
Have a look at
$_SERVER["SERVER_PROTOCOL"]
Extracted from CodeIgniter :
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']
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.
$SSL_test = $_SERVER['HTTPS'];
You can use the above to easily and cleanly test for HTTPS and implement accordingly. :)