Php https请求页面

Php https请求页面

问题描述:

I was trying to use this code:

if($_SERVER['HTTP_PORT'] !== 443 && !isset($_SERVER['HTTPS'])) {
  header('Location: https://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']);
  exit;
}

to make my login page and register page contain https to use the ssl to make sure that everything is secure, but when I run the page an error message shows up Undefined index error using $_SERVER['HTTPS']

so I decided to use another one:

if ( isset( $_SERVER["HTTPS"] ) && strtolower( $_SERVER["HTTPS"] ) == "on" ) {
  $pageURL .= "s";
}

but it didn't work. The https didn't show up and http is only there...

any idea how to do that with php...

Thanks

The solution is:

if($_SERVER["HTTPS"] != "on")

{ header("Location: https://" . $_SERVER["HTTP_HOST"] . $_SERVER["REQUEST_URI"]); exit(); }

From the manual:

$_SERVER['HTTPS']
  Set to a non-empty value if the script was queried through the HTTPS protocol.

Therefore, you should be using the following condition to check for an HTTPS connection:

if(!empty($_SERVER['HTTPS']))
{
    // https://...
}

You'd better use $_SERVER['SERVER_PORT']. HTTP_PORT doesnt always return the portnumber.