Skip to content

Instantly share code, notes, and snippets.

@ineentho
Created May 20, 2015 18:51
Show Gist options
  • Save ineentho/12a4bf88de1a7dd614f3 to your computer and use it in GitHub Desktop.
Save ineentho/12a4bf88de1a7dd614f3 to your computer and use it in GitHub Desktop.
<?php
$dbh = new PDO('mysql:host=localhost;dbname=test', 'root', 'sten123');
function authenticate($username, $password) {
global $dbh;
$loginStmt = $dbh->prepare('SELECT password FROM users WHERE username = :username');
$loginStmt->bindValue(':username', $username);
$loginStmt->execute();
$row = $loginStmt->fetch(PDO::FETCH_ASSOC);
if (!$row)
return 'Account not found';
if (!password_verify($password, $row['password']))
return 'Incorrect password';
return null;
}
$error = authenticate('Ineentho', 'sten123');
if ($error) {
echo 'Could not login: ' . $error;
} else {
echo 'Logged in';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment