2012-04-27 69 views
0

我剛開始學習PHP,並且在簡單的登錄頁面工作時遇到了一些麻煩。當我第一次加載這個腳本時,文本「錯誤的密碼/用戶名」。並且註銷按鈕被打印,但不是登錄表單。爲什麼會發生這種情況,以及如何更改代碼以使登錄表單和註銷按鈕按預期一起工作?PHP登錄表單和註銷按鈕不能正常顯示

<?php 

if (isset($_POST['log_out'])) { // If the page was reloaded as a result of the user having clicked the logout button, 
session_destroy(); // kill session. 
} 

session_start(); // Start a new session with 

$_SESSION['user'] = 'foo'; // a static username, and 
$_SESSION['pass'] = 'bar'; // a static password. 
// If I insert $_SESSION['logged_in'] = 'false'; here to start things off, a blank alert box will be returned no matter from where on the page I alert() the value with JS. On the other hand, without this line the same alert will return "1" both here and further down in the script. 

if (($_POST['username'] == $_SESSION['user']) && ($_POST['password'] == $_SESSION['pass'])) { // If the username and password filled in before the page reload match the static ones, 
    $_SESSION['logged_in'] = true; // the user is logged in. 
} else { // If there is no match ... 
    echo 'Wrong password/username.'; 
} 

include("head.php"); // HTML snippet with everything from the DOCTYPE to the opening BODY tag. 

?> 
      <div> // HTML interlude ... 
<?php 

// If the user is logged in, print out a logout button in HTML at the top of the page: 

if (isset($_SESSION['logged_in']) && ($_SESSION['logged_in'] == true)) { 
    echo '   <form action="index.php" method="post">'; 
    echo '   <input type="submit" name="log_out" value="Log out">'; 
    echo '   </form>'; 
} 

?> 
       <p>HTML interlude ...</p> 

<?php 

// If the user is not logged in, print out a login form in HTML at the bottom of the page: 

if ($_SESSION['logged_in'] != true) { 

    echo '   <form action="index.php" method="post">'; 
    echo '    <label for="username">Username</label><br>'; 
    echo '    <input type="text" name="username" id="username"><br>'; 
    echo '    <label for="password">Password</label><br>'; 
    echo '    <input type="text" name="password" id="password"><br>'; 
    echo '    <input type="submit" name="submit" id="submit" value="Log in">'; 
    echo '   </form>'; 

} 

?> 
      </div> 
<?php include("footer.php"); ?> 
+1

您能添加輸出的快照嗎? – 2012-04-27 07:58:47

回答

1
if (($_POST['username'] == $_SESSION['user']) && ($_POST['password'] == $_SESSION['pass'])) { the static ones, 
    $_SESSION['logged_in'] = true; // the user is logged in. 
} else { 
    echo 'Wrong password/username.'; 
} 

頁面加載後的第一次,因爲如果沒有來自$_POST['username']病情會是假的這部分將顯示「密碼錯誤/用戶名」。

將條件isset($_POST['username']) && isset($_POST['password'])添加到兩個選項中。像這樣:

if(isset($_POST['username']) && isset($_POST['password'])){ 
    if (($_POST['username'] == $_SESSION['user']) && ($_POST['password'] == $_SESSION['pass'])) { the static ones, 
     $_SESSION['logged_in'] = true; // the user is logged in. 
    } else { 
     echo 'Wrong password/username.'; 
    } 
} 

這種方式不會在第一次加載頁面時評估,但只有在發佈憑證時纔會對此進行評估。

+0

另外,我不會建議在$ _SESSION中存儲正確的用戶名和密碼。例如,最好從用戶數據庫中獲取。 – Gustav 2012-04-27 08:08:32

+0

非常感謝,@Gustav。一旦某人爲你指出這些事情,有些事情是非常明顯的。現在只要cookie被清除,我就可以按照預期在頁面加載登錄表單,並且不會輸入「錯誤的密碼/用戶名」。文本。然而,一旦我登錄後,我無法註銷(註銷按鈕在那裏,但點擊它什麼也不做)會話,甚至沒有頁面重新加載後。 (我應該爲此創建另一個問題嗎?) – Johanna 2012-04-27 08:47:01

+0

靜態用戶名和密碼僅用於測試目的;一旦我登錄功能正常工作,我會嘗試使用數據庫。 – Johanna 2012-04-27 08:50:31

0

您可以通過調試腳本發現:商店的預期爲變量,並打印出來,例如:

$isLoggedIn = $_SESSION['logged_in']; # Give things a name! (always useful) 
echo 'isLoggedIn: ', var_dump($isLoggedIn), "\n"; # debugging 
if ($isLoggedIn != true) { 

    echo '   <form action="index.php" method="post">'; 
    echo '    <label for="username">Username</label><br>'; 
    echo '    <input type="text" name="username" id="username"><br>'; 
    echo '    <label for="password">Password</label><br>'; 
    echo '    <input type="text" name="password" id="password"><br>'; 
    echo '    <input type="submit" name="submit" id="submit" value="Log in">'; 
    echo '   </form>'; 

} 
+0

感謝您的提示!我用JS提醒做了一些「醜陋」的調試 - 即使我使用調試器,我仍然處於這樣的階段,我經常不明白它要告訴我什麼,但是實踐是唯一能夠通過的方法那。 – Johanna 2012-04-27 08:59:40

+0

JS調試與PHP調試不同。對於PHP,您默認沒有控制檯。 – hakre 2012-04-27 11:02:32

0

第一次加載你的頁面,然後除非你傳遞所需的後變量,您的條件檢查

if (($_POST['username'] == $_SESSION['user']) && ($_POST['password'] == $_SESSION['pass'])) 

將失敗,因此顯示Wrong password/username.消息。

+0

感謝您指出!現在很明顯,我知道它。 :) – Johanna 2012-04-27 09:18:32

0

代碼看起來不錯,實際上對於「剛剛學習」的人來說做得很好。

鑑於代碼沒有明顯錯誤(幾個粗糙的邊緣 - 比較布爾值時應該考慮使用'==='而不是'=='),那麼也許這就是會話問題。

嘗試...

print_r($ _ SESSION);

after session_start();並在選擇是否顯示註銷按鈕的條件之前。

每當您不要將參數發佈到頁面以及發佈錯誤值時,都會顯示錯誤的用戶名消息。試試這個:

if (($_POST['username']) 
    && ($_POST['username'] == $_SESSION['user']) 
    && ($_POST['password'] == $_SESSION['pass'])) { 

順便說一下,這個會話不是存儲用戶名和密碼的好儲存庫 - 它是特定於當前用戶的。對於多用戶系統,這些將存儲在單獨的數據庫中,例如,

if (($_POST['username']) && lookup($_POST['username'], $_POST['password'])) { 
... 
+0

謝謝@symcbean鼓勵人物的話和關於print_r()的小技巧 - 我會把這個好用的!現在,在測試完參數是否發佈之後,在第一頁加載時,沒有錯誤消息和沒有註銷按鈕,只是登錄窗體像預期的那樣,但是一旦登錄,我無法註銷 - 註銷按鈕在那裏,但是沒有做任何事情。 (就像我上面提到的古斯塔夫一樣,也許正確的方法是爲此創建一個新問題?) – Johanna 2012-04-27 09:15:59