2017-09-02 32 views
0

我從週四晚上開始工作,但我無法弄清楚我做錯了什麼。我只是想創建一個簡單的登錄表單。我有用戶和管理員。當我試圖以用戶身份登錄時,它說用戶的用戶名和密碼無效,即使我的密碼和用戶名是正確的。爲什麼這個登錄表單代碼不工作?

<?php 

include 'Fonctions/fonctions.php'; 
teteHtml("Login"); 
enTete($messageErreur); 

//store the values found in SESSION 
$username = ""; 
$password = ""; 
$loginError = ""; 
if (isset($_POST["login"])) { 
    createCookie(); 
    echo $loginError; 
} else { 
    if (isset($_POST["deconnexion"])) { 
     deleteCookie(); 
    } 
} 

function createCookie() { 
    //if (isset($_POST["uname"], $_POST["psw"])) { 
    if (isset($_POST["login"])) { 
     //check if the system is lock 
     if (isset($_SESSION["login_error"]) && $_SESSION["login_error"] >= 3) { 
      die("Plusieurs essaies sont interdits!"); 
     } else { 


      $connection = getDatabaseConnection(); 


      $salted = "wrntjkhn4wervfmm" . $_POST["password"] . "wo2i45djk"; 
      $hashed = hash('sha512', $salted); 


      $stmt = $connection->prepare("CALL login(?,?)"); 
      $stmt->bindParam(1, $_POST["username"]); 
      $stmt->bindParam(2, $hashed); 
      //echo json_encode($stmt->errorInfo()); 
      // call the stored procedure 
      $stmt->execute(); 


      if ($row = $stmt->fetch()) { 
       $_SESSION["username"] = $row["username"]; 
      } else if ($_POST["username"] == "admin" && $_POST["password"] == "admin") { 
       $_SESSION["username"] = "admin"; 
      } else { 
       if (isset($_SESSION["login_error"])) { 
        echo $_SESSION["login_error"] . "jjjj"; 
        $_SESSION["login_error"] ++; 
       } else { 
        $_SESSION["login_error"] = 1; 
       } 
       if ($_SESSION["login_error"] >= 3) { 
        echo "you put 3 times wrong password."; 
       } 
       //echo gettype($_SESSION["login_error"]). ($_SESSION["login_error"] >= 3); 
       die("password and username are invalid"); 
      } 
     } 
    } 
} 

function deleteCookie() { 
    //$_SESSION["uname"] = ""; 
    session_destroy(); 

    //refresh the page 
    //header("Location: Mon_compte.php"); 
} 

include 'html/login.html'; 

?> 
<form method="POST" action="login.php"> 

     <label><b>Username</b></label> 
     <input type="text" placeholder="Enter Username" name="username" required> 

     <label><b>Password</b></label> 
     <input type="password" placeholder="Enter Password" name="password" required> 

     <button type="submit" name="login">Login</button> 
     <!--<input type="checkbox" checked="checked"> Remember me--> 
     <span id="error_connection"></span> 

     <span class="psw">Forgot <a href="#">password?</a></span> 

    </form> 
BEGIN 
select username, password from users 
    where username = p_username and password = p_password; 
END 
+2

問題尋求幫助調試( 「爲什麼不是這個代碼的工作?」)必須包括所期望的行爲,具體問題或錯誤和最短的代碼是必要的...你需要一步一步調試var_dump()和exit()然後你會發現問題.. –

+0

你還沒有添加任何「表單動作」,這是主要的您正面臨的問題 –

+0

'

'你應該讓你的表單代碼類似於這個。 –

回答

-2

MySQL數據庫/表

數據庫名稱 - Login

表名稱 - Users

表列 -

id(primary_key)|用戶名|密碼

DBConnection.php

<!-- Fill in the data below with your appropriate data --> 

<?php 
    define('server', 'localhost:3036'); 
    define('username', 'root'); 
    define('password', 'root_password'); 
    define('database_name', 'login'); 
    $db = mysqli_connect(server, username, password, database_name); 
?> 

的login.php

<?php 
    include("DBConnection.php"); 
    session_start(); 

    if($_SERVER["REQUEST_METHOD"] == "POST") {   
     $myusername = mysqli_real_escape_string($db,$_POST['username']); 
     $mypassword = mysqli_real_escape_string($db,$_POST['password']); 
     $sql = "SELECT id FROM users WHERE username = '$myusername' and passcode = '$mypassword'"; 
     $result = mysqli_query($db,$sql); 
     $row = mysqli_fetch_array($result,MYSQLI_ASSOC); 
     $active = $row['active']; 
     $count = mysqli_num_rows($result); 

     if($count == 1) { 
     session_register("myusername"); 
     $_SESSION['login_user'] = $myusername; 
     header("location: welcome.php"); 
     }else { 
     $error = "Your Login Name or Password is invalid"; 
     } 
    } 
?> 
<html> 
    <head> 
     <title>Login Page</title> 
    </head> 
    <body> 
     <div align = "center"> 
     <div>Login</div> 
     <form action = "" method = "post"> 
      <label>UserName :</label><input type = "text" name = "username" class = "box"/><br /><br /> 
      <label>Password :</label><input type = "password" name = "password" class = "box" /><br/><br /> 
      <input type = "submit" value = " Submit "/><br /> 
     </form> 
     <div><?php echo $error; ?></div> 
     </div> 
    </body> 
</html> 

session.php文件

<?php 
    include('DBConnection.php'); 
    session_start(); 

    $user_check = $_SESSION['login_user']; 

    $ses_sql = mysqli_query($db,"select username from users where username = '$user_check' "); 

    $row = mysqli_fetch_array($ses_sql,MYSQLI_ASSOC); 

    $login_session = $row['username']; 

    if(!isset($_SESSION['login_user'])){ 
     header("location:Login.php"); 
    } 
?> 

的welcome.php

<?php 
    include('Session.php'); 
?> 
<html>  
    <head> 
     <title>Welcome </title> 
    </head> 
    <body> 
     <h1>Welcome <?php echo $login_session; ?></h1> 
     <h2><a href = "Logout.php">Sign Out</a></h2> 
    </body> 
</html> 

Logout.php

<?php 
    session_start(); 
    if(session_destroy()) { 
     header("Location: Login.php"); 
    } 
?> 
+0

我討厭這樣的「答案」。代碼Blob沒有任何解釋。 Downvoted –

+1

SO作爲其他人的知識基礎。這不僅僅是你在這裏解決的問題,還有一些人可能會從未來的現有答案中受益。它也符合你的利益,以確保未來其他人可以獲得有用的,可讀的和可以理解的答案,否則從這個角度來看就沒有用處。 –

+0

@MarcinOrlowski和平兄弟...... OP是新的SO ......所以他很少出價情緒化....你顯然是對的你的觀點..事實上,我真的犯了錯誤,沒有給出適當的解釋。我知道,但實際上我缺乏時間,所以一開始試圖解決OP的問題。稍後,我將編輯我的答案,使其適合所有用戶。並且非常感謝您的建議。 –