-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase.php
More file actions
124 lines (116 loc) · 3.8 KB
/
database.php
File metadata and controls
124 lines (116 loc) · 3.8 KB
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
<?php
class database {
private $pdo ;
public function __construct() {
$this->pdo = $this->Db();
$create_table = $this->pdo->prepare("create TABLE if not EXISTS users(
id int(10) PRIMARY KEY AUTO_INCREMENT,
email varchar(30) UNIQUE,
password varchar(255),
token varchar(255) null)
");
$create_table->execute();
}
function Db(){
try {
$this->pdo = new PDO("mysql:host=localhost;dbname=authentication _app","root","",[
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
]);
return $this->pdo;}
catch(PDOException $e){
echo "".$e->getMessage()."";
}
}
function InsertData($email , $password){
try{
$hashedpassword = password_hash($password, PASSWORD_DEFAULT);
$query = $this->pdo->prepare("insert into users (email,password) values (?,?)");
$query->bindParam(1,$email);
$query->bindParam(2,$hashedpassword);
$query->execute();
}catch(Exception $e){
echo "". $e->getMessage();
}
}
function ReadDb(){
try{
$query =$this->pdo ->query("Select * from users",PDO::FETCH_ASSOC);
}catch(Exception $e){
echo "". $e->getMessage();
}
return $query;
}
function CheckEmail($email){
try{
$query =$this->pdo -> prepare("select email from users where email = ? ");
$query->bindValue(1,$email);
$query->execute();
if ($query->rowCount()> 0){
return true;
}else return false;
}
catch(Exception $e){
echo "check email ". $e->getMessage();
return false;
}
}
function CheckPassword($email,$password){
try{
$query =$this->pdo ->prepare("select password from users where email = ? ");
$query->bindValue(1,$email);
$query->execute();
$hashedPassword=$query->fetchColumn();
if ($hashedPassword === "false"){//check if email does not exist the column gives as false
return false;
}
if (password_verify($password,$hashedPassword)){
return true;
}else { echo "fasle bw ";
return false;
}
}catch(Exception $e){
echo "check password". $e->getMessage();
return false;
}
}
function InsertToken($email , $token ){
try{
$query =$this->pdo -> prepare("update users set token = ? where email = ?");
$query ->bindValue(1,$token);
$query->bindValue(2,$email);
$query->execute();
}catch (Exception $e){
echo "". $e->getMessage();
}
}
function CheckToken($token){
try{
$query =$this->pdo -> prepare("select email from users where token = ?");
$query ->bindValue(1,$token);
$query->execute();
$featchToken = $query->fetchColumn();
if ($featchToken){
return true;
}else return false ;
}catch(Exception $e){
echo "". $e->getMessage();
return false;
}
}
function ChangePassword($email,$password){
try{
$query =$this->pdo -> prepare("update table users set password=? where email=?");
$query ->bindValue(1,$password);
$query->bindParam(2,$email);
$query->execute();
if($query->rowCount()> 0){
echo "password updated successfully";
}else {
echo "an error accured";
}
}catch(Exception $e){
echo "". $e->getMessage();
}
}
}
?>