forked from meganspeir/php-verify
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase.php
More file actions
executable file
·103 lines (81 loc) · 2.84 KB
/
Copy pathdatabase.php
File metadata and controls
executable file
·103 lines (81 loc) · 2.84 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
<?php
function setupDatabase()
{
// put your database information here
$username = 'root';
$password = 'root';
$host = 'localhost';
$dbname = 'verify';
try {
$pdo = new PDO("mysql:host=$host;dbname=$dbname",$username,$password);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch(PDOException $e) {
return 'ERROR: ' . $e->getMessage();
}
return $pdo;
}
// attempts to delete existing entries and
// save verification code in DB with phone number
function updateDatabase($phoneNumber, $code)
{
$pdo = setupDatabase();
if (!is_a($pdo, 'PDO')) {
echo 'PDO is false';
return $pdo;
}
// Assuming US country code for example
$params = [ 'phoneNumber' => '1' . $phoneNumber ];
try {
$stmt = $pdo->prepare("DELETE FROM numbers WHERE phone_number=:phoneNumber");
$stmt->execute($params);
$params['code'] = $code;
$stmt = $pdo->prepare("INSERT INTO numbers (phone_number, verification_code) VALUES(:phoneNumber, :code)");
$stmt->execute($params);
} catch(PDOException $e) {
return 'ERROR: ' . $e->getMessage();
}
return $code;
}
function matchVerificationCode($phoneNumber, $code)
{
$pdo = setupDatabase();
if (!is_a($pdo, PDO::class)) {
echo 'ERROR: PDO is false';
return 'ERROR: PDO is false '.$pdo;
}
$params = [ 'phoneNumber' => $phoneNumber ];
try {
$stmt = $pdo->prepare("SELECT * FROM numbers WHERE phone_number=:phoneNumber");
$stmt->execute($params);
$result = $stmt->fetch();
$response = 'unverified';
if ($result['verification_code'] == $code) {
$stmt = $pdo->prepare("UPDATE numbers SET verified = 1 WHERE phone_number=:phoneNumber");
$stmt->execute($params);
$response = 'verified';
}
return $response;
} catch(PDOException $e) {
return 'ERROR: ' . $e->getMessage();
}
}
function statusIs($phoneNumber)
{
$pdo = setupDatabase();
if (!is_a($pdo, 'PDO')) {
echo 'PDO is false';
return $pdo;
}
$params = [ 'phoneNumber' => $phoneNumber ];
try {
$stmt = $pdo->prepare("SELECT * FROM numbers WHERE phone_number=:phoneNumber");
$stmt->execute($params);
$result = $stmt->fetch(PDO::FETCH_ASSOC);
if ($result['verified'] == 1) {
return 'verified';
}
return 'unverified';
} catch(PDOException $e) {
return 'ERROR: ' . $e->getMessage();
}
}