-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreateAccountHandler.php
76 lines (62 loc) · 1.65 KB
/
createAccountHandler.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
<?php
$_GET['error']=TRUE;
$errors="";
$values="";
$isEmailValid = true;
$isPasswordValid = true;
$isConfirmValid = true;
//check if email field is empty and check if it's in the correct format
if ( empty($_POST["email"]) ){
$isEmailValid = false;
$errors.='email=required&';
}else{
if(!filter_var($_POST["email"], FILTER_VALIDATE_EMAIL)){
$isEmailValid = false;
$errors.='email=invalid&';
}
}
if ( $_POST["pwd"] == null){
$isPasswordValid = false;
$errors.='pwd=invalid&';
}
//confirm must not be null and must match the entered password
if ( empty($_POST["confirm"]) ){
$isConfirmValid = false;
$errors.='confirm=required&';
}
if ( $_POST["confirm"] !== $_POST["pwd"] ){
$isConfirmValid = false;
$errors.='confirm=invalid&';
}
if($isEmailValid and $isPasswordValid and $isConfirmValid)
{
try {
$db = new PDO ("sqlite:users_DB/users.db");
$db -> setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$email = $_POST["email"];
$pwd = password_hash($_POST["pwd"], PASSWORD_DEFAULT);
$stmt = $db->prepare("insert into users (email, password) VALUES (:email, :pwd)");
$stmt->bindValue(':email', $email);
$stmt->bindValue(':pwd', $pwd);
$stmt->execute();
$db = null;
header('Location: http://10.250.94.60/~ubuntu/CRSFF/pages/login_page.php?');
echo "Account created!";
}catch (PDOException $e) {
die("Exception : " .$e->getMessage());
}
}
if($isEmailValid)
{
$values.="email=" . $_POST["email"] . "&";
}
if($isPasswordValid)
{
$values.="pwd=" . $_POST["pwd"] . "&";
}
if($isConfirmValid)
{
$values.="confirm=" . $_POST["confirm"] . "&";
}
header('Location: http://10.250.94.60/~ubuntu/CRSFF/pages/create_account.php?' . $errors . $values);
?>