Advertisement
darryljf

user.class.php

Mar 16th, 2020
226
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.80 KB | None | 0 0
  1.   class User{
  2.     // $Conn is the database connection handler defined in db.include.php
  3.     protected $Conn;
  4.     // the constructor links $Conn inside the User class to the one outside of class defined in db.include.php
  5.     public function __construct($Conn) {
  6.       $this->Conn = $Conn;
  7.     }
  8.  
  9.     public function createUser($user_data) {
  10.       // take users set password and convert to hash value using password_hash method
  11.       $sec_password = password_hash($user_data['password'], PASSWORD_DEFAULT);
  12.       // INSERT values into database using :placeholders
  13.       $query = "INSERT INTO users(user_email, user_pass, user_name) VALUES
  14.      (:user_email, :user_pass, :user_name)";
  15.  
  16.       $stmt = $this->Conn->prepare($query);
  17.       // execute the statement using an array to pass the values
  18.       return $stmt->execute(array(
  19.         'user_email'=>$user_data['email'],
  20.         'user_pass'=>$sec_password,
  21.         'user_name'=>$user_data['fullName']
  22.         // method complete, refer to join.php
  23.       ));
  24.     }
  25.  
  26.     public function loginUser($email,$password){
  27.       // query database for users email address
  28.       $query = "SELECT * FROM users WHERE user_email = :user_email";
  29.       $stmt = $this->Conn->prepare($query);
  30.       // execute query using user email
  31.       $stmt->execute(array(':user_email'=>$email));
  32.       // fetch the results of query
  33.       $attempt = $stmt->fetch();
  34.  
  35.       if($attempt && password_verify($password,$attempt['user_pass'])) {
  36.         return $attempt;
  37.       }else{
  38.         return false;
  39.       }
  40.     }
  41.     public function getUser($user_id){
  42.       // gets all user_id
  43.       $query = "SELECT * FROM users WHERE user_id = :user_id";
  44.       $stmt = $this->$Conn->prepare($query);
  45.       $stmt->execute(array(':user_id' => $user_id));
  46.       return $stmt->fetch();
  47.     }
  48.   }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement