PHP: HTTP or HTTPS? [duplicate]
How can I tell if a php page was accessed via http or https?
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'] ) ) {
If your request is sent by HTTPS you will have an extra server variable named 'HTTPS'
if (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off') { //HTTPS }
$protocol = strtolower(substr($_SERVER["SERVER_PROTOCOL"],0,5))=='https'?'https':'http';
$protocol = isset($_SERVER["HTTPS"]) ? 'https' : 'http';
These should both work
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:
- How can I prevent access to PHP files if the caller isn't using HTTPS?
- Detecting HTTPS vs HTTP on server sending back nothing useful
$_SERVER['HTTPS']
This will contain a 'non-empty' value if the request was sent through HTTPS
PHP Server Variables
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.
精彩评论