Advertisement
Magento15

db_function.php

Aug 19th, 2018
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.42 KB | None | 0 0
  1. <?php
  2. /**
  3. *
  4. */
  5. class DB_Functions
  6. {
  7.    
  8.     //constructor
  9.     function __construct()
  10.     {
  11.         require_once 'db_connect.php';
  12.         $db = new DB_Connect();
  13.         $this->conn = $db->connect();
  14.     }
  15.  
  16.     //destructor
  17.     function __destruct()
  18.     {
  19.         //TODO: Implement __destruct() method.
  20.     }
  21.  
  22.     //store new user
  23.     //return user details
  24.     public function storeUser($nama,$email,$password)
  25.     {
  26.         $uuid = uniqid('',true);
  27.         $hash = $this->hashSSHA($password);
  28.         $encrypted_password = $hash["encrypted"]; //encrypted password
  29.         $salt = $hash["salt"];
  30.  
  31.         $stmt = $this->conn->prepare("INSERT INTO users(unique_id,name,email,encrypted_password,salt,created_at) VALUES (?,?,?,?,?,NOW())");
  32.         $stmt->bind_param("sssss",$uuid,$name,$email,$encrypted_password,$salt);
  33.         $result = $stmt->execute();
  34.         $stmt->close();
  35.  
  36.         //check for succesful store
  37.         if ($result) {
  38.             $stmt = $this->conn->prepare("SELECT * FROM users WHERE email = ?");
  39.             $stmt = bind_param("s",$email);
  40.             $stmt->execute();
  41.             $user = $stmt->get_result()->fetch_assoc();
  42.             $stmt->close();
  43.  
  44.             return $user;
  45.         }else {
  46.             return false;
  47.         }
  48.  
  49.     }
  50.  
  51.     //get user by email and password
  52.     public function getUserByEmailAndPassword($email, $password)
  53.     {
  54.         $stmt = $this->conn->prepare("SELECT * FROM users WHERE email =?");
  55.         $stmt->bind_param("s",$email);
  56.  
  57.         if ($stmt->execute())
  58.         {
  59.             $user = $stmt->get_result()->fetch_assoc();
  60.             $stmt->close();
  61.  
  62.             //veryfing user password
  63.             $salt = $user['salt'];
  64.             $encrypted_password = $user['encrypted_password'];
  65.  
  66.             //check password for equality
  67.             if ($encrypted_password == $hash)
  68.                 return $user;
  69.         }
  70.         else
  71.         {
  72.             return NULL;
  73.         }
  74.     }
  75.  
  76.     //check user existed or not
  77.     public function isUserExisted($email)
  78.     {
  79.         $stmt = $this->conn->prepare("SELECT email FROM users WHERE email =?");
  80.         $stmt->bind_param("s",$email);
  81.         $stmt->execute();
  82.         $stmt->store_result();
  83.  
  84.         if ($stmt->num_rows > 0) {
  85.             $stmt->close();
  86.             return true;
  87.         }
  88.         else
  89.         {
  90.             $stmt->close();
  91.             return false;
  92.         }
  93.     }
  94.  
  95.     //encrypting password
  96.     public function hashSSHA($password)
  97.     {
  98.         $salt = sha1(rand());
  99.         $salt = substr($salt, 0, 10);
  100.         $encrypted = base64_encode(sha1($password.$salt, true).$salt);
  101.         $hash = array("salt"=>$salt,"encrypted"=>$encrypted);
  102.         return $hash;
  103.     }
  104.     //decrypted password
  105.     public function checkhashSSHA($salt,$password)
  106.     {
  107.         $hash = base64_encode(sha1($password.$salt,true).$salt);
  108.         return $hash;
  109.     }
  110. }
  111.  ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement