I'm working on this since Thursday night but I can not figure it out what I'm doing wrong . I'm trying just create a simple login form . I have user and admin .when I'm trying to login as a user it says the user's username and password is invalid even my password and username are correct .any help appreciate it in advance .
<?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