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] ? "//" : "//";
$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 = '//';
$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!
BalusC
1.1m364 gold badges3571 silver badges3520 bronze badges
asked Dec 21, 2010 at 19:30
5
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 = '//';
}
else {
$protocol = '//';
}
answered Jan 11, 2013 at 1:17
Rid IculousRid Iculous
3,5762 gold badges21 silver badges28 bronze badges
3
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 ? '//' : '//';
...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"]
ashleedawg
19.4k7 gold badges69 silver badges100 bronze badges
answered Feb 26, 2013 at 23:21
IvoIvo
6,2701 gold badge24 silver badges43 bronze badges
12
It is not automatic. Your top function looks ok.
answered Dec 21, 2010 at 19:38
profitphpprofitphp
7,9482 gold badges26 silver badges21 bronze badges
3
Some changes:
function siteURL[] {
$protocol = [[!empty[$_SERVER['HTTPS']] && $_SERVER['HTTPS'] != 'off'] ||
$_SERVER['SERVER_PORT'] == 443] ? "//" : "//";
$domainName = $_SERVER['HTTP_HOST'];
return $protocol.$domainName;
}
Dave Jarvis
29.9k38 gold badges176 silver badges306 bronze badges
answered Sep 11, 2012 at 6:42
Anoop KAnoop K
2813 silver badges3 bronze badges
short way
$scheme = $_SERVER['REQUEST_SCHEME'] . '://';
answered Feb 20, 2015 at 17:37
softcod.comsoftcod.com
4865 silver badges8 bronze badges
2
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 HTTPS
constant returns TRUE
if $_SERVER['HTTPS']
is set and equals to "1", "true", "on" or "yes". Returns FALSE otherwise.
answered Oct 1, 2014 at 15:43
alexalex
5,5416 gold badges32 silver badges54 bronze badges
2
For any system except IIS this is quite enough to define site self 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
answered Sep 15, 2016 at 18:17
In case of proxy the SERVER_PORT
may not give the correct value so this is what worked for me -
$protocol = [[!empty[$_SERVER['HTTPS']] && $_SERVER['HTTPS'] != 'off'] || $_SERVER['SERVER_PORT'] == 443 || $_SERVER['HTTP_X_FORWARDED_PORT'] == 443] ? "//" : "//"
answered Aug 26, 2016 at 7:00
HarshitHarshit
7111 gold badge9 silver badges29 bronze badges
0
Use this server variable to get the protocol details:
$scheme = $_SERVER['REQUEST_SCHEME'] . '://';
echo $scheme; //it gives // or //
Note that this server variable is unreliable. For more information take a look at: Is $_SERVER['REQUEST_SCHEME'] reliable?
answered Jul 8, 2015 at 9:11
shashik493shashik493
7701 gold badge9 silver badges12 bronze badges
1
Extracted from CodeIgniter :
if [ ! function_exists['is_https']]
{
/**
* Is HTTPS?
*
* Determines if the application is accessed via an encrypted
* [HTTPS] connection.
*
* @return bool
*/
function is_https[]
{
if [ ! empty[$_SERVER['HTTPS']] && strtolower[$_SERVER['HTTPS']] !== 'off']
{
return TRUE;
}
elseif [isset[$_SERVER['HTTP_X_FORWARDED_PROTO']] && strtolower[$_SERVER['HTTP_X_FORWARDED_PROTO']] === 'https']
{
return TRUE;
}
elseif [ ! empty[$_SERVER['HTTP_FRONT_END_HTTPS']] && strtolower[$_SERVER['HTTP_FRONT_END_HTTPS']] !== 'off']
{
return TRUE;
}
return FALSE;
}
}
answered Jul 3, 2018 at 9:40
GuillaumeGuillaume
4334 silver badges11 bronze badges
$protocal = 'http';
if [$_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https' || $_SERVER['HTTPS'] == 'on'] {$protocal = 'https';}
echo $protocal;
answered Jun 30, 2016 at 17:39
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'];
echo '$SSL_test: '.$SSL_test.'
';
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. :]
answered Apr 2, 2018 at 23:21
Rob StockiRob Stocki
1461 silver badge9 bronze badges
1
made a function using the Rid Iculous's answer which worked on my system.
function site_protocol[] {
if[isset[$_SERVER['HTTPS']] && [$_SERVER['HTTPS'] == 'on' || $_SERVER['HTTPS'] == 1] || isset[$_SERVER['HTTP_X_FORWARDED_PROTO']] && $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https'] return $protocol = '//'; else return $protocol = '//';
}
Hope it helps
answered May 10, 2014 at 6:28
miyurumiyuru
1,09111 silver badges19 bronze badges
I've tested the most voted answer and it didn't work for me, I ended up using:
$protocol = isset[$_SERVER['HTTPS']] ? '//' : '//';
answered May 13, 2016 at 15:10
Pedro LobitoPedro Lobito
88.6k29 gold badges238 silver badges256 bronze badges
4
Here is how I do it ... it is a shorthand if else
version of Rid Iculous's answer ...
$protocol = isset[$_SERVER['HTTPS']] && [$_SERVER['HTTPS'] === 'on' || $_SERVER['HTTPS'] === 1] || isset[$_SERVER['HTTP_X_FORWARDED_PROTO']] && $_SERVER['HTTP_X_FORWARDED_PROTO'] === 'https' ? 'https' : 'http';
answered Nov 14, 2017 at 17:54
Scotty GScotty G
3682 silver badges6 bronze badges
I think the complete func should look like :
function siteURL[]
{
$protocol = "//";
if [
//straight
isset[$_SERVER['HTTPS']] && in_array[$_SERVER['HTTPS'], ['on', 1]]
||
//proxy forwarding
isset[$_SERVER['HTTP_X_FORWARDED_PROTO']] && $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https'
] {
$protocol = '//';
}
$domainName = $_SERVER['HTTP_HOST'];
return $protocol . $domainName;
}
Notes:
- you should look also for HTTP_X_FORWARDED_PROTO [e.g. if proxy server]
- relying on 443 port is not safe [https could be served on different port]
- REQUEST_SCHEME not reliable
answered Feb 19, 2020 at 14:24
koalaokkoalaok
4,6909 gold badges41 silver badges81 bronze badges
it's the best solution of https or http use this :
But can't display https or http, so it only use to link your site content like image, etc.
if want to redirect your site in https, add this code in .htaccess file :
RewriteCond %{CF-Visitor} '"scheme":"http"'
RewriteRule ^[.*]$ //www.your-domain.com$1 [L]
Change www.your-domain.com with your dowmain name.
answered May 18, 2017 at 16:15
I know I'm a bit late to this party, but if you much prefer not using $_SERVER as it's strongly discouraged, and even deactivated on some PHP frameworks; and you have an apache web server, you can use it's native command thusly: -
$protocol = apache_getenv['HTTPS'] ? '' : '';
answered Mar 5, 2020 at 1:24
JonathanJonathan
7572 gold badges10 silver badges17 bronze badges
Also late to the party, but here is a much shorter version of Rid Iculous's answer using the Null Coalescing Operator:
$is_ssl = in_array[$_SERVER['HTTPS'] ?? '', ['on', 1]] ||
[$_SERVER['HTTP_X_FORWARDED_PROTO'] ?? ''] == 'https';
$protocol = $is_ssl ? '//' : '//';
Or:
$protocol = in_array[$_SERVER['HTTPS'] ?? '', ['on', 1]] ||
[$_SERVER['HTTP_X_FORWARDED_PROTO'] ?? ''] == 'https' ?
'//' : '//';
answered Jun 2 at 0:41
AranxoAranxo
5622 silver badges14 bronze badges