2017-02-18 90 views
2

我在用PHP代碼查找密碼保護我的網站(index.html)上的特定頁面。只保護一頁的密碼

這就是我現在所擁有的(此代碼是從另一個問題就在這裏C/P):

<?php 
session_start(); 
$username="admin"; 
$password="jATPqhu9"; 

if(isset($_POST['username']) && $_POST['username'] == $username && $_POST['password'] == $password) 
{ 
    header("Location: ecks.html"); 
    exit; 
} 
else 
{ 
?> 

<html> 
    <head> 
    </head> 
    <body> 
     <div style='text-align:center'> 
     <h3>Welcome To ---</h3> 
     </div> 
     <hr /><br /> 
     <form id='login' action="" method='post' accept-charset='UTF-8'> 
      <fieldset style="width:550px"> 
      <legend>Admin Login</legend> 
      <input type='hidden' name='submitted' id='submitted' value='1'/> 

      <label for='username' >UserName:</label> 
      <input type='text' name='username' id='username' maxlength="50" /> 


      <label for='password' >Password:</label> 
      <input type='password' name='password' id='password' maxlength="50" /> 

      <input type='submit' name='submit' value='Submit' /> 
      </fieldset> 
     </form> 
    </body> 
</html> 
<?php 
} 
?> 

出於某種原因,重定向的部分(標題( 「位置:ecks.html」); )不會重定向到ecks.html。

+0

是的。除了重定向之外,一切都有效。 –

+0

檢查你的http服務器錯誤日誌文件,很可能是在調用head函數的輸出_before_中。 – arkascha

+0

另外看看你的瀏覽器控制檯網絡選項卡應該顯示是否根本發送了這個頭文件。 – arkascha

回答

0
<?php 
session_start(); 
$username="admin"; 
$password="jATPqhu9"; 

if(isset($_POST['username']) && $_POST['username'] == $username && $_POST['password'] == $password) 
{ 

$_SESSION['username'] = $username; 
header("Location: ecks.html"); 
exit; 
} 
else 
{ 
    echo "incorrect info."; 
} 
?> 

Put this in the ecks.html page 
<?php 
session_start(); 

if (!isset($_SESSION['username'])) { 
    //login html 
} 
?> 

希望這個作品

+0

我已經更新了所有內容,但是它仍然沒有做任何事情!此外,ecks.html部分只是爲了確保人們在查看ecks.html時登錄,否則他們必須登錄? –

1

我不認爲有任何使用HTML或這是最好的保護它免受服務器端的其他一些網頁上設定的用戶名和密碼。 這樣做,在服務器中,當您嘗試訪問該頁面時,直接訪問Leach Protection或目錄保護或熱鏈接併爲其設置密碼,當您在Web上訪問您的文件時,您將需要用戶名和密碼。

+0

是的,我知道這根本不安全,但我需要在確保它的安全性之前先設置它。 –