2016-12-07 20 views
0

我嘗試編寫可以顯示錯誤消息的登錄過程。在HTML我的登錄表單是這樣的:對於登錄時出現錯誤消息(HTML和php)

<form id="login" action="data/login.php" method="post"> 
    <H2>Rgister</H2> 
    Your login and password: 
    <br><br> 
    <div id="login-name" style="margin-top:20px;">Login:</div><div id="login-fld" style="margin-top:20px;"><input name="username" class="form-login" title="Username" value="" size="30" maxlength="2048" /></div> 
    <div id="login-name">Pass:</div><div id="login-fld"><input name="password" type="password" class="form-login" title="Password" value="" size="30" maxlength="2048" /></div> 
    <br><br><br> 
    <input type="image" src="obr/login-klw.png" alt="Submit" width="103" height="42" style="margin-left:90px;"> 
    <br><br> 

    <?php if ($_GET["log_err"]) { if ($_GET["rsn"] == "pass") { ?> 
    <html><body><h3>Error no 1</h3></body></html> 
    <?php } else { ?> 
    <html><body><h3>Error no 2</h3></body></html> 
    <?php } exit; } ?> 
</form> 

和PHP代碼:

... [ question to the database and return in $result object ] ... 

     if ($result->num_rows == 0) { 
      setcookie('logged_in', false, time() + 600, '/'); 
      die(header("location:../index.html?log_err=true&powod=pass")); 
     } 
     else { 
      setcookie('logged_in', $_POST['username'], time() + 600, '/'); 
      header("Location: /AAA/app.html"); 
     } 
    } 
    else { 
     setcookie('logged_in', false, time() + 600, '/'); 
     die(header("location:../index.html?log_err=true&powod=empty")); 
} 

和兩個消息(「錯誤號1」和「錯誤號2」)我看,每次我加載 此頁。我不知道爲什麼以及如何調試這部分代碼。任何人都可以幫助我?

問題是,這種形式看起來是這樣的: enter image description here

我看到錯誤的按摩所有的時間。

+0

請粘貼整個代碼 – Blueblazer172

+0

爲什麼使用$ _GET? –

+1

你把' .....'裏面的'

'。 – Banzay

回答

0

解決:第一個文件名應爲 「的index.php」 不 「的index.html」,第二個代替:

<?php if ($_GET["log_err"]) { if ($_GET["rsn"] == "pass") { ?> 

應該是:

<?php if (isset($_GET['log_err'])) if ($_GET["log_err"]) { if ($_GET["rsn"] == "pass") { ?> 

在應用程序啓動有沒有$ _GET,所以它的存在應該先測試。 謝謝你的回覆。

+0

停止使用'$ _GET',改用SESSIONs。 – Martin

0

這是你會一遍又一遍地面對的問題。在開發模式下學習將變量轉儲到屏幕上,以便在服務器正在理解的內部進行對等。

以最簡單的形式,你所要做的就是在你的代碼下面。

<?php var_dump($_GET); ?>

那麼,也許,var_dump($_GET["log_err"]);等。

其他正確的答案不能承受,哦,檢查你沒有var_dump()語句在你發佈你的代碼之前徘徊。

祝你好運。

+0

謝謝你的信息 – Tad