Advertisement
taufiqhidayah97

db_function

Sep 12th, 2016
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.48 KB | None | 0 0
  1. <?php
  2.  
  3. /**
  4. * @author Ravi Tamada
  5. * @link http://www.androidhive.info/2012/01/android-login-and-registration-with-php-mysql-and-sqlite/ Complete tutorial
  6. */
  7.  
  8. class DB_Functions {
  9.  
  10. private $conn;
  11.  
  12. // constructor
  13. function __construct() {
  14. require_once 'DB_Connect.php';
  15. // connecting to database
  16. $db = new Db_Connect();
  17. $this->conn = $db->connect();
  18. }
  19.  
  20. // destructor
  21. function __destruct() {
  22.  
  23. }
  24.  
  25. /**
  26. * Storing new user
  27. * returns user details
  28. */
  29. public function storeUser($name, $email, $password) {
  30. $uuid = uniqid('', true);
  31. $hash = $this->hashSSHA($password);
  32. $encrypted_password = $hash["encrypted"]; // encrypted password
  33. $salt = $hash["salt"]; // salt
  34.  
  35. $stmt = $this->conn->prepare("INSERT INTO users(unique_id, name, email, encrypted_password, salt, created_at) VALUES(?, ?, ?, ?, ?, NOW())");
  36. $stmt->bind_param("sssss", $uuid, $name, $email, $encrypted_password, $salt);
  37. $result = $stmt->execute();
  38. $stmt->close();
  39.  
  40. // check for successful store
  41. if ($result) {
  42. $stmt = $this->conn->prepare("SELECT * FROM users WHERE email = ?");
  43. $stmt->bind_param("s", $email);
  44. $stmt->execute();
  45. $user = $stmt->get_result()->fetch_assoc();
  46. $stmt->close();
  47.  
  48. return $user;
  49. } else {
  50. return false;
  51. }
  52. }
  53.  
  54. /**
  55. * Get user by email and password
  56. */
  57. public function getUserByEmailAndPassword($email, $password) {
  58.  
  59. $stmt = $this->conn->prepare("SELECT * FROM users WHERE email = ?");
  60.  
  61. $stmt->bind_param("s", $email);
  62.  
  63. if ($stmt->execute()) {
  64. $user = $stmt->get_result()->fetch_assoc();
  65. $stmt->close();
  66.  
  67. // verifying user password
  68. $salt = $user['salt'];
  69. $encrypted_password = $user['encrypted_password'];
  70. $hash = $this->checkhashSSHA($salt, $password);
  71. // check for password equality
  72. if ($encrypted_password == $hash) {
  73. // user authentication details are correct
  74. return $user;
  75. }
  76. } else {
  77. return NULL;
  78. }
  79. }
  80.  
  81. /**
  82. * Check user is existed or not
  83. */
  84. public function isUserExisted($email) {
  85. $stmt = $this->conn->prepare("SELECT email from users WHERE email = ?");
  86.  
  87. $stmt->bind_param("s", $email);
  88.  
  89. $stmt->execute();
  90.  
  91. $stmt->store_result();
  92.  
  93. if ($stmt->num_rows > 0) {
  94. // user existed
  95. $stmt->close();
  96. return true;
  97. } else {
  98. // user not existed
  99. $stmt->close();
  100. return false;
  101. }
  102. }
  103.  
  104. /**
  105. * Encrypting password
  106. * @param password
  107. * returns salt and encrypted password
  108. */
  109. public function hashSSHA($password) {
  110.  
  111. $salt = sha1(rand());
  112. $salt = substr($salt, 0, 10);
  113. $encrypted = base64_encode(sha1($password . $salt, true) . $salt);
  114. $hash = array("salt" => $salt, "encrypted" => $encrypted);
  115. return $hash;
  116. }
  117.  
  118. /**
  119. * Decrypting password
  120. * @param salt, password
  121. * returns hash string
  122. */
  123. public function checkhashSSHA($salt, $password) {
  124.  
  125. $hash = base64_encode(sha1($password . $salt, true) . $salt);
  126.  
  127. return $hash;
  128. }
  129.  
  130. }
  131.  
  132. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement