2016-04-05 37 views
-2

, alt kısmında '0' hatası veriyor. Doğru ismi ve şifreyi koyduğumda, beni doğru sayfaya yönlendirir. Yanlış kullanıcı ve şifre koyduğumda, doğru mesaj belirir. Neden ilk '0' var?PHP şifre formu

<!DOCTYPE html> 
<html lang = "en"> 
    <head> 
     <meta charset ="utf-8"> 
      <title>Login page</title> 
    </head> 
    <body> 
    <h1>Log in to our Website here:</h1> 

    <?php 
    $self = htmlentities($_SERVER['PHP_SELF']); 
    $creds = array(
       array('username' => 'Bob', 
         'password' => 'bill'), 
       array('username' => 'mary', 
         'password' => 'jane') 
       ); 
    $match = false; 
    $errors = $username = $password = 0; 
    if (isset($_POST['username']) && isset($_POST['password'])) { 
     $details_entered = true; 
     $username = trim($_POST['username']); 
     $password = trim($_POST['password']); 
      for ($i=0; $i<count($creds); $i++) { 
       if(strcmp($username, $creds[$i]['username']) ==0) { 
        if(strcmp($password, $creds[$i]['password']) ==0) { 
       session_start(); 
       $_SESSION['authenticated'] = true; 
       $_SESSION['username'] = $creds[$i]['username']; 
       $match = true; 
      } 
     } else { 
      $errors = "***Either your username or password are not correct***"; 
     } 
    } 
} 

if($match) { 
    header ('location: login.php'); 
} 
?> 

     <form id = "login" action ="<?php echo $self; ?>" method ="post"> 
     <fieldset> 

     <p> 
     <label for ="username">*UserName:</label> 
     <input type ="text" name ="username" id ="username" maxlength ="50" placeholder ="Username"/> 
     </p> 

     <p> 
     <label for = "password">*Password:</label> 
     <input type ="password" name ="password" id="password" maxlength ="50" placeholder ="Password"/> 
     </p><span><?php echo $errors; ?></span> 

     <p> 
     <input type ="submit" name ="Login" value ="Login"/> 
     </p> 

     <p> 
     <a href = "register.html">Or register with us here:</a> 
     </p> 

     </fieldset> 
     </form> 
    </body> 
</html> 
+2

Parola güvenliğini işlemek için lütfen PHP'nin [built-in işlevleri] (http://jayblanchard.net/proper_password_hashing_with_PHP.html) kullanın. PHP sürümü 5.5'ten küçükse, 'password_hash()' [uyumluluk paketi] 'ni (https://github.com/ircmaxell/password_compat) kullanabilirsiniz. –

+4

'Neden bu bir başlangıç ​​'0'?' Çünkü '$ errors = $ username = $ password = 0;' –

+0

@Lashane'ın işaret ettiği gibi, ilk sayfa yüklemesinde form gönderimini kontrol etmiyorsunuz. beklemediğiniz bir çıktı alırsınız. –

cevap

0

$errors = $username = $password = 0; - $errors = $username = $password = ""; değiştirmeyi deneyin.

+0

Sorun ne * gerçekten * sorunu form teslim kontrol ediyor. Ve başka şeyler. –

+0

@ jay-blanchard, bu kodun birçok sorunu var ama bu, bu sorunun bir teması değil. –

+0

Eğer bu cevap düzelttiyse, lütfen cevabı iyice belirtin. İngilizcem için üzgünüm – Naumov

0

$ Hata değişkeninizi '0' başlangıç ​​değeriyle bildirdiniz. Bu gerekli değil ve sorunun sebebi de bu.