Guest User

Untitled

a guest
Jan 8th, 2018
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.22 KB | None | 0 0
  1. <?php
  2.  
  3. class DBOperations {
  4.  
  5. private $DBhost = '******';
  6. private $DBuser = '********';
  7. private $DBname = '********';
  8. private $DBpassword = '*******';
  9. private $conn;
  10.  
  11. public function __construct(){
  12.  
  13. $this -> conn = new PDO("mysql:host=".$this -> DBhost.";dbname=".$this -> DBname, $this -> DBuser, $this -> DBpassword);
  14.  
  15.  
  16. }
  17.  
  18. public function insertData($email,$password,$phonenumber){
  19.  
  20. $unique_id = uniqid('', true);
  21. $hash = $this->getHash($password);
  22. $encrypted_password = $hash["encrypted"];
  23. $salt = $hash["salt"];
  24.  
  25. $sql = "INSERT INTO users (unique_id,email,encrypted_password,phone_number,salt,created_at) VALUES (:unique_id,:email,:encrypted_password,:phone_number,:salt, NOW())";
  26.  
  27. $query = $this ->conn ->prepare($sql);
  28. $query->execute(array('unique_id' => $unique_id,':email' => $email,':encrypted_password' => $encrypted_password,':phone_number' => $phonenumber,':salt' => $salt));
  29.  
  30. if($query){
  31.  
  32. return true;
  33. } else {
  34. return false;
  35. }
  36. }
  37.  
  38. public function insertData2($date_of_birth) {
  39.  
  40. $sql = "INSERT INTO users (date_of_birth) VALUES (:date_of_birth)";
  41.  
  42. $query = $this ->conn ->prepare($sql);
  43. $query->execute(array(':date_of_birth' => $date_of_birth));
  44.  
  45. if($query){
  46. return true;
  47. } else {
  48. return false;
  49. }
  50. }
  51.  
  52. public function insertData3($username) {
  53.  
  54. $sql = "INSERT INTO users (username) VALUES (:username)";
  55.  
  56. $query = $this ->conn ->prepare($sql);
  57. $query->execute(array(':username' => $username));
  58.  
  59. if($query){
  60. return true;
  61. } else {
  62. return false;
  63. }
  64. }
  65.  
  66.  
  67. public function checkLogin($email, $password){
  68.  
  69. $sql = 'SELECT * FROM users WHERE email = :email';
  70. $query = $this -> conn -> prepare($sql);
  71. $query -> execute(array(':email' => $email));
  72. $data = $query -> fetchObject();
  73. $salt = $data -> salt;
  74. $db_encrypted_password = $data -> encrypted_password;
  75.  
  76. if($this -> verifyHash($password.$salt,$db_encrypted_password)) {
  77.  
  78. $user["email"] = $data -> email;
  79. $user["phone_number"] = $data -> phone_number;
  80. $user["date_of_birth"] = $data -> date_of_birth;
  81. $user["username"] = $data -> username;
  82. $user["unique_id"] = $data -> unique_id;
  83.  
  84. return $user;
  85.  
  86. } else {
  87.  
  88. return false;
  89. }
  90. }
  91.  
  92. public function changePassword($email, $password) {
  93.  
  94. $hash = $this -> getHash($password);
  95. $encrypted_password = $hash["encrypted"];
  96. $salt = $hash["salt"];
  97.  
  98. $sql = 'UPDATE users SET encrypted_password = :encrypted_password, salt = :salt WHERE email = :email';
  99. $query = $this -> conn -> prepare($sql);
  100. $query -> execute(array(':email' => $email, ':encrypted_password' => $encrypted_password, ':salt' => $salt));
  101.  
  102. if ($query) {
  103.  
  104. return true;
  105.  
  106. } else {
  107.  
  108. return false;
  109.  
  110. }
  111. }
  112.  
  113. public function checkUserExist($email){
  114.  
  115. $sql = 'SELECT COUNT(*) from users WHERE email =:email';
  116. $query = $this -> conn -> prepare($sql);
  117. $query -> execute(array('email' => $email));
  118.  
  119. if($query){
  120.  
  121. $row_count = $query -> fetchColumn();
  122.  
  123. if ($row_count == 0){
  124.  
  125. return false;
  126.  
  127. } else {
  128.  
  129. return true;
  130.  
  131. }
  132. } else {
  133.  
  134. return false;
  135. }
  136. }
  137. public function getHash($password) {
  138.  
  139. $salt = sha1(rand());
  140. $salt = substr($salt, 0, 10);
  141. $encrypted = password_hash($password.$salt, PASSWORD_DEFAULT);
  142. $hash = array("salt" => $salt, "encrypted" => $encrypted);
  143.  
  144. return $hash;
  145.  
  146. }
  147. public function verifyHash($password, $hash) {
  148.  
  149. return password_verify ($password, $hash);
  150. }
  151.  
  152. }
Add Comment
Please, Sign In to add comment