Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- *
- */
- class DB_Functions
- {
- //constructor
- function __construct()
- {
- require_once 'db_connect.php';
- $db = new DB_Connect();
- $this->conn = $db->connect();
- }
- //destructor
- function __destruct()
- {
- //TODO: Implement __destruct() method.
- }
- //store new user
- //return user details
- public function storeUser($nama,$email,$password)
- {
- $uuid = uniqid('',true);
- $hash = $this->hashSSHA($password);
- $encrypted_password = $hash["encrypted"]; //encrypted password
- $salt = $hash["salt"];
- $stmt = $this->conn->prepare("INSERT INTO users(unique_id,name,email,encrypted_password,salt,created_at) VALUES (?,?,?,?,?,NOW())");
- $stmt->bind_param("sssss",$uuid,$name,$email,$encrypted_password,$salt);
- $result = $stmt->execute();
- $stmt->close();
- //check for succesful store
- if ($result) {
- $stmt = $this->conn->prepare("SELECT * FROM users WHERE email = ?");
- $stmt = bind_param("s",$email);
- $stmt->execute();
- $user = $stmt->get_result()->fetch_assoc();
- $stmt->close();
- return $user;
- }else {
- return false;
- }
- }
- //get user by email and password
- public function getUserByEmailAndPassword($email, $password)
- {
- $stmt = $this->conn->prepare("SELECT * FROM users WHERE email =?");
- $stmt->bind_param("s",$email);
- if ($stmt->execute())
- {
- $user = $stmt->get_result()->fetch_assoc();
- $stmt->close();
- //veryfing user password
- $salt = $user['salt'];
- $encrypted_password = $user['encrypted_password'];
- //check password for equality
- if ($encrypted_password == $hash)
- return $user;
- }
- else
- {
- return NULL;
- }
- }
- //check user existed or not
- public function isUserExisted($email)
- {
- $stmt = $this->conn->prepare("SELECT email FROM users WHERE email =?");
- $stmt->bind_param("s",$email);
- $stmt->execute();
- $stmt->store_result();
- if ($stmt->num_rows > 0) {
- $stmt->close();
- return true;
- }
- else
- {
- $stmt->close();
- return false;
- }
- }
- //encrypting password
- public function hashSSHA($password)
- {
- $salt = sha1(rand());
- $salt = substr($salt, 0, 10);
- $encrypted = base64_encode(sha1($password.$salt, true).$salt);
- $hash = array("salt"=>$salt,"encrypted"=>$encrypted);
- return $hash;
- }
- //decrypted password
- public function checkhashSSHA($salt,$password)
- {
- $hash = base64_encode(sha1($password.$salt,true).$salt);
- return $hash;
- }
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement