-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsignup.php
executable file
·155 lines (127 loc) · 5.47 KB
/
signup.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
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
<?php
session_start();
date_default_timezone_set('America/Vancouver');
$userName = $_SESSION["firstName"];
$userId = $_SESSION["userId"];
$userEmail = $_SESSION["email"];
$validation_error;
$global_values;
require __DIR__ . '/validation/validate_date.php';
require __DIR__ . '/validation/validate_gender.php';
require __DIR__ . '/validation/validate_email.php';
require __DIR__ . '/validation/validate_password.php';
require __DIR__ . '/validation/validate_string.php';
require __DIR__ . '/model/util/connect_db.php';
require __DIR__ . '/util/mail/send_welcome_mail.php';
require __DIR__ . '/validation/get_validation_message.php';
require __DIR__ . '/util/form/remember_input_value.php';
require __DIR__ . '/config.php';
require_once __DIR__ . '/component/header.php';
require_once __DIR__ . '/component/head.php';
require_once __DIR__ . '/component/footer.php';
if($_SERVER["REQUEST_METHOD"] == "POST"){
global $validation_error;
$isValidName = validate_str("firstName", "first name");
$isValidLast = validate_str("lastName", "last name");
$isValidBirth = validate_birthday();
$isValidEmail = validate_email();
$isValidGender = validate_gender();
$isValidPassword = validate_password();
$isValidPassConf = isset($_POST["passwordConfirm"]) && $_POST["passwordConfirm"] == $_POST["password"]? true: false;
if(!$isValidPassConf){
$validation_error["passwordConfirm"] = "passwords does not match!";
}
if ($isValidName &&
$isValidLast &&
$isValidBirth &&
$isValidEmail &&
$isValidGender &&
$isValidPassword &&
$isValidPassConf) {
// TODO
// connect to DB
$conn = connect_db();
$hashedPass = password_hash($_POST["password"], PASSWORD_DEFAULT);
$stmt = $conn->prepare("INSERT INTO Users (firstName, lastName, email, birthday, gender, password )
VALUES (?,?,?,?,?,?)");
$stmt->bind_param("ssssss", $_POST["firstName"],$_POST["lastName"],$_POST["email"],$_POST["birthday"],$_POST["gender"], $hashedPass);
if ($stmt->execute()) {
// Insertion was successful
$sql = "SELECT * FROM Users WHERE email=?";
$stmt = $conn->prepare($sql);
$stmt->bind_param("s", $_POST["email"]);
$stmt->execute();
$result = $stmt->get_result();
$row = $result->fetch_assoc();
$firstName = $row["firstName"];
session_start();
$_SESSION["userId"] = $row["userID"];
$_SESSION["firstName"] = $row["firstName"];
$_SESSION["email"] = $row["email"];
header("Location: index.php");
exit();
} else {
if($conn->errno == 1062){
$validation_error["general"] = "email already exists! please use another email or login.";
} else {
$validation_error["general"] = "Something went wrong! Please try again later.";
}
}
// log user in
// redirect to home page
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<?= render_head("FT - Sign up"); ?>
</head>
<body>
<div class="a-index-wrapper">
<?= render_header($userName); ?>
<main>
<form method="POST" action="signup.php" class="sign-up">
<h2>Sign up</h2>
<div>
<label for="firstName">First Name</label>
<input type="text" name="firstName" id="firstName" <?= remember_input_value("firstName")?> />
<?= get_validation_message("firstName") ?>
<label for="lastName">Last Name</label>
<input type="text" name="lastName" id="lastName" <?= remember_input_value("lastName")?> />
<?= get_validation_message("lastName") ?>
<label for="birthday">Birth Date</label>
<input type="date" name="birthday" id="birthday" <?= remember_input_value("birthday")?> />
<?= get_validation_message("birthday") ?>
<label for="gender">Gender</label>
<select name="gender" id="gender">
<option value="F" <?= isset($_POST["gender"]) && $_POST["gender"]=="F" ? "selected": ""; ?>>
Female
</option>
<option value="M" <?= isset($_POST["gender"]) && $_POST["gender"]=="M"? "selected": ""; ?>>Male
</option>
<option value="O" <?= isset($_POST["gender"]) && $_POST["gender"]=="O"? "selected": ""; ?>>
Others
</option>
</select>
<?= get_validation_message("gender") ?>
</div>
<div>
<label for="email">Email</label>
<input type="email" name="email" id="email" <?= remember_input_value("email")?> />
<?= get_validation_message("email") ?>
<label for="password">Password</label>
<input type="password" name="password" id="password" />
<?= get_validation_message("password") ?>
<label for="passwordConfirm">Confirm Password</label>
<input type="password" name="passwordConfirm" id="passwordConfirm" />
<?= get_validation_message("passwordConfirm") ?>
</div>
<button type="submit">Submit</button>
<p><?php echo isset($validation_error["general"])? $validation_error["general"]: ""?></p>
</form>
</main>
<?= render_footer(); ?>
</div>
</body>
</html>