Last active
January 22, 2017 12:10
-
-
Save bradynpoulsen/4a418559c6543de984a7e5e17cd7d8c4 to your computer and use it in GitHub Desktop.
Basic PHP Password Hashing
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
<?php | |
$user = new User; | |
$user->email = 'foo@example.com'; | |
$user->password_digest = password_hash('mypassword', PASSWORD_DEFAULT); | |
$user->save(); |
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
<?php | |
$providedEmail = $_SERVER['PHP_AUTH_USER']; | |
$providedPassword = $_SERVER['PHP_AUTH_PW']; | |
$user = User::find_by_email($providedEmail); | |
if ($user) { | |
if (password_verify($providedPassword, $user->password_digest)) { | |
session_start(); | |
$_SESSION['user_id'] = $user->id; | |
header('HTTP/1.1 200 Ok'); | |
header('Location: https://example.com/my-account'); | |
echo 'You have successfully logged in!'; | |
exit(); | |
} | |
} | |
header('HTTP/1.1 403 Forbidden'); | |
echo 'You provided an invalid email/password'; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment