I want to detect whether or not a user is viewing a secure page and redirect if not (for logging in).
However, my site travels through a proxy before I see the server variables and the proxy (right now) is telling me that $_SERVER['HTTPS']
is 'on'
when the URI clearly indicates otherwise. It also shows 'on'
when the user is navigating 'securely'.
Navigating through http://
and https://
both output that $_SERVER['SERVER_PORT']
= 443
.
I don't have the ability to make any changes to the proxy so I want to know:
- Does PHP have any other options for me to detect the truth or...
- Am I stuck to resort to JavaScript's mechanisms for detection and redirection.
I mined this question for ideas but they mostly revolve around the $_SERVER['HTTPS']
variable being trustworthy. Bah!
It appears that this question is experiencing at least something similar, but s/he was able to resolve it by adapting an apache solution.
Are there any other PHP SERVER variables or tricks available to detect what the user's URI begins with? The only difference between the $_SERVER variables when my site is viewed http versus https are the following:
- _FCGI_X_PIPE_ (appears random)
- HTTP_COOKIE (sto-id-47873 is included in the non-secure version but I did not put it there)
- REMOTE_ADDR (This and the next two keep changing inexplicably!)
- REMOTE_HOST
- REMOTE_PORT ('proxy people', why are you continually changing this?)
Are any of these items strong enough to put one's weight upon without it splintering and causing pain later? Perhaps I shouldn't trust anything as filtered through the proxy since it could change at any given time.
Here is my plan to use JavaScript for this purpose; is it the best I have?
function confirmSSL() {
if(location.protocol != "https:") {
var locale = location.href;
locale = locale.replace(/http:\/\//,"https://");
location.replace(locale);
}
}
<body onLoad="confirmSSL()">...
I think if the user has JavaScript disabled in my community, then they hopefully know what they are doing. They should be able to manually get themselves into a secure zone. What sort of <noscript>
suggestions would be commonplace / good practice? Something like this, perhaps?:
<noscript>
Navigate using https://blah.more.egg/fake to protect your information.</noscript>
PHP solutions that work (with good explanation) will be given preference for the correct answer. Feel free to submit a better JavaScript implementation or link to one.
Many thanks!