Created
January 7, 2022 17:00
-
-
Save CuzImBisonratte/bdcaff57afdede91c5bcb1859bec3c06 to your computer and use it in GitHub Desktop.
Login - Password checker
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Connect to the database | |
$con = new mysqli($db_host, $db_user, $db_pass, $dbname); | |
// Check connection | |
if ($con->connect_error) { | |
exit("Connection failed: " . $con->connect_error); | |
} | |
// Get the form input | |
$username = $_POST['username']; | |
$password = $_POST['password']; | |
// Check if the username and password are correct | |
if ($stmt = $con->prepare('SELECT password FROM accounts WHERE username = ?')) { | |
// Bind parameters (s = string, i = int, b = blob, etc) | |
$stmt->bind_param('s', $username); | |
$stmt->execute(); | |
// Store the result so we can check if the account exists in the database. | |
$stmt->store_result(); | |
// Check if Account exist | |
if ($stmt->num_rows > 0) { | |
$stmt->bind_result($id, $password); | |
$stmt->fetch(); | |
if (password_verify($_POST['password'], $password)) { | |
session_regenerate_id(); | |
$_SESSION['loggedin'] = TRUE; | |
$_SESSION['name'] = $_POST['username']; | |
$_SESSION['id'] = $id; | |
header('Location: ../app/'); | |
exit; | |
} else { | |
exit("Wrong password"); | |
} | |
} else { | |
exit("Wrong username"); | |
} | |
} else { | |
exit("Error: " . $con->error); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment