Advertisement
Guest User

Untitled

a guest
Feb 15th, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.19 KB | None | 0 0
  1. LOGING
  2. <?php
  3. $db = new SQLite3('my_database') or die('unable to open database');
  4. $user = $_POST['username'];
  5. $pass = $_POST['password'];
  6.  
  7.  
  8.  
  9. $result = $db->querySingle("SELECT * FROM users WHERE username = '$user'", true) or die('Query failed');
  10.  
  11. $salt = $result['salt'];
  12.  
  13. $options = [
  14. 'cost' => 11,
  15. 'salt' => $salt,
  16. ];
  17.  
  18.  
  19. $pass = password_hash($pass, PASSWORD_BCRYPT, $options);
  20.  
  21. if($pass == $result['password']){
  22. echo "Welcome";
  23. }
  24. else{
  25. echo "Invalid username or password";
  26. }
  27.  
  28.  
  29. ?>
  30.  
  31.  
  32.  
  33.  
  34.  
  35. create user
  36. <?php
  37. $db = new SQLite3('my_database') or die('unable to open database');
  38. $query = <<<EOD
  39. CREATE TABLE IF NOT EXISTS users (
  40. username STRING PRIMARY KEY,
  41. password STRING,
  42. salt STRING)
  43. EOD;
  44. $db->exec($query) or die('Create db failed');
  45. $user = $_POST['username'];
  46. $pass = $_POST['password'];
  47. $salt = mcrypt_create_iv(22, MCRYPT_DEV_URANDOM);
  48.  
  49.  
  50. $options = [
  51. 'cost' => 11,
  52. 'salt' => $salt,
  53. ];
  54.  
  55. $pass = password_hash($pass, PASSWORD_BCRYPT, $options);
  56.  
  57. $query = <<<EOD
  58. INSERT INTO users VALUES ( '$user', '$pass', '$salt' )
  59. EOD;
  60.  
  61. $db->exec($query) or die("Unable to add user $user");
  62.  
  63. echo "Account created";
  64.  
  65.  
  66. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement