Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Preview
Start a free Courses trial
to watch this video
Any authentication system that you need should have a way for a user to be registered into the system. Using the templates from the previous stage, we will create our registration system. This will involve 2 more files, A registration form and a way to process the registration.
[MUSIC]
0:01
We have a pretty simple system now that
lets guests of our site submit books and
0:04
vote on them.
0:09
This is a great start but let's make
it even better with authentication.
0:10
I want to set the site up so
0:15
that a user has to be logged in to be
able to submit a book and to vote on it.
0:16
The first requirement here is to allow
user to register for a book voting site.
0:21
I've already provided
the registration form itself.
0:27
So we can focus on the creation
of the registration procedure.
0:30
To understand the data that we have
available during registration,
0:34
let's take a look quickly
at the registration form.
0:37
We are currently asking for three items,
0:42
the email of the user, a password and
for the user to confirm their password.
0:45
This form can be extended to
require any other information
0:51
that you would like to
associate with the user.
0:54
Our first step is to
create the procedure for
0:57
a registration that
the form is submitted to.
0:59
Create a new file in
the procedures folder.
1:04
Name this doRegister.php.
1:10
We'll start this file the same
as all the other procedures,
1:19
by including the bootstrap file.
1:22
Next we want to capture the variables
we need from the request object.
1:36
Our password, confirm password, and email.
1:56
We'll use these variables
throughout the rest of this script.
2:14
We're now ready to begin the checks before
inserting a user into the database.
2:18
When writing your registration script,
I always suggest that you start with
2:23
the checks that do not require
a database connection first.
2:27
So for our first one,
2:30
let's compare to make sure
the passwords were typed the same.
2:31
If password does not equal,
ConfirmPassword.
2:38
If they're not the same,
2:48
we want to redirect them back
to the registration page.
2:49
If we pass this first check, the next
thing we should do is check to see if
3:02
a user already exists with that email.
3:07
We're going to create a new helper
function called findUserByEmail.
3:10
So open the functions file.
3:14
FindUserByEmail.
3:23
And we'll pass the email.
3:29
We start with our global db and
then our try catch block.
3:35
We're going to rethrow this error.
3:57
For our query we're going to select
4:06
all from users WHERE email = the email.
4:12
Prepare a query, And bind our email.
4:27
Execute the statement and
then return a fetch.
4:51
We want to fetch an associative array.
5:04
This function will either return
an empty array if no user is found, or
5:10
an associative array of the user
from the database if one is found.
5:15
We'll use this for our next check.
5:20
User = findUserByEmail.
5:26
And we'll pass the email.
5:31
We want to make sure that there is no user
in this system with that email address.
5:34
So we expect an empty user array.
5:38
So if not empty, The user,
5:41
Then will redirect the user
back to the registration page.
5:51
Before we can finish
off this registration,
6:01
we need to talk about passwords and
security.
6:04
You need to sign up for Treehouse in order to download course files.
Sign up