duanjunjie0196 2012-03-24 12:49
浏览 5
已采纳

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(); }

  • 写回答

2条回答 默认 最新

  • doushua7737 2012-03-24 12:53
    关注

    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://...
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?