tags:

views:

79

answers:

6

How can I tell if a php page was accessed via http or https?

+1  A: 

http://www.php.net/manual/en/reserved.variables.php#74040

You could also have Googled. This one even has the same title as your question - http://bytes.com/topic/php/answers/488284-php-http-https-how-can-one-tell

Phil Brown
A: 
$protocol = strtolower(substr($_SERVER["SERVER_PROTOCOL"],0,5))=='https'?'https':'http';

$protocol = isset($_SERVER["https"]) ? 'https' : 'http';

These should both work

Mark Baijens
+6  A: 

If the request was sent with HTTPS you will have a extra parameter in the $_SERVER superglobal - $_SERVER['HTTPS']. You can check if it is set or not

if( isset($_SERVER['HTTPS'] ) ) {
Eran Galperin
The problem with this approach is that it doesn't always work, as the `$_SERVER` documentation says: "The entries in this array are created by the web server. There is no guarantee that every web server will provide any of these; servers may omit some, or provide others not listed here."
Bruno
If the server is properly configured, is shouldn't be a problem. Better to try this approach first and resort to hacks if it fails
Eran Galperin
A: 
$_SERVER['HTTPS']

This will contain a 'non-empty' value if the request was sent through HTTPS

PHP Server Variables

Craige
A: 

You should be able to do this by checking the value of $_SERVER['HTTPS'] (it should only be set when using https).

See http://php.net/manual/en/reserved.variables.server.php.

wimvds
+1  A: 

This can get more complicated depending on where PHP sits in your environment, since your question is quite broad. This may depend on whether there's a load-balancer and how it's configured. Here are are a few related questions:

Bruno