-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsignup.php
54 lines (38 loc) · 1.58 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
<?php
//Check that a name is entered
if (empty($_POST["name"])) {
die("name requrired");
}
//Checks that the email is a valid email format
if ( ! filter_var($_POST["email"], FILTER_VALIDATE_EMAIL)) {
die("Email must be valid");
}
//Checks that a password has been entered and that it is at least 6 characters long
if (strlen($_POST["password"]) < 6) {
die("Password must be 6 characters or more");
}
//Checks that both entered passwords match
if ($_POST["password"] !== $_POST["confirm_password"]) {
die("Passwords do not match");
}
//Creates a hash of the submitted password
$hashed_password = password_hash($_POST["password"], PASSWORD_DEFAULT);
//Makes this script require the database_connect script. Ensures a connection with the database.
$sql_var = require __DIR__ . "/database_connect.php";
//MySQL statement for inserting user info into the database
$sql_statement = "INSERT INTO user (name, email, hash_password) VALUES (?,?,?)";
//Initialing SQL statement
$prepared_statement = $sql_var->stmt_init();
//Preparing SQL statement for execution
$prepared_statement->prepare($sql_statement);
//Binding values to placehoders in $sql_statement
$prepared_statement->bind_param("sss", $_POST["name"], $_POST["email"], $hashed_password);
//Inserts the values into the database.
//Checks if the email is already registered -----------------------> this does not work - fatal error if email already registered.
if ($prepared_statement->execute()) {
//Redirects once completed
header("Location: successful_signup.html");
exit;
} else {
echo "Email already registered";
}