It gives me always Invalid Username or Password
what is the problem with this code ?
<form action="admin_login.php" method="POST">
<div class="form-group" style="width: 280px;">
<label for="usr">Username</label>
<input type="text" class="form-control" name="userbox" id="user">
<br>
<label for="pass">Password</label>
<input type="password" class="form-control" name="passbox" id="pass">
<br>
<button type="submit" name="login" class="btn btn-success">Log In</button>
</div>
<?php
if(isset($_POST["login"])) {
$user = $_POST["userbox"];
$pass = password_encrypt($_POST["passbox"]);
$sql = "SELECT `ID` FROM `admins` WHERE `username`='".$user."' AND `password`='".$pass."'";
$result = $conn->query($sql);
if(!$result) {
echo "There is problem with connection try again later.";
trigger_error('Wrong SQL: ' . $sql . ' Error: ' . $conn->error, E_USER_ERROR);
} else {
if($result->num_rows > 0) {
$row = $result->fetch_assoc();
$_SESSION['admin_id'] = $row['ID'];
header('Location: admin.php');
} else {
$_SESSION['admin_id'] = '';
?>
<li>Invalid Username or Password</li>
<a href="#">Forgot your password?</a>
<?php
}
}
//mysqli_close($conn);
}
?>
</form>
**Password encrypt**
function password_encrypt($password) {
$hash_format = "$2y$10$";
$salt_lenght = 22;
$salt = generate_salt($salt_lenght);
$format_and_salt = $hash_format . $salt_lenght;
$hash = crypt($password, "e5h8g7ghe58g7e5hg8e57he58h7j10jxd");
return $hash;
}
function generate_salt($lenght) {
$unique_random_string = md5(uniqid(mt_rand(), true));
$base64_string = base64_encode($unique_random_string);
$modified_base64_string = str_replace('+', '.', $base64_string);
$salt = substr($modified_base64_string, 0, $lenght);
return $salt;
}
function password_check($password, $existing_hash) {
$hash = crypt($password, $existing_hash);
if($hash === $existing_hash) {
return true;
} else {
return false;
}
}
Also i want to ask does this code is secure to use and can i put even more security ?