-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreateuser.php
157 lines (142 loc) · 6.27 KB
/
createuser.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
156
157
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Book Listing: Sign Up</title>
<?php include_once 'dependencies.php' ?>
</head>
<body>
<div class="ui container">
<?php
include_once 'includes/header.inc.php'; ?>
</div>
<?php
include_once 'includes/nav.inc.php';
$show_success = false;
$show_error = false;
$user_exists = false;
function format($data)
{
$data = trim($data);
$data = addslashes($data);
$data = htmlspecialchars($data);
return $data;
}
function getprevdata($info)
{
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (empty($_POST[$info])) return null;
else return $_POST[$info];
} else return null;
}
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (empty($_POST["username"]) || empty($_POST["firstname"]) || empty($_POST["lastname"]) ||
empty($_POST["email"]) || empty($_POST["password"]) || empty($_POST["confirmpassword"])) {
$show_error = true;
} else {
$user = new User();
$user->checkusername(format($_POST["username"]));
if ($user->id) {
//cant register when the user name already exists.
$show_error = true;
$user_exists = true;
$show_success = false;
} else {
//encrypt the password before storing
$password = password_hash($_POST["password"], PASSWORD_DEFAULT);
$user->newuser(format($_POST["username"]), format($_POST["firstname"]), format($_POST["lastname"]),
format($_POST["email"]), format($_POST["gender"]), format($_POST["dob"]),
$password, $_POST["picture"]);
$user->store();
$show_success = true;
}
}
}
?>
<div class="ui container">
<div class="ui grid">
<section class="wide column">
<div class="ui large breadcrumb">
<a href="index.php" class="section">Home</a>
<i class="right arrow icon divider"></i>
<div class="active section">Create new User</div>
</div>
</section>
</div>
<section class="ui segment custom-form">
<form class="ui <?php if ($show_success) echo 'success'; elseif ($show_error) echo 'error' ?> form register-form"
method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>">
<h4 class="ui dividing header">Register</h4>
<div class="fields">
<div class="eight wide field required">
<label>Username</label>
<input type="text" name="username" maxlength="256" placeholder="Pick a username"
value="<?php echo(getprevdata('username')) ?>">
</div>
<div class="eight wide field required">
<label>Email Address</label>
<input type="email" name="email" maxlength="256" placeholder="A valid email address"
value="<?php echo(getprevdata('email')) ?>">
</div>
</div>
<div class="fields">
<div class="four wide field required">
<label>First Name</label>
<input type="text" name="firstname" maxlength="256" placeholder="Your first name"
value="<?php echo(getprevdata('firstname')) ?>">
</div>
<div class="four wide field required">
<label>Last Name</label>
<input type="text" name="lastname" maxlength="256" placeholder="Your last name"
value="<?php echo(getprevdata('lastname')) ?>">
</div>
<div class="four wide field">
<label>Gender</label>
<select id="" name="gender" class="ui fluid dropdown">
<option value="1">Male</option>
<option value="2">Female</option>
</select>
</div>
<div class="four wide field">
<label>Date of Birth</label>
<input type="date" name="dob" placeholder="Date of Birth"
value="<?php echo(getprevdata('dob')) ?>">
</div>
</div>
<div class="fields">
<div class="four wide field required">
<label>Password</label>
<input type="password" name="password" placeholder="Password"
value="<?php echo(getprevdata('password')) ?>">
</div>
<div class="four wide field required">
<label>Confirm Password</label>
<input type="password" name="confirmpassword" placeholder="Confirm Password"
value="<?php echo(getprevdata('confirmpassword')) ?>">
</div>
<div class="eight wide field">
<label>Profile Image</label>
<input type="text" name="picture" placeholder="Image URL or Link"
value="<?php echo(getprevdata('picture')) ?>">
</div>
</div>
<div class="ui error message">
<div class="header">Error</div>
<?php if ($user_exists)
echo '<p>Registration failed, account already exists, please <a href="login.php">Log-in.</a></p>';
else
echo '<p>Registration failed, please make sure that you have inputed all the information correctly.</p>'; ?>
</div>
<div class="ui success message <?php if (!$show_success) echo 'hidden'; ?>">
<div class="header">Success</div>
<p>You are successfully registered, please <a href="login.php">Log-in</a> to continue to the site. </p>
</div>
<p>Note: <i>Fields marked with (*) are required fields.</i></p>
<button type="submit" class="ui button" tabindex="0">Register</button>
</form>
</section>
</div>
<?php include_once 'includes/footer.inc.php' ?>
</body>
<script src="javascript/script.js"></script>
</html>