Advertisement
Guest User

Untitled

a guest
Sep 25th, 2016
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.69 KB | None | 0 0
  1. <?php
  2.  
  3. class Register {
  4.  
  5. private $username, $email, $pasword, $re_password;
  6. private $connection;
  7. private $error;
  8.  
  9. function __construct($username, $email, $password, $re_password) {
  10.  
  11. try {
  12.  
  13. $this->connection = new PDO("mysql:host=localhost; dbname=test", "root", "");
  14. $this->connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  15.  
  16. } catch (Exception $e) {
  17.  
  18. die("Error: " . $e->getMessage());
  19.  
  20. }
  21.  
  22. $this->username = $username;
  23.  
  24. $this->email = $email;
  25.  
  26. $this->password = md5($password);
  27.  
  28. $this->re_password = md5($re_password);
  29.  
  30. if(!$this->validate_username() || !$this->validate_email() || !$this->validate_password()) {
  31.  
  32. echo $this->error;
  33.  
  34. }else {
  35.  
  36. try {
  37.  
  38. $sql = "INSERT INTO users (username, email, password) VALUES (:username, :email, :password)";
  39.  
  40. $result = $this->connection->prepare($sql);
  41.  
  42. $result->bindValue(":username", $this->username);
  43. $result->bindValue(":email", $this->email);
  44. $result->bindValue(":password", $this->password);
  45.  
  46. $result->execute();
  47.  
  48. } catch (Exception $e) {
  49.  
  50. die("Error: ".$e->getMessage());
  51.  
  52. }
  53.  
  54. }
  55.  
  56. }
  57.  
  58. private function validate_username() {
  59.  
  60. try {
  61.  
  62. $sql = "SELECT username FROM users WHERE username = :username";
  63.  
  64. $result = $this->connection->prepare($sql);
  65.  
  66. $result->bindValue(":username", $this->username);
  67.  
  68. $result->execute();
  69.  
  70. if($result->rowCount() > 0) {
  71.  
  72. $this->error ="El username introducido ya existe.";
  73. return false;
  74.  
  75. }else {
  76.  
  77. return true;
  78.  
  79. }
  80.  
  81. } catch (Exception $e) {
  82.  
  83. die("Error: ".$e->getMessage());
  84.  
  85. }
  86.  
  87. }
  88.  
  89. private function validate_email() {
  90.  
  91. if (!preg_match('/^[A-Za-z0-9-_.+%]+@[A-Za-z0-9-.]+\.[A-Za-z]{2,4}$/', $this->email)) {
  92.  
  93. $this->error = "El email debe tener la siguiente estructura: name@domain.sub";
  94. return false;
  95.  
  96. }else {
  97.  
  98. try {
  99.  
  100. $sql = "SELECT email FROM users WHERE email = :email";
  101.  
  102. $result = $this->connection->prepare($sql);
  103.  
  104. $result->bindValue(":email", $this->email);
  105.  
  106. $result->execute();
  107.  
  108. if($result->rowCount() > 0) {
  109.  
  110. $this->error = "El email introducido ya existe.";
  111. return false;
  112.  
  113. }else {
  114.  
  115. return true;
  116.  
  117. }
  118.  
  119. } catch (Exception $e) {
  120.  
  121. die("Error: ".$e->getMessage());
  122.  
  123. }
  124.  
  125. }
  126.  
  127. }
  128.  
  129. private function validate_password() {
  130.  
  131. if($this->password != $this->re_password) {
  132.  
  133. $this->error = "Passwords don't match.";
  134. return false;
  135.  
  136. }else {
  137.  
  138. return true;
  139.  
  140. }
  141.  
  142. }
  143.  
  144. }
  145.  
  146. //$register = new Register("usern2ame", "email@domain2.sub", "password", "password");
  147.  
  148. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement