Misc code cleanups and comment.
[Project_webapp.git] / includes / formregister.php
1 <?php
2 $form_name = filter_input(INPUT_POST, "name", FILTER_SANITIZE_STRING);
3 $form_firstname = filter_input(INPUT_POST, "firstname", FILTER_SANITIZE_STRING);
4 $form_numstreet = filter_input(INPUT_POST, "numstreet", FILTER_VALIDATE_INT);
5 $form_street = filter_input(INPUT_POST, "street", FILTER_SANITIZE_STRING);
6 $form_postalcode = filter_input(INPUT_POST, "postalcode", FILTER_VALIDATE_INT);
7 $form_city = filter_input(INPUT_POST, "city", FILTER_SANITIZE_STRING);
8 $form_email = filter_input(INPUT_POST, "email", FILTER_VALIDATE_EMAIL);
9 $form_password = filter_input(INPUT_POST, "password", FILTER_SANITIZE_STRING);
10 $form_confirmpassword = filter_input(INPUT_POST, "confirmpassword", FILTER_SANITIZE_STRING);
11
12 $input_failure = false;
13
14 if (!$form_numstreet) {
15 echo "The street number is not valid. <br>" ;
16 $input_failure = true;
17 }
18 if (!$form_postalcode) {
19 echo "The postal code is not valid. <br>";
20 $input_failure = true;
21 }
22 if (!$form_email) {
23 echo "The email is not valid. <br>";
24 $input_failure = true;
25 }
26 if (strcmp($form_password, $form_confirmpassword) !== 0) {
27 echo "Password do not match. <br>";
28 $input_failure = true;
29 }
30
31 if (!empty($form_name) && !empty($form_firstname) && !empty($form_numstreet) && !empty($form_street) &&
32 !empty($form_postalcode) && !empty($form_city) && !empty($form_email) && !empty($form_password) &&
33 !$input_failure) {
34 if (!chk_account($form_email)) {
35 global $connection;
36 $hashed_password = password_hash($form_password, PASSWORD_DEFAULT);
37 $sql_pquery = "insert into CLIENTS (NomCl, PrenomCl, EmailCl, PasswordCl, NumRueCl, NomRueCl, CodePosteCl, VilleCl)
38 values (?, ?, ?, ?, ?, ?, ?, ?)";
39 $connection->prepare_query($sql_pquery);
40 $connection->prepared_query_bind_param("ssssisis", array($form_name, $form_firstname, $form_email, $hashed_password, $form_numstreet, $form_street, $form_postalcode, $form_city));
41 $connection->run_prepared_query();
42 $connection->close_prepared_query();
43 echo "You've successfully registered, you will be redirected to the login form in 3 seconds. <br>";
44 redirect("index.php?page=login", 3);
45 } else {
46 echo "You're already registered, you will be redirected to the login form in 3 seconds. <br>";
47 redirect("index.php?page=login", 3);
48 }
49 } else {
50 echo "There's a required non filled field or the input in a field do not match the required pattern. <br>";
51 echo "<a href=\"javascript:history.go(-1)\">Retour</a>";
52 }
53 ?>