Advertisement
Guest User

Untitled

a guest
Jul 26th, 2016
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.87 KB | None | 0 0
  1. <?php
  2. session_start();
  3.  
  4. require_once 'config.php';
  5.  
  6. /**
  7. * Include ircmaxell's password_compat library.
  8. */
  9. require '../lib/password.php';
  10.  
  11. if(isset($_POST['submit'])){
  12. //Retrieve the field values from our registration form.
  13. $username = !empty($_POST['username']) ? trim($_POST['username']) : null;
  14. $pass = !empty($_POST['password']) ? trim($_POST['password']) : null;
  15.  
  16. //Now, we need to check if the supplied username already exists.
  17. $stmt = $pdo->prepare("SELECT COUNT(username) AS num FROM users_table WHERE username = :username");
  18.  
  19. //Bind the provided username to our prepared statement.
  20. $stmt->bindValue(':username', $username);
  21.  
  22. //Execute.
  23. $stmt->execute();
  24.  
  25. //Fetch the row.
  26. $row = $stmt->fetch(PDO::FETCH_ASSOC);
  27.  
  28. //If the provided username already exists - display error.
  29. //TO ADD - Your own method of handling this error. For example purposes,
  30. //I'm just going to kill the script completely, as error handling is outside
  31. //the scope of this tutorial.
  32. if($row['num'] > 0){
  33. echo '<script>alert("That username already exist!");</script>';
  34. header("Refresh: 0; URL = signup.php");
  35. die();
  36. }
  37.  
  38. //Hash the password as we do NOT want to store our passwords in plain text.
  39. //$pas = hash('sha256', $pass);
  40. $pas = password_hash($pass, PASSWORD_BCRYPT, array("cost" => 12));
  41.  
  42. //Prepare our INSERT statement.
  43. //Remember: We are inserting a new row into our users table.
  44. $stmt = $pdo->prepare("INSERT INTO users_table (username, password) VALUES (:username, :password)");
  45.  
  46. //Bind our variables.
  47. $stmt->bindValue(':username', $username);
  48. $stmt->bindValue(':password', $pas);
  49.  
  50. //Execute the statement and insert the new account.
  51. $result = $stmt->execute();
  52.  
  53. //If the signup process is successful.
  54. if($result){
  55. echo '<script>alert("Thank you for register! Now please sign in.");</script>';
  56. header("Refresh: 0; URL = ../signin.php");
  57. }
  58. }
  59. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement