SHARE
TWEET

Untitled

a guest Aug 13th, 2016 67 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2.  
  3. class DBOperations{
  4.  
  5.      private $host = '127.0.0.1';
  6.      private $user = 'root';
  7.      private $db = 'UserDB';
  8.      private $pass = '';
  9.      private $conn;
  10.  
  11. public function __construct() {
  12.  
  13.     $this -> conn = new PDO("mysql:host=".$this -> host.";dbname=".$this -> db, $this -> user, $this -> pass);
  14.  
  15. }
  16.  
  17.  
  18.  public function insertData($name,$email,$password){
  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 SET unique_id =:unique_id,name =:name,
  26.     email =:email,encrypted_password =:encrypted_password,salt =:salt,created_at = NOW()';
  27.  
  28.     $query = $this ->conn ->prepare($sql);
  29.     $query->execute(array('unique_id' => $unique_id, ':name' => $name, ':email' => $email,
  30.      ':encrypted_password' => $encrypted_password, ':salt' => $salt));
  31.  
  32.     if ($query) {
  33.        
  34.         return true;
  35.  
  36.     } else {
  37.  
  38.         return false;
  39.  
  40.     }
  41.  }
  42.  
  43.  
  44.  public function checkLogin($email, $password) {
  45.  
  46.     $sql = 'SELECT * FROM users WHERE email = :email';
  47.     $query = $this -> conn -> prepare($sql);
  48.     $query -> execute(array(':email' => $email));
  49.     $data = $query -> fetchObject();
  50.     $salt = $data -> salt;
  51.     $db_encrypted_password = $data -> encrypted_password;
  52.  
  53.     if ($this -> verifyHash($password.$salt,$db_encrypted_password) ) {
  54.  
  55.  
  56.         $user["name"] = $data -> name;
  57.         $user["email"] = $data -> email;
  58.         $user["unique_id"] = $data -> unique_id;
  59.         return $user;
  60.  
  61.     } else {
  62.  
  63.         return false;
  64.     }
  65.  
  66.  }
  67.  
  68.  
  69.  public function changePassword($email, $password){
  70.  
  71.  
  72.     $hash = $this -> getHash($password);
  73.     $encrypted_password = $hash["encrypted"];
  74.     $salt = $hash["salt"];
  75.  
  76.     $sql = 'UPDATE users SET encrypted_password = :encrypted_password, salt = :salt WHERE email = :email';
  77.     $query = $this -> conn -> prepare($sql);
  78.     $query -> execute(array(':email' => $email, ':encrypted_password' => $encrypted_password, ':salt' => $salt));
  79.  
  80.     if ($query) {
  81.        
  82.         return true;
  83.  
  84.     } else {
  85.  
  86.         return false;
  87.  
  88.     }
  89.  
  90.  }
  91.  
  92.  public function checkUserExist($email){
  93.  
  94.     $sql = 'SELECT COUNT(*) from users WHERE email =:email';
  95.     $query = $this -> conn -> prepare($sql);
  96.     $query -> execute(array('email' => $email));
  97.  
  98.     if($query){
  99.  
  100.         $row_count = $query -> fetchColumn();
  101.  
  102.         if ($row_count == 0){
  103.  
  104.             return false;
  105.  
  106.         } else {
  107.  
  108.             return true;
  109.  
  110.         }
  111.     } else {
  112.  
  113.         return false;
  114.     }
  115.  }
  116.  
  117.  public function getHash($password) {
  118.  
  119.      $salt = sha1(rand());
  120.      $salt = substr($salt, 0, 10);
  121.      $encrypted = password_hash($password.$salt, PASSWORD_DEFAULT);
  122.      $hash = array("salt" => $salt, "encrypted" => $encrypted);
  123.  
  124.      return $hash;
  125.  
  126. }
  127.  
  128.  
  129.  
  130. public function verifyHash($password, $hash) {
  131.  
  132.     return password_verify ($password, $hash);
  133. }
  134. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top