1<?php
2session_start();
3
4// initializing variables
5$username = "";
6$email = "";
7$errors = array();
8
9// connect to the database
10$db = mysqli_connect('localhost', 'root', '', 'registration');
11
12// REGISTER USER
13if (isset($_POST['reg_user'])) {
14 // receive all input values from the form
15 $username = mysqli_real_escape_string($db, $_POST['username']);
16 $email = mysqli_real_escape_string($db, $_POST['email']);
17 $password_1 = mysqli_real_escape_string($db, $_POST['password_1']);
18 $password_2 = mysqli_real_escape_string($db, $_POST['password_2']);
19
20 // form validation: ensure that the form is correctly filled ...
21 // by adding (array_push()) corresponding error unto $errors array
22 if (empty($username)) { array_push($errors, "Username is required"); }
23 if (empty($email)) { array_push($errors, "Email is required"); }
24 if (empty($password_1)) { array_push($errors, "Password is required"); }
25 if ($password_1 != $password_2) {
26 array_push($errors, "The two passwords do not match");
27 }
28
29 // first check the database to make sure
30 // a user does not already exist with the same username and/or email
31 $user_check_query = "SELECT * FROM users WHERE username='$username' OR email='$email' LIMIT 1";
32 $result = mysqli_query($db, $user_check_query);
33 $user = mysqli_fetch_assoc($result);
34
35 if ($user) { // if user exists
36 if ($user['username'] === $username) {
37 array_push($errors, "Username already exists");
38 }
39
40 if ($user['email'] === $email) {
41 array_push($errors, "email already exists");
42 }
43 }
44
45 // Finally, register user if there are no errors in the form
46 if (count($errors) == 0) {
47 $password = md5($password_1);//encrypt the password before saving in the database
48
49 $query = "INSERT INTO users (username, email, password)
50 VALUES('$username', '$email', '$password')";
51 mysqli_query($db, $query);
52 $_SESSION['username'] = $username;
53 $_SESSION['success'] = "You are now logged in";
54 header('location: index.php');
55 }
56}
57
58// ...
59
1<html>
2<head>
3<title>PHP User Registration Form</title>
4<link href="./css/style.css" rel="stylesheet" type="text/css" />
5</head>
6<body>
7 <form name="frmRegistration" method="post" action="">
8 <div class="demo-table">
9 <div class="form-head">Sign Up</div>
10
11<?php
12if (! empty($errorMessage) && is_array($errorMessage)) {
13 ?>
14 <div class="error-message">
15 <?php
16 foreach($errorMessage as $message) {
17 echo $message . "<br/>";
18 }
19 ?>
20 </div>
21<?php
22}
23?>
24 <div class="field-column">
25 <label>Username</label>
26 <div>
27 <input type="text" class="demo-input-box"
28 name="userName"
29 value="<?php if(isset($_POST['userName'])) echo $_POST['userName']; ?>">
30 </div>
31 </div>
32
33 <div class="field-column">
34 <label>Password</label>
35 <div><input type="password" class="demo-input-box"
36 name="password" value=""></div>
37 </div>
38 <div class="field-column">
39 <label>Confirm Password</label>
40 <div>
41 <input type="password" class="demo-input-box"
42 name="confirm_password" value="">
43 </div>
44 </div>
45 <div class="field-column">
46 <label>Display Name</label>
47 <div>
48 <input type="text" class="demo-input-box"
49 name="firstName"
50 value="<?php if(isset($_POST['firstName'])) echo $_POST['firstName']; ?>">
51 </div>
52
53 </div>
54 <div class="field-column">
55 <label>Email</label>
56 <div>
57 <input type="text" class="demo-input-box"
58 name="userEmail"
59 value="<?php if(isset($_POST['userEmail'])) echo $_POST['userEmail']; ?>">
60 </div>
61 </div>
62 <div class="field-column">
63 <div class="terms">
64 <input type="checkbox" name="terms"> I accept terms
65 and conditions
66 </div>
67 <div>
68 <input type="submit"
69 name="register-user" value="Register"
70 class="btnRegister">
71 </div>
72 </div>
73 </div>
74 </form>
75</body>
76</html>