Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class DBOperations {
- private $DBhost = '******';
- private $DBuser = '********';
- private $DBname = '********';
- private $DBpassword = '*******';
- private $conn;
- public function __construct(){
- $this -> conn = new PDO("mysql:host=".$this -> DBhost.";dbname=".$this -> DBname, $this -> DBuser, $this -> DBpassword);
- }
- public function insertData($email,$password,$phonenumber){
- $unique_id = uniqid('', true);
- $hash = $this->getHash($password);
- $encrypted_password = $hash["encrypted"];
- $salt = $hash["salt"];
- $sql = "INSERT INTO users (unique_id,email,encrypted_password,phone_number,salt,created_at) VALUES (:unique_id,:email,:encrypted_password,:phone_number,:salt, NOW())";
- $query = $this ->conn ->prepare($sql);
- $query->execute(array('unique_id' => $unique_id,':email' => $email,':encrypted_password' => $encrypted_password,':phone_number' => $phonenumber,':salt' => $salt));
- if($query){
- return true;
- } else {
- return false;
- }
- }
- public function insertData2($date_of_birth) {
- $sql = "INSERT INTO users (date_of_birth) VALUES (:date_of_birth)";
- $query = $this ->conn ->prepare($sql);
- $query->execute(array(':date_of_birth' => $date_of_birth));
- if($query){
- return true;
- } else {
- return false;
- }
- }
- public function insertData3($username) {
- $sql = "INSERT INTO users (username) VALUES (:username)";
- $query = $this ->conn ->prepare($sql);
- $query->execute(array(':username' => $username));
- if($query){
- return true;
- } else {
- return false;
- }
- }
- public function checkLogin($email, $password){
- $sql = 'SELECT * FROM users WHERE email = :email';
- $query = $this -> conn -> prepare($sql);
- $query -> execute(array(':email' => $email));
- $data = $query -> fetchObject();
- $salt = $data -> salt;
- $db_encrypted_password = $data -> encrypted_password;
- if($this -> verifyHash($password.$salt,$db_encrypted_password)) {
- $user["email"] = $data -> email;
- $user["phone_number"] = $data -> phone_number;
- $user["date_of_birth"] = $data -> date_of_birth;
- $user["username"] = $data -> username;
- $user["unique_id"] = $data -> unique_id;
- return $user;
- } else {
- return false;
- }
- }
- public function changePassword($email, $password) {
- $hash = $this -> getHash($password);
- $encrypted_password = $hash["encrypted"];
- $salt = $hash["salt"];
- $sql = 'UPDATE users SET encrypted_password = :encrypted_password, salt = :salt WHERE email = :email';
- $query = $this -> conn -> prepare($sql);
- $query -> execute(array(':email' => $email, ':encrypted_password' => $encrypted_password, ':salt' => $salt));
- if ($query) {
- return true;
- } else {
- return false;
- }
- }
- public function checkUserExist($email){
- $sql = 'SELECT COUNT(*) from users WHERE email =:email';
- $query = $this -> conn -> prepare($sql);
- $query -> execute(array('email' => $email));
- if($query){
- $row_count = $query -> fetchColumn();
- if ($row_count == 0){
- return false;
- } else {
- return true;
- }
- } else {
- return false;
- }
- }
- public function getHash($password) {
- $salt = sha1(rand());
- $salt = substr($salt, 0, 10);
- $encrypted = password_hash($password.$salt, PASSWORD_DEFAULT);
- $hash = array("salt" => $salt, "encrypted" => $encrypted);
- return $hash;
- }
- public function verifyHash($password, $hash) {
- return password_verify ($password, $hash);
- }
- }
Add Comment
Please, Sign In to add comment