網站的某些頁面,我們需要強制使用者使用https加密連線,例如登入的頁面,來保護使用者輸入的資料。這時可利用以下兩種檢查加上重導來達到:

  • 檢查$_SERVER["HTTPS"]
  • <?php
    if(empty($_SERVER["HTTPS"])) {
      $https_login = "https://" . $_SERVER["SERVER_NAME"] . '/login' ;
      header("Location: $https_login");
      exit();
    }
    ?>
  • 檢查$_SERVER["SERVER_PORT"]
  • <?php
    if($_SERVER["SERVER_PORT"] != '443' ) {
      $https_login = "https://" . $_SERVER["SERVER_NAME"] . '/login' ;
      header("Location: $https_login");
      exit();
    }
    ?>