Now that we have our form in JavaScript code completed we need to create the HP script that validates
our form and inputs it into the database we've created earlier.
We also need our HP script to encrypt our plain text password prior to storing it into the database
so to get started.
Create a new file and save it is add user dot P. HP.
Make sure to save it in the perfect cup directory
the HP script will start with a session start.
Function
this will make sure that the user is logged into their account after registration will store their user
I.D. first name and last name using session variables later on we'll create a logout button that will
terminate the session.
So you'll see this in action later on.
Next we need to create a connection to our database so that we can insert a new record.
If the form data passes validation so this syntax should look familiar.
We've just created a database connection and we've set the parameters to local host route user.
Password and the perfect cup database has been selected.
Now we need to create the variables that will store the first name last name email and password from
our form
since we use the post method in our ajax call.
We'll set the method to post in our HP script as well.
You can see that we've used the my rescue while I real escape string function to escape any special
characters that the user might have typed into the form inputs.
This keeps your web page secure by preventing Eskew all injection
so the values that have been entered have been stored into these variables f name l name email and password.
Next we'll set up our validation so here we have an if statement and I haven't closed the if statement
yet and we'll explain that in just a second.
The first three statements check to make sure that the F name l name and email data meets a minimum
number of characters.
Each check will output a different error message.
If the validation fails for example if f name is less than two characters the error message will be
f name and if you recall on a registered IP HP page we had our javascript interpret these error codes
and display an error message to the user depending on the error code
so the first second and third check for the length using the string length function.
This line here checks to make sure that the email format is correct.
P HP has a built in function for this.
It's called filter.
Var and if the format is incorrect the error code we've set is e format.
So this is the email variable that stores the email address the user input it.
It will be checked for the correct format and if the check fails the format will be output.
The last statement checks to make sure that the password input contains more than four characters.
So essentially we want the password to be a minimum of four characters just for security purposes.
And if that check fails we've set the output to P. short if the form data passes all validation.
The next step in the process is to encrypt the password and that's why we have an L statement so if
all this passes and there's no error codes the next step is to encrypt our password
the password encryption is done using the password hash function that's built into BHP.
This function will hash a plain text password into a 60 character string inside this function.
We specified the password to hash and the encryption method.
So we want to hash the data contained in the password variable so that's the text at the user's input
it into the password field.
We also need to specify the encryption method and the be crypto method is is most widely used and considered
one of the best methods for hashing.
So that's why we have the password B crip statement here.
We can also specify a cost the higher the cost value the stronger the hash.
Note that encrypting with very high cost values slows down the Web page and is more demanding on the
server and we've set it to twelve so a higher cost value would be a 13 or 14 and any number below that
would be considered a low cost.
The hash password is stored in a new variable called s password.
We'll show you how to unpack the password when we create the log in page.
So for now don't worry about on hashing we're only concerned concerned with hashing the password after
validation and password hashing.
We execute a query statement and this query statement is executed on the members table.
So we select records from members where email is equal to the email variable.
This is done to make sure that the registrants email does not already exist exist in the system.
If the user's email does exist we're going to be outputting an error code false and you'll see that
in just a moment.
So here's our if statement that's what that's what it looks like.
This comment just trailed off.
I'm going to put it back in the right place.
OK so this is what the if statement looks like.
If the number of rows resulting from our query statement is less than 1 that means that the user's email
does not exist in the system.
And if that's the case we're going to insert a row into our database and into the member's table which
contains the first name last name email and password that the user input it.
So these are the the column names.
These are the values that are going to be inserted into these columns if the row insertion was successful.
We create a three session variables that store the record I.D. and the idea will be stored in a session
variable called log in.
We also store the user's first name and last name and this way the user will be logged into their account
and they can access the member only blog until they decide to log out.
We've also set the p HP script to output this access code is true.
Once the record is inputted so if the records input it their account has been created successfully and
our javascript code on the registered dog HP page is then going to redirect them to indexed IP HP which
is the home page and that's going to happen after the account process message appears
so user L statement the L statement again is if the email does exist exist in the system.
So if the user is already in the system we echo the error code false and on a registered IP HP page
you can see here that if HP AML is equal to False the alert will be email address already exists in
the system
and that's the complete script.
So what we can do now is go ahead and test our script to make sure that it does work and that the record
does input into the database.
I'm going to bring back p p my admin and I'm just gonna click on our members table.
So right now we can see that there's no records in sight.
Our table
is going to go back to the Web page and here's the registered IP HP page
you'll just refresh it.
So first of all if I click submit you can see that we get the first name is required so that tells us
that the validation is working.
So the javascript and Ajax code is successfully communicating with our HP script which is a good sign
and so I'll just enter a test record
and I'll click submit so account process and it redirects to the index page so that tells us that all
this was most likely successful.
And when you refresh the table and there is the record we just created with the hashed password
and in the next lesson we'll create our log in page so stay tuned for that.