Login And Register Authentication using PHP, OOP, PDO Completely From Scratch - No back up codes.

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
what's up guys it's been a while since i uploaded my last video so i'm gonna make it up to you so we're going to create a login register authentication in php using pdo so to start go to your song hd docs and create a folder i'm gonna create authentication say demo authentication demo and just drag and drop it to your code editor i'm using vs cool before we start uh as you can see i have no backup code or anything so this is kind of like um freestyle code coding so let's do some googling later if we encounter some bugs and stuffs so let's first create a let's say index.php standard boilerplate let's name the title authentication and by the way since we're using pdo i have this pdo guide in my repository i'll link it up in the description below later so here you can this is a guide for you this has authentication set up like this and some other good functionality the play read update and delete even search data this can serve as a gate for you so better check this out later now let's view the index page it's like global host your folder name which is authentication demo it will automatically run the index.php so let's say this is not a beginner tutorial so i'm going a bit fast now let's create the register php and let's create our hp [Music] and before we do that by the way this is not a css series so i just grab the straps in the end for a bit of styling but we're not going to voice on styling i'm gonna grab this cdn css cdn step two check this out let's say hello world with a class of say text access just to check if the postdrop cdn is working go to your browser refresh the page now it's right it's working so let's let's remove that for now i'm gonna this is the index page let's move to the register page and we need a form for the user to input their data say register and we should have a include let's say header.php and refer that php in our header and let's go to our index.php and copy all this cut let's place it here let me just grab some standard navigation search snap bar i'm gonna copy this one space it the header you can use quick format format document to quickly format your document without pressing shift tab or tab don't just go alt shift f now let's see oh it's not working why i haven't included header so at the top say require once or i'm gonna use include then includes header [Music] now this is our mouth bar then footer just copy this and paste it here and include the footer as well includes footer.php you should not see any changes for now and let's go to the header i'm just gonna change some stuffs here also gonna delete this disabled let's see example changes and we're going to change this to this now let's move to our register.php and we need to create a form so firstly container then i'm not i'm not going to discuss all the styling we're going to focus on the logic so this is from scratch i have no backup so expect some typos or errors later maybe say form oh um say first name then we need to have the input type text the name of first name and id of first name by the way you can remove this but since we are using the bed it is recommended to have an id in your input tool for the label to reference later put a class up and i'm just gonna copy and paste it say last name and you need to have a email and you can validate your email in your html like this but we want server side validation for ebay so i'm gonna use text name email id email and we should also have password i'm gonna use password one because we're going to have a concurrent password and i'm gonna name that password too so the data is confirmed password input type password then should be password to id password and another foreign group or let's say because i want a button block button type submit then class of vpn dtm and say register and let's not forget that name which is registered go to the browser let's go to register php and this is our form the bootstrap is not working as you can see because we haven't included the headers and the footer yet so i'm gonna remove this and let me just copy here based i'm gonna make it one line paste it inside then click format document now we should see that changes and this is now our registered form with let me put an h1 here says register register here and let's move this is the index page because we're not on the index page so this is our register now let's move to that let's first create our database let's go to localhost slash phpmyadmin then you can press sql to create database database or you can click this new let's say database the name of our database let's say uh authentication demo then ctrl enter let me i have some error goes under the authentication database my other database so let me just drop that database database authentication oh nice now it's working fine we have this authentication demo and now let's create a table named users let's say how many columns i want an id first name last name email password and created that so i want six number of volumes we need to have an id auto increment and so first name subway car 255 max value muslin another last name for car 255 and 4 car 25 and what else first name a name first name last name email password fire char or varchar again to that's define with current time timestamp and with the default of current timestamp then press save now this is our database we have an id first name last name email password created that now we should connect our database and in order to do that we need to so i'm just gonna put another php tag here at the top by the way in normal cases i usually do the logic in another file service like class and just extend it to the page but for now let's do all the logic here first we need to create our page our website to the database and in order to do that let's say let's go to the our guide we need to define the host username password and the base name so let's say i usually make them constant so refine say username my username is i usually uh make the constant uppercase then we need to have the host oh wait post this is host host then username and password and we need to have a database the use my username is what password is none database is authentication demo i think yeah authentication demo now let's first set up psn so according to our guide just copy it so the advantage of using pdo is you can have multiple database you just have to specify what type of database for example here we're using mysql mysql can use other databases such as postgres or etc so we need to place it with the host database name database this one got any of that and now we should wait let me google something i need to watch the error say try patch pdo connection i forgot the syntax anyway so when you master googling almost 50 of your of your work will be a lot faster oh yeah i need this say try scoping ah let me place it usually name it variable say new video and psn wait i forgot the proper arrangement okay let's go to the php.net documentation and we need to pass the ideas in here which is the host and the database and replace the user and password here please set up an exception for the error handling so this try if nothing goes wrong this will execute while if something bad goes wrong this catch will execute what's wrong with user it should be username and let me set up another attribute because i need i want to say i want to fetch my data as an object okay let me copy here just make a comment here say patch object and this one this is for limits then let's try we should have no error for now what if we mess something up let's say and you as you can see the catch will run just connection field then the error messages this is the error message [Music] and that's it let's fix that so now the database is set up i usually make a separate folder for that let's say config let's have config.php and let's have a php tag okay let's cut all this place it in the coin pig and we should just include the config file say are required once say i'm in the register as one ping slash config let me mess this up again to see if it's still working and it's working fine now we have to define the logic for the registration so let's say if it's set say since it is supposed to press let's say register then let's just echo register for now to see if it's working and in the option i'm gonna use php echo okay i've got underscore survey server and php itself it goes to the current page so let's try it if we press the register button it should echo register express and it's working fine then now [Applause] we should fetch the data and to do that let me first declare an error sorry because we need to show some errors to the user if the validation goes yeah and so if check if empty say if empty and the square pose let's check where the first name first let's put a moment here [Music] first name here sorry about that then [Applause] oh hey i want the errors to be on social media first name equal to nothing go to empty store for now and lastly email and password one and password too we'll fill the value later so i as i've said this is i don't have codes for expect me to mess up at some point so let's say if empty first name let's have an errors then grab the first name key let's say um first name should not be empty and let's check it first now if we submit with the first name as empty we should yeah get it this is the error for our first name first name should not be empty which is the value of the first name in our errors array so the errors are working fine for now now let's move to the s as um what should i say i usually do a pretty much [Music] to validate the first the first name [Music] to exclude the unnecessary characters regex so let's leave it yeah for now and let's check the last name they should be somewhat similar i'm just gonna rename this last name [Applause] [Music] nice name and should be last name too um by the way we can do as f for this condition but i just prefer if for now see let's check that so if empty let me just grab this empty email install it in the errors email key say email should not be empty else if the email is not empty we need to validate the email so let's say if built not filter bar then wait i forgot the syntax dammit if not filter bar i think you should pass that let's move the email to here there is an underscore post sorry if i mess up sometimes because this is this is all freestyle no now we need to grab the e then we need to pass that email and filter validate email i think yeah so if not if the email is not validated it's not invalid you should expect an error what error message should we use say please provide nobody email let's cross our hands and see if this we're work print the pin our errors first see so if we uh leave the first name and last name empty they should yeah so i think first name should not be empty and last name email should not be empty now if we have an email which is not valid like here like this we should get the email it should be about it even something and yep we get please provide a valid email [Applause] [Music] wait all right it's working fine now let's validate the validity password one check password one just gonna copy here [Applause] say password one say password should not be empty [Applause] and yes if the password is not empty we need to check if password one is supposed to password two wait let me just create a volume variable for them say first last name anyway you can do this then last name equal to empty string for now [Music] but you can also do this last name equal to mean equal to password one password two that's all it that's all equal them to an empty string password one [Applause] then we need we also need that password too we have a lot of tigers i'm sorry sorry about that then we need to check a password one is equal to password [Applause] password too you need to provide an error say um errors password one say passwords do not much and also for that password [Music] password say passwords not much and let's add this guy again then let's check the password too let's just copy the password one transfer to password to password to and we can remove that as of course we already checked out password two and password one if they are much here now at the moment of course twist let's just paint our errors if they are working fine let's check it they are empty they are working oh let's rename this to confirm password should not be empty now it's working fine computer password vmp if we provide something let's say valid email say one two three one two three four passwords do not much yup please try again i've noticed in the password one it should do input type password where's that now if you refresh yeah it's working now we should show the users their errors so here say under the input say first name let's add a say if there is an error errors say first name say yup this is working fine and we can just in other scenarios you should have a condition like this if errors is defined errors if first name is defined this should uh this error message should show up well if not it should be empty but since we declare them at empty at first we avoid we can avoid that so let me just copy the other one so last name you know there's a lots of stuff stuffs you have to do in the validation especially in the authentication so you should learn these things if you want to be a well foreign and let's copy the password to it's gonna format me document and let's check if we register with that data wow they are working this is looking good let's check for typos no typos now if this is valid and related the the value should be displayed so let's provide a value say by you php echo say first name so if this is correct oh what the hell php echo first thing wait i know because i didn't say as i need to ask the sign and why should i put this first name first first name i think this should and now it's working and we should avoid some security issues because you know you have to say html special choice to avoid unnecessary characters or scripts say for example this this is not protected with html espresso charts or other functions i can add a script tag here say script say either say and if i register oh it's not working i don't know why [Applause] well anyway let's move on hey i need to reassign them last name the enhancements this is html charts this is just where so that we avoid some unnecessary extra line of code here so we can just echo the data without doing some conditions save where we mean password it's correct it's copies say yes [Music] say html special choice and you should also wrap this to html charts by the way you can use other functions like html like html special html entities or mysqli escape stream or something like that i can just use html especially charts should spread it right now if you check if the field is the same if the field is valid should not expect an error or we should add the value copy listening gaming password one and the last one is password yeah i know what you're thinking there is a lot of stuff you need to specify when you're doing uh authentication so let's say any valid first name last name and uh saying passwords incorrect not match yup it's working how about if email is not valid yet please provide the valid email it's working now we should if there are no more errors we should um insert that data to our database so let's see some comment here say check if no more errors yeah say here say array filter because the errors this already filter is um it will iterate over in the array and it will check if there's a value in it or not say if there are no errors so if not array filter save recorder no more errors for now see if all these stops is valid say one two three four yep there are no more errors now this time there are number errors we should save the data to the database and say we have a sql variable insert into users then providing value first name name last name name password1 password2 and that's for baby value and i'm going to use the name parameters or if you are familiar with the mysqli prepared statement you can use positional parameters by putting an exclamation mark and later on bind the data but i am going to use the name parameters which is like this first name last name password one by the way the order is very important so should follow the order first name last name will be password and password too now let's do stmt equal to let's grab the connection variable then prepare where that is then we need to execute now we need to bind we have um we need to have an associative array so let's say first name is equal to first name yeah let me just put this in multiple lines then i need one last name and email email them password one our password this should be equal to password but we need to hash first our password so oh we have an error here should be password only and this should be password only as well i need to first say um hash the password we're going to use the nd method for function if you want [Music] we're going to use the password one [Applause] yep now we should save the database let's just echo um new user inserted let's try as you can see this is our database plain database no data now if we add new data register new user inserted so we expect a new user is it inserted in our database yep new user has been registered and you got the password it's now hash yep the registration is complete let's say let me just put this or i want to have a session of user if the user is so i need to grab the last wait let me just go to the documentation next id [Applause] yeah we need to get less inserted [Applause] inside here it's a function called last insert id [Applause] yes insert id say select the movie register user and store [Applause] [Applause] [Music] boy [Music] repair [Applause] ah this is taking so long [Applause] accept you and we don't find that um yep [Applause] slayer all we need to have a parenthesis now i need to store that data so just worry about that here [Applause] numbers for essential and same [Applause] people user and redirect them to the index page [Music] and that's our message [Applause] say uh [Applause] or just anyway i'm just going to remove this direct them to the index page let's see let's register another user let's say john go yeah they are now on the index page and verify that wait let us let us have a session start here so if it include config my session already started [Applause] say we need to input copy required then if objective is said that there's an underscore session user this just means if the user is active say um welcome a user [Applause] and see our first name say f1 it's not working session users okay somewhere oh we are we need to please treat the object as an object to check the sessions session user yep it's working now the user is now active you can access the page and by the way guys we forgot something very important here we first need to check if there are no if there are existing email so before we save the data so here check if email to already exist [Applause] so your coil from users um i'm just gonna copy experience enable execute i only need let's move this say it i think this coin yep if there is this one then email is already registered so we have to provide an error message so already exists please try a new one and if there is the image is not existing yet should run this code cut this format now we need to place some coil in there let's first check that let's go to the register page and let's use uh already registered email i'm gonna use charmander gmail.com last name yep e-min or radixes please try a new one and say john 2 yep it's working fine and we need to remove this we don't have a condition say okay now for header restart the php if it's set [Applause] looser phpmd oh what's their php if is set so we need to open the parenthesis they should yeah they are set for the moment because we don't have a lag out here so let's provide a lot of if i say okay if not yes that is yup they are now you need to have a logout say if not then let's put an else scrappy we have to make this so far later yep if the user is authenticated the logout should be show if it's not the login register should show up let's make this a form real quick let's go to php or not yep you need to open this button type submit i mean then we need to have a logout.php let's grab it sorry guys my voice is kind of soft now you need to destroy that session and they direct them to the lagging page again.php oh let's try it you don't have luck in that hp and you're going to work on that now just got here real quick i need to require that or we should have a condition if is that user we have to redirect them next php and we should also check this in the register so the bottom maybe um outside that [Applause] so if the user is active they can go to the register they will be headed back back to the index now we should be about now if it said that was another score for snuggle [Applause] not working in the header if not is set login and register yes what's wrong hmm okay let me move this squat first oh why is it not working let me just do this right now this alternative since as it's not working but this this is another command that you should use as i'm just gonna use this anyway ah sir space it remove that format save it oh why it's not working if not this arbi is um [Applause] yeah this is what you called uh it's bug bugs fixing almost 90 percent of the fuel requisite developer is to find bugs and fix it i'm going to remove this first so i can go to the register this is taking so long so i'm just gonna remove this oil so here right now yep now let's go to our login just gonna copy everything from the register here place it in the login well i don't need this content password i only need the email and uh password so let me just log in here then they don't need this anymore remove this and i don't need this remove that and i swear to i no longer need the check condition so if there are no more errors let me remove this uh we need to check if the email and password exist in their database equal to password taking so long this authentication needs a lot lots of good you know then i need to say oh i gotta type here i need to execute secure i need to hash the password save password again shift first [Music] md5 function yep and then if if there is stm t row count is greater than zero for open all right let's have a look if there is uh the data exists then should let them login so i need to [Applause] grab the user i'm going to extend the fetch this will fetch it the data as an object and i have a session ah let's say user equal to user or i hope this works fine hope there are no more errors because this is getting so long say that in hp i will define password line 74 24 here is password also now how is that password oh yeah [Music] yes console going should equal to empty string yeah it should work now say i'm going to lag in obviously cross your fingers [Music] you are now left here now we just need to have a lockout [Music] we're almost done here guys let's go to the say header i need to have an e-check [Music] if it's set first underscore essential one her name are you sir i hope there are more errors if it said level button should appear so i need to reform again i guess i believe this earlier you know there's a lot of stops stops [Music] i don't know all right again that php let's hope this work take that out right now index i'm not logging out why is that logo php [Applause] [Music] oh sorry about that we need to require [Music] i think this should work yep this is working fine this is a complete authentication say another one let's make some changes in the index say if you want container h1 say welcome first name and exclamation point i think integrate an empty user first then reassign it if there is a session user let's try again let's register say uh say julia the secondary is dynamic the user is lagging and also naga now the very last thing we have to do is we need to prevent the user from going to the login page if the user is authenticated so let's go to the medium page let's add some statements here here is set dollars underscore session let's say user header face one let's start hp then we also need this to the um register yep then we should also have a condition in our index let's say maybe let's negate it if not is set they shouldn't have access to the index page if they are not lagging now if we go to the index page we should be redirected oh what's stairman hmm and it said trying to get property of this number oh yes let's just grab this and make it yes oh okay yep when we are not authenticated and let's and if we go to the index the page should be protected it's working and if we are authenticated we should not have access to the latin page in the register page which is working fine and this is it this is a complete authentication from scratch i have no backup code or anything we made it all here from scratch we use some googling documentation etc and i'm proud of you for those who are watching until now this is it the code will be on the description below and this guidebook video will also be in the description [Applause] that's it if you like the tutorial press like and subscribe for more videos like this thank you
Info
Channel: wendell suazo
Views: 5,852
Rating: undefined out of 5
Keywords:
Id: DIEPbJOj08U
Channel Id: undefined
Length: 84min 48sec (5088 seconds)
Published: Sat Jan 23 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.