2012-12-23 38 views
2

我試圖推出HTTPS上一個小網站以最小的變化,我在我想保護的頁面的頂部添加代碼...在PHP網站有效添加HTTPS

<?php 
$securepage ="1"; 

if ($_SERVER['HTTPS']=='on') { 
    // we are on a secure page. 
    if (!$securepage) { 
     // but we shouldn't be! 
     $url='http://www.mywebsite.com'.$_SERVER['REQUEST_URI']; 
     header('location: '.$url); 
     exit; 
    } 
    } else { 
    // we aren't on a secure page. 
    if ($securepage) { 
     // but we should be! 
     $url='https://www.mywebsite.com'.$_SERVER['REQUEST_URI']; 
     header('location: '.$url); 
     exit; 
    } 
    } 
?> 

的頁面我不希望我的保護刪除以下...

$securepage ="1"; 

但是當我嘗試加載頁面我得到一個錯誤說該網頁的重定向次數過多。

任何人有任何指針,什麼是錯與上面的代碼?

+0

嘗試http://tools.seobook.com/server-header-checker/檢查'頭()'s的工作 – Raekye

回答

1

我在開發一個應用程序,具有相同需求的過程。有些頁面需要通過SSL(https)進行查看,其他頁面則不需要。

對於需要通過SSL瀏覽過的網頁,我用

if ($_SERVER["SERVER_PORT"]==80){ header("Location: https://".$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF']); exit(); } 

對於那些不這樣做,即可以通過http觀看,使用

if ($_SERVER["SERVER_PORT"]!=80){ header("Location: http://".$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF']); exit(); } 

如果您的網頁內的主機上的目錄,那麼這可能也是幫助

"location:http://".$_SERVER['HTTP_HOST'].dirname($_SERVER['PHP_SELF'])"; 

希望這有助於...