-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathuser_class.php
84 lines (69 loc) · 2.39 KB
/
user_class.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
<?php
require_once("db_config.php");
class User{
private $connection;
public function __construct(){
$db = new Database();
$this->connection = $db->dbConnect();
}
public function runQuery($sql_query){
$stmt = $this->connection->prepare($sql_query);
return $stmt;
}
public function logIn($username, $passwd){
try{
$stmt = $this->connection->prepare("SELECT user_id, user_name, user_passwd, type_id FROM users WHERE user_name = :username");
$stmt->execute(array(':username' => $username));
$user_row = $stmt->fetch(PDO::FETCH_ASSOC);
if($stmt->rowCount() == 1){
if(password_verify($passwd, $user_row['user_passwd'])){
$_SESSION['user_session'] = $user_row['user_id'];
$_SESSION['user_login'] = $user_row['user_name'];
$_SESSION['user_type'] = $user_row['type_id'];
return true;
}
else{
return false;
}
}
}
catch(PDOException $exception){
echo $exception->getMessage();
}
}
public function isLoggedIn(){
if(isset($_SESSION['user_session'])){
return true;
}
}
public function redirectTo($url){
header("Location: $url");
}
public function logOut(){
session_destroy();
unset($_SESSION['user_session']);
return true;
}
public function register($username,$email,$password) {
$type = 2;
$active = 1;
try
{
$new_password = password_hash($password, PASSWORD_DEFAULT);
$stmt = $this->connection->prepare("INSERT INTO users(user_name,user_email,user_passwd,type_id,is_active)
VALUES(:username, :email, :password, :type, :active)");
$stmt->bindparam(":username", $username);
$stmt->bindparam(":email", $email);
$stmt->bindparam(":password", $new_password);
$stmt->bindparam(":type", $type);
$stmt->bindparam(":active", $active);
$stmt->execute();
return $stmt;
}
catch(PDOException $e)
{
echo $e->getMessage();
}
}
}
?>