PHP Email Verification And Validation Tutorial - Registration & Login Form

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
recently I have got a lot of comments on how to send an email with email confirmation link where a person needs to verify his email before he is able to make a next step on our website so let me show you how to do it hey what's up guys Senaid here from Codingpassiveincome.com the place where I help others to become a web developer much easy and faster than they will do in on the own so if that is something that interests you consider subscribing so now I'm here at my PHP store and my goal for this video is to first create a registration form and after that we will send an email with email confirmation link and that person will not be able to log into our website before he is a he verify his email so in order to create a restoration form we need to stop HTML document ok I will set title to the register and then I'm going to include the bootstrap library download and I will see the link from the latest version of booster ok so now inside the body we are going to have one container and then inside this container we are going to have one row and I went just to put everything to the center and in this one row we'll have one column so I'll set the class on set column md6 and let's align everything to the center and maybe let's put some offset let's say three so that this column with six is in the center because there is 12 columns and maybe let's put margin top to the 100 pixels and that's read it so I will just add a top include mine the PNG said to new heights and then let's create some form sub use method post I will use action the same file and then we are going to us to have few basic inputs so let's say name will be name I'll set placeholder to the name new live and now I'm just going to duplicate this the next part will be email I will set type to the email and placeholder image so I'll duplicate again and I will set this one to the password type password and then placeholder restaurant and let's put one conformation password this is just a good practice okay and that's a conflict best but okay and now we'll just create another input I will set type submit submit and value register okay so let's see what we have got at the moment I will just upload everything to my server okay say here PHP email confirmation register.PHP okay and here's how it looks like maybe it'll be better if for each input we will set class to the form control from the bootstrap and maybe what this fellow said it's button primary so let's see now so yeah this form is looking a lot better so now this is does the basic design and we do not have any functionality and in order to make one we will need to have a database and some table so I have already created a new database with this name so does not were to create a new table I will set name to the users let's use and shine DB collation I will use utf-8 unicode see okay so first one I will set ID type integer Auto increment the next one let's see name bar chart let's say 50 and let's add any good egg see again okay duplicate this email it can be the same password and for this one I will use 255 okay and we will need for latest news is confirmed American say is email confirming it can be time integer 0 or 1 by default will be 0 and then we can use one column it will be a token let's say water and I will set it to 10 and I could see again so which means that we are going to create one token for our email confirmation so that we are sure that the right person has clicked on that link and also we are going to make sure that email is confirmed before the person is able to you make any further action on our website and pretty much that's it okay I'll just hit save and now our date this has been our table has been created okay so now for this registration as you can see I'm not going to use jQuery anything I want to make sure that this tutorial will be understand by those that are just starting out and don't know anything about jQuery okay so now we are going to check if someone is pressed on this button so take I will say F is it post submit which means if someone has pressed on this button okay this one we are just going to accept all those informations so I will say here our name is post name I'll duplicate is you have email duplicate again I'll have password okay duplicate again and we are going to have confirm password and that's it so now we can make test and verify if an email is correct if name is not empty etc etc but really that is not a rule of this reader I have another tutorial my channel we are talking about how to verify all those inputs so for this one I will just verify if a password and cPassword are be equal and maybe we can just say if name equal empty or email = " " poor passport different than conference password okay we will say that somewhere evil message will be please check your inputs okay else LT is okay so maybe let's define here one evil message and here we will just display that message if it's not everything okay so see if message different on empty echo message and two new lines okay so here if everything is okay we should put all the details inside our table but before that I'm going to make a new connection to our MySQL database I will lose mysqli I'll see our host is localhost as username is research email see that's my real name password is test 1 2 3 and the name of my database is research PHP email confirmation okay so now when we have this connection we are able to escape the inputs that's very important so maybe I should tell it like this we want to escape all of those and that's it okay so now we can check if the person with this email already exists in the database so we can say well a connection query select for example ID from users where email equal email okay we will see if num_rows > 0 you can say that message now here's the crucial part now we need to insert all those information inside the table but also we need to create one token that we will use for a person and also we need to send an email to that person so that he is able to verify his email so in order to create one token I will show how to simply we can do it so I'll maybe use this website PHP tester so let's say that we are going to create our token from our list of characters so maybe let's first write all the with the small letters like this and then I'm going to use all those just with the caps lock on so maybe let's just write like this this is just a very simple way to create so now let's also add the numbers okay and then we can also add some special characters so maybe I will do it like this okay and that's enough okay so now the next thing that I want to do is let's just use this one function from PHP it's called the string shuffle and let me show what it will happen so basically it will just mix all of those characters so each time it will be different okay as you can see here so I'll say that our strength is equal dad and after that I'm going just to echo for example I will use function substring and I will specify the first parameter is our string and then first parameter is the starting position from those things where we will start so I will use 0 which means start from the first character and then the next number is how many characters we want to make I will say that ok and let's see what will happen now so yeah look at this so this is now a very random token of course there are some other and better ways on how to do this but I think that this one is probably the one of the easiest way for the beginners to understand this okay so that enough maybe I will just remove those two so that we don't have any problems later okay so those should make any problem in the URL okay so this is how our token will look like okay so let me just now copy this I will paste it and say that our string is equal this okay maybe let's change this string sign to them okay okay and now let's say this to the database so I will say connection query and I will say insert into users and then we are going to store name email password is email confirmed and token okay and I will say here values so we have our name we have our email and now our password I will show later is email confirm I will set to the zero and our token is and now for the password well it will be better to create a hash of the password that user has entered so I will use something that is available available from the PHP version 5.6 so say hashed password is equal and then I will say password hash function I was specified the password that a person has entered and now we need to specify the algorithm type that we need to use so i will use password default you can also use password bcrypt here okay the only difference is that if we use password default we need to remember that in the future this algorithm type can be changed and that our hash can be longer than 60 characters and if we use bcrypt it will always be 60 characters so let's say I will use B crib and I will Stuart here okay and now we need to put some message okay you have been registered please verify your email and now we need here to send an email to this person so that he is able to verify his email so in order for us to send an email we can use a native mail PHP function but I want to show you how to use PHP mail alert and on that way you will always send an email and probably in most cases it won't go to the spam folder so first we need to download PHP mailer so I will go to the google and type PHP mailer there the first link okay and down here I will go to the phone or download and hit download I'm going to store it to this folder here I will extract okay delete this library and then from here I can only get those files we don't need anything else or copy/paste I will delete this and let's maybe change the name to the PHP mail okay so now in this latest version of PHP mailer we need to use a namespace from the PHP mailer so you can do it like this like this and then we need to also include see PHP mailer file okay and now here if we did everything as we should maybe we should know this top here and he wants me having food let's create a new worry so I'll see mail equal new PHP mailer and now I will set mail and then I'll says set from so this is who is sending an email so I will just say in this example this is codingpassiveincome.com okay now we need to set where we are sending an email so this is an email from the customer from the owl person who is registering and then we can also set the name of that person okay we need to set a subject so I will say please verify email and then we need to create to set if is HTML or not I will set to the true okay and then we need to create a body of this email so I will make a very simple so maybe let's say please click and the link below I will set to new lines and then we are going to create one link so let's say that our verification link will go to the http://codingpassiveincome.com PHP email confirmation okay and now I will just have one file let's say confirm.php and to that file I'm going to pass through get method two variables first one will be email that is an email of the person and the second one will be token so I will say token okay and that's it and maybe I'll see quick here okay and now all we need to do is just to send an email so does he if mail send we will say this okay else message something wrong happening please try again so there's some problem okay so there is quite a lot of the code so let's try to test so we need to upload those two okay and now let's refresh and try to test so I'll see okay password test one two three test one two three register okay so what is the problem cool - undefined - function password - so it looks that the server version on my server at the moment for this folder is not five point six so let me just update it through the control panel so now I have updated my server version and we can try again and I didn't show this part how I did that because it all depends from the hosting to the hosting so the best way for you will be to use a Google to see how to update the server version and as I said this password hash function requires 5.6 or later okay so let's try again and just delete everything from the table users okay so let's try again one two three test one two three register and now it says you have been registered please verify your email so let's check now our table users as you can see there is one new person added but our token is empty let's see did we get an email so yeah the email is here let's see okay and the token is empty here too as you can see down below so the latest email and let's see where is the problem so the token problem is here we need to use token instead of string okay so let's quickly delete this person upload the new version and let's test again so I'll just try to refresh continue okay select users and now everything looks good the new user has been added and the token is day so we have received an email 2 let's see now and as you can see now down below there is a token appended to this link okay but now the problem is if we click here as you can see it says page not found because we do not have conflict dot PHP file so let's create a one so I will test you here you PHP file I'll set confirm okay and now what we need to do is we need first to check if there is an email and a token and if there isn't we will just resurrect user to the register PHP so I'll see if not isset get email or not isset the get token we will just set here header register.PHP else now we need to get an email from the link okay email token okay and let's make a new connection again we can just copy that part from our register it will be the same okay so let's sanitize real escape string okay so now first thing that we need to do is we need to make sure that in our table we have this person with this email and this token okay so let's say as well equal connection query and I will say select ID from users where email = email and password and side token = token and maybe let's say we also want to make sure that and this email confirmed = 0 so only the if there is an email and token and email is not confirmed we will execute this will see if sql->num_rows > 0 we will just read is read wrecked again so maybe let's move this to the function redirect because we will use it a couple of times we are going to use it here okay and before that we need to update so we will see connection query and I'll say update users set is email confirmed = one and token equal let's see an empty okay and that's it else if that is not the case you are going again just to redirect a purse so let's say upload this file to and let's try to refresh so I have been now redirected to the registered PHP let's see what had happened inside the database sock it to select and there is nothing that has happened I'm not sure why let's see we said if number of rows is bigger than zero so in his email confirmed his email confirmed equals zero token equal token and email equal image so this part is looking good I'm not sure what can be wrong problems here let's let's not redirect just see what will happen maybe there is some error let's click again here so nothing so this is interesting problem so the problem is obviously in this query so it made mistake here needs to be like this and we need to see where email equal here so let's try again fresh let's not drink so yeah now his email comfort is said to the won and the token is empty so that's it I'll just remove this comment so that it will redirect when it executes okay so now maybe the better way will be to reject the person to the log or maybe to just show a message that email has been verified so he will say echo your email has been verified you can log-in now okay and that's it okay so now let's quickly just also create a login for so just to quickly show you how to verify if the email is verified or not so I will just duplicate this register file I will set in okay and then I can just remove this and we don't need a name we just need a password and here you'll does see if email or like this and then let's delete this part here okay and here we will just remove name and conflict password okay button will be alone in so now when someone submit this form where we sending in email and password at the top here we will just check if email is empty or password and if it is we will show a message please check your inputs otherwise we are going to check our users table so I will say here password so select ID password from users where email equal email okay and now our password is as you can see here a hash so we need to take this hash so I will say here in this part so I'll say again here please check your inputs if there isn't a person with this email and maybe let's say we will get is email confirm so here first thing we need to get this sql->fetch array and then we are going to say that if password verified so first parameter is the entered password and the next one is the hash that is stored in the database so if this password is okay we will we will continue here else they will say again please check your inputs we can also say please take your password but probably that is not the right move regarding security okay and now if a password is okay let's make a test for if is email confirmed so I will say if data is email confirmed = 0 we will say again please verify your email else we will say here now you can create a session here and all other things that you will usually do with the login but our case told us a message you have been logged in okay so let's test this and see did we write the code correctly okay login okay so our email is test123 login and says please check your inputs so I'm not sure where did we get this last email password we have both so this part here is not the case so let's see what is it says password is email from users where email = email so let's give some numbers so that we know what is happening so let's say this is number one this is number two it has to make sure that you know where is the problem a freshmen team and says number one so number one is here it says that there is an arrow like this let's try and see a scene and now it's okay close that I have made a mistake with my email some type of mistake okay so it says you have been logged in but now let's make a test and change this is email confirmed to the zero okay I'll hit save and let's try to walk in again and it says please verify your email and basically guys this is all that you need to know it's just how to create a link how to create a talk on how to send an email and then you will just have one marry one column and we are going to check if the user has already verified his email or not so guys I hope that this tutorial is very simple and useful to you and if you have any questions please ask in the comments below and also if you liked this video please like it and share your friends take care
Info
Channel: Coding Passive Income
Views: 88,761
Rating: undefined out of 5
Keywords: php email verification, php tutorial, php, tutorial, php for beginners, email, php programming, php login, php email confirmation, email validation in php, validation, email verification in php, email verification, email verification key, email validation, verification, senaid bacinovic, php login form, php registration form, how to
Id: u2O_QyPfdpE
Channel Id: undefined
Length: 42min 42sec (2562 seconds)
Published: Wed Oct 11 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.