Advertisement
Guest User

Untitled

a guest
Apr 10th, 2017
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.43 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. $hash = $this->hashSSHA($password);
  31. $encrypted_password = $hash["encrypted"]; // encrypted password
  32. $salt = $hash["salt"]; // salt
  33.  
  34. $stmt = $this->conn->prepare("INSERT INTO users(name, email, encrypted_password, salt) VALUES(?, ?, ?, ?)");
  35. $stmt->bind_param("ssss", $name, $email, $encrypted_password, $salt);
  36. $result = $stmt->execute();
  37. $stmt->close();
  38.  
  39. // check for successful store
  40. if ($result) {
  41. $stmt = $this->conn->prepare("SELECT * FROM users WHERE email = ?");
  42. $stmt->bind_param("s", $email);
  43. $stmt->execute();
  44. $user = $stmt->get_result()->fetch_assoc();
  45. $stmt->close();
  46.  
  47. return $user;
  48. } else {
  49. return false;
  50. }
  51. }
  52.  
  53. /**
  54. * Get user by email and password
  55. */
  56. public function getUserByEmailAndPassword($email, $password) {
  57.  
  58. $stmt = $this->conn->prepare("SELECT * FROM users WHERE email = ?");
  59.  
  60. $stmt->bind_param("s", $email);
  61.  
  62. if ($stmt->execute()) {
  63. $user = $stmt->get_result()->fetch_assoc();
  64. $stmt->close();
  65.  
  66. // verifying user password
  67. $salt = $user['salt'];
  68. $encrypted_password = $user['encrypted_password'];
  69. $hash = $this->checkhashSSHA($salt, $password);
  70. // check for password equality
  71. if ($encrypted_password == $hash) {
  72. // user authentication details are correct
  73. return $user;
  74. }
  75. } else {
  76. return NULL;
  77. }
  78. }
  79.  
  80. /**
  81. * Check user is existed or not
  82. */
  83. public function isUserExisted($email) {
  84. $stmt = $this->conn->prepare("SELECT email from users WHERE email = ?");
  85.  
  86. $stmt->bind_param("s", $email);
  87.  
  88. $stmt->execute();
  89.  
  90. $stmt->store_result();
  91.  
  92. if ($stmt->num_rows > 0) {
  93. // user existed
  94. $stmt->close();
  95. return true;
  96. } else {
  97. // user not existed
  98. $stmt->close();
  99. return false;
  100. }
  101. }
  102.  
  103. /**
  104. * Encrypting password
  105. * @param password
  106. * returns salt and encrypted password
  107. */
  108. public function hashSSHA($password) {
  109.  
  110. $salt = sha1(rand());
  111. $salt = substr($salt, 0, 10);
  112. $encrypted = base64_encode(sha1($password . $salt, true) . $salt);
  113. $hash = array("salt" => $salt, "encrypted" => $encrypted);
  114. return $hash;
  115. }
  116.  
  117. /**
  118. * Decrypting password
  119. * @param salt, password
  120. * returns hash string
  121. */
  122. public function checkhashSSHA($salt, $password) {
  123.  
  124. $hash = base64_encode(sha1($password . $salt, true) . $salt);
  125.  
  126. return $hash;
  127. }
  128.  
  129. }
  130.  
  131. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement