php - Checking sha1 against stored sha1 password -
i'm hashing password using sha1 , storing in database, cannot seem check see if sha1 matches 1 in database. i've tried numerous different iterations of below code, nothing seems work - missing?
registration
<?php $username = $_post['username']; $password = $_post['password']; $passwordencrypted = sha1($password); try { $result = $db->prepare("insert user_info set username = :user, pass = :pass "); $result->bindparam(':user', $username); $result->bindparam(':pass', $passwordencrypted); $result->execute(); } catch (exception $e) { echo "could not create username"; } if (isset($_post['submit'])) { foreach ($_post $field) { if (empty($field)) { $fail = true; } else { $continue = false; } } if ($field == $fail) { echo "you must enter username and/or password"; } else { echo "your account has been created."; } } ?>
logging in
<?php $username = $_post['username']; $password = $_post['password']; $encryptedpassword = sha1($password); try { $result = $db->prepare("select username, pass user_info username = :user , binary pass = :pass"); $result->bindparam(':user', $username); $result->bindparam(':pass', $password); $result->execute(); $rows = $result->fetch(pdo::fetch_num); } catch (exception $e) { echo "could not retrieve data database"; exit(); } if ($rows) { session_start(); $_session['username'] = $_post['username']; $_session['loggedin'] = true; include("inc/redirect.php"); } else { if (isset($_post['login'])) { echo "username or password incorrect (passwords case sensitive)"; } } ?>
you need hash password before querying table, not afterwards:
<?php $username = $_post['username']; $password = $_post['password']; $passwordencrypted = sha1($password); try { $result = $db->prepare("select username, pass user_info username = :user , binary pass = :pass"); $result->bindparam(':user', $username); $result->bindparam(':pass', $passwordencrypted); $result->execute(); if ($result->fetch(pdo::fetch_num)) { session_start(); $_session['username'] = $_post['username']; $_session['loggedin'] = true; include("inc/redirect.php"); } else { if (isset($_post['login'])) { echo "username or password incorrect (passwords case sensitive)"; } } } catch (exception $e) { echo "could not retrieve data database"; exit(); } ?>
Comments
Post a Comment