This repository has been archived by the owner on Sep 10, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sign-up.php
148 lines (135 loc) · 6.47 KB
/
sign-up.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
<?php
/**
* Basic page to handle user registration.
*/
require_once 'config.php'; ?>
<?php
$form_data = array(
'first_name' => null,
'last_name' => null,
'email' => null,
'password' => null,
'conf_password' => null,
);
/**
* Method to check if the user is already registered for the given email address
*
* @param String $email
* @param Mixed $connection
* @return Boolean
*/
function user_exists($email, $connection) {
$sql = "SELECT * FROM `users` WHERE `email` = '{$email}'";
$result = $connection->query($sql);
return $result->num_rows > 0 ? TRUE : FALSE;
}
/**
* Method to create a user in the dabase.
*
* @param Mixed $user
* @param Mixed $connection
*/
function create_user($user, $connection) {
$hashed_pw = md5($user['password']);
$sql = "INSERT INTO `users` (`email`, `password`, `first_name`, `last_name`) VALUES ("
. "'{$user['email']}', "
. "'{$hashed_pw}',"
. "'{$user['first_name']}',"
. "'{$user['last_name']}')";
$connection->query($sql);
}
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$form_error = array();
$form_data = array(
'first_name' => $_POST['first_name'],
'last_name' => $_POST['last_name'],
'email' => $_POST['email'],
'password' => $_POST['password'],
'conf_password' => $_POST['conf_password'],
);
if (user_exists($form_data['email'], $connection)) {
$form_error['email'] = "This email is already registered";
}
if ($form_data['password'] !== $form_data['conf_password']) {
$form_error['password'] = "Password doesn't match with Password confirmation";
}
if (count($form_error) === 0) {
create_user($form_data, $connection);
header('Location: reg-success.php');
}
}
?>
<?php require 'template/header.php'; ?>
<?php require 'template/navigation.php'; ?>
<div class="container">
<div class="row">
<div class="col-md-12">
<div class="panel panel-default">
<div class="panel-heading">
<h3>Sign Up</h3>
</div>
<div class="panel-body">
<form class="form-horizontal" action="sign-up.php" method="post">
<div class="form-group <?php echo (isset($form_error['first_name']) ? "has-error" : null); ?>">
<label for="first_name" class="col-sm-2 control-label">First Name</label>
<div class="col-sm-8">
<input type="text" class="form-control" id="first_name" name="first_name" placeholder="Enter your first name" value="<?php echo $form_data['first_name']; ?>" required>
<?php if (isset($form_error['first_name'])) { ?>
<span class="help-block">
<?php echo $form_error['first_name']; ?>
</span>
<?php } ?>
</div>
</div>
<div class="form-group <?php echo (isset($form_error['last_name']) ? "has-error" : null); ?>">
<label for="last_name" class="col-sm-2 control-label">Last Name</label>
<div class="col-sm-8">
<input type="text" class="form-control" id="last_name" name="last_name" placeholder="Enter your last name" value="<?php echo $form_data['last_name']; ?>" required>
<?php if (isset($form_error['last_name'])) { ?>
<span class="help-block">
<?php echo $form_error['last_name']; ?>
</span>
<?php } ?>
</div>
</div>
<div class="form-group <?php echo (isset($form_error['email']) ? "has-error" : null); ?>">
<label for="email" class="col-sm-2 control-label">E-Mail Address</label>
<div class="col-sm-8">
<input type="email" class="form-control" id="email" name="email" placeholder="Enter your email address" value="<?php echo $form_data['email']; ?>" required>
<?php if (isset($form_error['email'])) { ?>
<span class="help-block"><?php echo $form_error['email']; ?></span>
<?php } ?>
</div>
</div>
<div class="form-group <?php echo (isset($form_error['password']) ? "has-error" : null); ?>">
<label for="password" class="col-sm-2 control-label">Password</label>
<div class="col-sm-8">
<input type="password" class="form-control" id="password" name="password" placeholder="Enter your password" required>
<?php if (isset($form_error['password'])) { ?>
<span class="help-block">
<?php echo $form_error['password']; ?>
</span>
<?php } ?>
</div>
</div>
<div class="form-group <?php echo (isset($form_error['conf_password']) ? "has-error" : null); ?>">
<label for="conf_password" class="col-sm-2 control-label">Confirm Password</label>
<div class="col-sm-8">
<input type="password" class="form-control" id="conf_password" name="conf_password" placeholder="Enter your password again" required>
<?php if (isset($form_error['conf_password'])) { ?>
<span class="help-block"></span>
<?php } ?>
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-8">
<button type="submit" class="btn btn-info">Sign Up</button>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
<?php require 'template/footer.php'; ?>