Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Registration System5:01 with Alena Holligan
Any authentication system that you need should have a way for a user to be registered into the system. This will involve a registration form and a way to process the registration.
I've included the code for the basic system of the website, 0:00 which allows guests of our site to submit books and to vote on them. 0:03 Although the basic functionality is working, 0:08 let's make it even better with authentication. 0:10 I want to set the site up, so 0:14 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 a user to register for our book voting site. 0:22 I've already provided the registration form itself. 0:28 So we can focus on the creation of the registration procedure. 0:31 We are currently asking for three items. 0:36 A username, a password, and for the user to confirm their password. 0:38 This form can be extended to require any 0:44 other information that you would like to have associated with the user. 0:47 Our first step is to create the procedure for 0:50 registration that this form will be submitted to. 0:53 In the procedure's folder, create a new file named doRegister. 0:57 We'll start this file the same as all other procedures, 1:07 require_once, and require our bootstrap file. 1:13 We'll go up one level, /inc/bootstrap.php. 1:20 Next, we want to capture the variables that we need from the request object. 1:27 $username = request. 1:31 Get('username'). 1:39 $password = request get('password'). 1:44 And we can copy this for 1:51 $confirmPassword and 1:55 get('confirm_password'). 1:59 We'll use these variables through the rest of this script. 2:08 We are now ready to begin the checks before 2:12 inserting the user into the database. 2:15 When writing your registration scripts, I always suggest that you start 2:17 with the checks that do not require database connection. 2:22 So, for our first check, 2:26 let's compare to make sure the passwords were typed in the same. 2:27 If the $password does not equal the $confirmPassword. 2:31 If they don't match, then we're going to add a flash message. 2:45 We use $session, 2:51 getFlashBag, add, error, 2:55 passwords do not match. 3:02 And then we'll redirect 3:08 to register.php. 3:13 If we pass this first check, 3:18 the next thing we should do is to check to see if that username already exists. 3:20 We have a function for the database connection in our functions_users file. 3:25 The function is findUserByUsername. 3:31 This function will return an empty array if no username is found or 3:36 if a user is found and associative array of the user details from the database. 3:42 We'll use this for our next check. 3:48 We set $user equal to findUserByUsername. 3:54 And we pass our $username. 4:03 We want to make sure that there is no user in the system with that username, 4:06 so we expect an empty user array. 4:12 So we'll check if not empty, $user. 4:15 If the user array is not empty, we add a flash error message. 4:23 $Session getFlashBag, 4:28 add error, and we'll say, 4:34 user already exists. 4:41 And then we'll redirect to register.php. 4:46 Before we can finish off this registration, 4:55 we need to talk about passwords and security. 4:58
You need to sign up for Treehouse in order to download course files.Sign up