Advertisement
Guest User

Untitled

a guest
Jan 22nd, 2019
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.19 KB | None | 0 0
  1. $username = htmlspecialchars($_POST['username']);
  2. $password = htmlspecialchars($_POST['password']);
  3. $email = htmlspecialchars($_POST['email']);
  4.  
  5.  
  6.  
  7. echo register_user($username, $email, $password);
  8. }
  9.  
  10.  
  11. function connect($ip, $user, $password, $database){
  12. $con = mysqli_connect($ip, $user, $password, $database) or die('try again in some minutes, please');
  13. if (!$con)
  14. printf("Connect failed: %s\n", mysqli_connect_error());
  15. else
  16. return $con;
  17. }
  18.  
  19. function encrypt($username, $password)
  20. {
  21. $password = sha1(strtoupper($username) . ":" . strtoupper($password));
  22. $password = strtoupper($password);
  23. return $password;
  24. }
  25.  
  26. function check_user_exist($username){
  27. global $db_ip, $db_user, $db_password, $db_auth;
  28. $con = connect($db_ip, $db_user, $db_password, $db_auth);
  29. $stmt = $con->prepare("SELECT * FROM account WHERE `username`=?");
  30. $stmt->bind_param("s", $username);
  31. $stmt->execute();
  32. $stmt->store_result();
  33. return $stmt->num_rows;
  34. $stmt->close();
  35. $con->close();
  36. }
  37.  
  38. function check_email_exist($email){
  39. global $db_ip, $db_user, $db_password, $db_auth;
  40. $con = connect($db_ip, $db_user, $db_password, $db_auth);
  41. $stmt = $con->prepare("SELECT * FROM account WHERE `email`=?");
  42. $stmt->bind_param("s",$email);
  43. $stmt->execute();
  44. $stmt->store_result();
  45. return $stmt->num_rows;
  46. $stmt->close();
  47. $con->close();
  48. }
  49.  
  50. function register_user($username, $email, $password){
  51.  
  52. $new_password = encrypt($username,$password);
  53.  
  54. $sql = "INSERT INTO `account` (`username`, `sha_pass_hash`, `email`) VALUES (?,?,?)";
  55.  
  56. global $db_ip, $db_user, $db_password, $db_auth;
  57.  
  58. $con = connect($db_ip, $db_user, $db_password, $db_auth);
  59.  
  60. if (check_user_exist($username) > 0)
  61. return 1;
  62. if (check_email_exist($email) > 0)
  63. return 2;
  64.  
  65. if (check_user_exist($username) == 0 && check_email_exist($email) == 0) {
  66. if ($stmt = $con->prepare($sql)) {
  67. $stmt->bind_param("sss", $username, $new_password, $email);
  68. $stmt->execute();
  69. $stmt->close();
  70. return 3;
  71. }
  72. }
  73. return 0;
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement