Complete Login & Register System Using MVC & PDO | Object-Oriented PHP | PHP Tutorials

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
what's up guys and welcome back to a new episode where we will be creating a complete login and register system using the MVC pattern and for our database we will use PDO the output will be like this we have a landing page we have a couple of navigation items so a home page and about projects blog contact and login and whenever we click on the login button we will be redirected to the sign-in page with a link where we could create a new account so let's click on it whenever we register so let's say that my username is dari 10 my email is dari underscore add life up NL and my password is well let me just add a password let's click on submit we will be redirected to our sign-in page again and if we go to our database click on the table users which we will be using you can see that our ID is auto increment we have a user name email that has been implemented and we have a hashed password so whenever we try to sign in so let's say dari 10 and my password is well my password let's click on the submit button and we have been redirected to the home page and our log in button has been changed to a log out button and this is what we will be creating in the next one or two hours I will also focus a little bit more on design but if you don't want to follow the design stuff that I will show in this video I won't add a timestamp on the screen and you can just fast forward to that specific part of this video I have already created the MVC framework in the last video so if you haven't checked it out the link will be in the description and if you don't want to follow up video that's one hour and 10 minutes long I will also add a link where you could download the MVC framework before we start with our PHP code I want to create a landing screen first because it's empty right now if I go to my local host and i refresh my local host MVC framework I have a white screen and that's not what we want so let's go to the editor and open our app folder and let's also open our views where we need to create a new folder called includes and what we want to add right here is well to separate our pages so we want our head navigation and further to be reused so we want to separate it from every other file that we have so NR includes let's create a new file called head dot PHP now let's create another one called navigation dot PHP I will go over this stuff pretty fast because it's basic HTML and I'm going to focus on the object-oriented programming stuff inside our head dot PHP let's write down doc and hit tab now let me zoom in a little bit we can remove our body and HTML because that will be included inside our footer below our meta charset let's create another meta which has two attributes the first one is HTTP - equal if and the value is equal to cachet that I should control I forgot to - at last 99 and the second one is the content which is equal to no cached and what this basically does is it allows the web publishers to define how pages should be handled by caches let's create another meta but we don't need the double quotes let's create another one which has a name that is equal to a viewport and the content is equal to width equal device - width comma initial - scale which is equal to 1.0 inside our title tag we want to create PHP opening and closing tags and we want to echo out a content that we created in our comic that PHP file called site name and we want to end it with a semicolon now let's create a link tag with the rel equal to stylesheet and the HR f is equal to PHP and we want to echo out the URL route and after the closing PHP tags that's the forward slash public forward slash CSS /title is I also want to change the font so let's go to the browser and let's open a new tab and let's go to font at google.com and you can search for whatever font you want I will just type in model because that's actually what I like the most let's click on it let's say that we want to select this style and we can add even more so I want the regular one as well and I want the light one as well let's click on embed and right here we just created our link so let's copy it and let's place it right below the link of our style sheet that we created and don't close off the tab because we actually do need our font family in our CSS but we won't do it right now you could also add your font awesome and jQuery right here for now we won't be using it but I will actually place it in just so you can use it whenever you want let me dial it a little bit and the script for a jQuery as well now we're done with our head that PHP so let's save it and close it off and let's focus on our navigation the first thing that I want to create right here is an F so let's hit tab now let's give it a class which is equal to top - nav our navigation has an unordered list so let's write down ul hit tab and inside our unordered list we have list items and every list item consists of an H ref so let's write down a and hit tab the first link that I want to have is the homepage so let's write down home and inside the href let's create PHP opening and closing tags and let's echo out the URL root again which is well I can show it to you let's open the comic that PHP where we created a constant which is equal to the URL root which is pretty much the link or the path of our URL what we want to do is to add a forward slash after the closing PHP tags and we want to go to the pages forward slash index page and this is why I like to work with them URL root we don't need to change every single URL whenever we upload our website to an actual server we only need to change it inside the convict that PHP file so but I just showed you we only need to change localhost and/or MPC framework and it will be changed everywhere let's copy-paste our list item and paste this five more times so for and this is the fifth one let's change the second one to about now let's change the H ref as well the third one is the project space the fourth one is the blog so let's say blog we have a contact page so let's change it to contact and the last one is the page that we want to focus on and that's a login page but we don't want to put it inside a pages folder but we rather want to create a folder called users but we will have our full log in register page and so on so let's change it while the text to login let's change the path to the users forward slash login for our login button we want to add a border around of it so let's give it a class so our list item of BTN - login so let's save it because we're done with our navigation as well and let's go inside our index dot PHP inside our pages folder the first thing that I want to do right here is to include our head that PHP file so on the top of our page let's create opening and closing PHP tags and be aware that we do need our closing PHP tag right now because we will add HTML inside this page and what we want to do is to require space F root space and we want to concatenate what we want to include so in single quotes /vo forward-slash includes /head dot php' which is the file name so let's save it and see if we have an error inside the browser and we don't have it so that's what we want we could also add the navigation but what I want to do is to add a navigation on top of the background image so let's create a div outside of our require now let's give it an ID of section - landing and inside the div we could basically copy our require paste it let's save it and double check if it works well I need to change it to navigation of course save it again refresh the browser and our navigation is included I also want a piece of text on the image so let's go outside of our PHP tags and I'm going to create an h1 with a quote so let's say one man's crappy software now right below our h1 I want to create an h2 is another man's full-time job and the last thing that we need to do before we go sign on our page is well to go to the browser open a new tab and write down pixabay.com and you can search for whatever image you would like I already have an image I brought down city and the image that I actually have is this one so it's Hong Kong and if I go inside my public folder the image folder you can see that I have my banner jpg image safe right here and that's what you need to do pause the video go find the image that you like and I'll see you in a second now that we're done in our index at PHP let's save it let's close it off let's go inside our public folder because we need our CSS right now I will go over it real quick because this video is not about styling if you do like styling I have HTML and CSS videos that I made before the first thing that we need to do is to set our HTML comma body the height to 100% and the width to 100% I want um padding of zero margin of zero and top and bottom of zero and I want to change the font family so let's go to our google fonts again let's copy the font family and paste it right there what I'm going to do next is to style the navigation so let's say punctuation mark top - nav and I want to set the display equal to block now I want to style the list items so let's copy-paste it and add an ordered list excuse me after it and what we want to do for our unordered list is to set a margin equal to zero adding zero as well we want the position of absolute so we want our navigation to be right 6% and top 2% now let's copy paste it one more time because we want to style the list items where you want to set the display equal to inline block and the margin left - 28 pixels we also have our unordered list items answers but we will set the color equal to F F F F F F so white the text decoration is equal to none and the font size is 18 pixels save it refresh the browser and you won't see anything right now well right here so I want to add a hover so let's copy the UL li a and let's add hover right after it we will set the color equal to AF AF AF and the transition equal to 0.15 seconds - East - in save it refresh the browser and now the hover effect has been added what I want to do now is to add our background image so let's say hashtag section - landing has a background of URL I made a typo excuse me of single quotes punctuation punctuation /img /a a jpg and this name is whatever you have for your image I want the background - sighs to be cover I want the background position to be in the center and I don't want any repeats so the background repeat is equal to no - repeat and we have a height because we want it to be full width of 100% and the width is equal to 100% as well save it refresh the browser and this is the output and this works well pretty good to be honest I actually forgot to do something inside are indexed at PHP let's wrap our navigation and h1 and h2 inside another div called wrapper - landing so another class let me style this alright let's save it let's go to our song sheet again what we will basically say that the wrapper - landing so everything that's inside the background image has a position of relative text online everything in the center so I want a margin of zero Auto and I want the top to be 40% so let's save it refresh it and something went wrong oh yeah because we don't need to wrap our navigation inside the div excuse me another mistake that I made let's tunnel let's make this a little bit better all right save it refresh it and this is what we want now let's go down because we're not done yet I want to set the wrapper - landing h 1 equal to a font size of 48 pixels the color is white - ffffff a margin of 0 and the font weight is 100 save it refresh it and this is what I want and for the h2 so let's copy-paste it and change h1 to h2 I want this font size to be a little bit taller so 42 I'm with the color of f2 f2 f2 and I want to change the opacity to 0.6 and the margin and font weight is the same save it refresh it and this looks very good I also want to add a border on the login button so let's say that the BTN - login as a border of 1 pixel solid white and the padding is equal to 6 pixels 14 pixels so 6 top bottom 14 left right well let's say 18 left right 1 it a little bit more so 24 and this is what I want now that we're done with the home page we need to go inside our controllers dot PHP so where you have our pages that PHP and we need to create a new file called users dot PHP with a capital u because we want to start working on our login page and what the users that PHP will do is basically take care of the flow of our application it gathers all the necessary data from the models and it will assign it to the view so let's create an opening PHP tag because we don't need a closing PHP text since we're only working with PHP code right here and what we want to do is to create a class called users which needs to have the same name as the file and we want to extend the controller the first thing that we need to create inside our class is a constructor so whatever will be inside the constructor will be loaded first whenever a page inside the users class is called so let's create a public function double underscore construct or we say that this pointer user model is equal to this model and we want to search for a file called you what we want to do now is to create a method inside our users class with a name login so below our constructor let's create a new public function login and this is basically where all the logical stuff of the login form will happen and be aware that this file needs to have the same while this method name needs to be the same as the file that we will create in a second inside our method we want to pass an array so data is equal to an empty array let's hit enter inside our array which will go look inside the models find data from the database and it will pass it inside the view right now we don't have any data to pass in so let's just pass in the title and this is an associative array so let's say pointer login page and right below our array we need to define the view by saying that this pointer of view and what we want to do is inside single quotes search for a file inside the user folder /login and we also want to pass in the data array that we created above so let's say dollar sign data let's save it because we're done inside our controller and what we need to do is to go inside our models folder and let's create a new file called user dot PHP inside our user dot PHP after models let's create opening PHP tags and right below our opening PHP tag we need to create a class called user and the first thing that we need to do in our model is to create a private property of the database so let's say private dollar sign DB so a private database where we need to create a constructor first so let's say public function double underscore construct and what we want to do is to instantiate the database property by saying this pointer DB is equal to a new database object and with our database that PHP is is basically in our late library's database that PHP where we have all our database stuff now that we're done with this let's go to our abuse let's create a new folder called users and inside their users folder let's create a new file called login dot PHP we could also create another one for a register form so let's say register dot PHP and let's close it off because I want to focus on our login page well we can actually test it out by writing down login save it go to the browser and right after MVC framework right down users forward slash login and the class user is not found in the controller dot PHP I'll assign this login works now that this works inside our login dot PHP I want to add input fields and other stuff inside our code what we could do is to go inside our index at PHP copy the require paste it right there because well we need our hat in every single page but we also need our navigation but since the color of our navigation items are white we want to place it right inside of a div called nav bar so let's face our require right here save it refresh the browser and well our items are somewhere right here that will change head again to navigation and we have our items well right here but we want to add another background color but I don't want to do it right now because I want to add everything inside our login dot PHP first right outside of our nav bar class let's create a new div called wall has a class of container - login and it has another div inside of it called wrapper - login the first div will basically be the full width so the 100% and the wrapper will be a 80% with inside the full width and I hope that makes sense what I want to do is to add an h2 with the text of sign-in and right below our h2 I want to create a form since we're going to pass in input fields to the controller so let's write down form hit tab which has an action of PHP opening and closing tags and we want to echo out the URL root semicolon and after the closing PHP tags forward slash users forward slash login which will go inside our users in the controller and search for a function called or method called login we also want to set a method equal to post since we're working with sensitive data I want to set the method equal to post because we don't want to pass in a user's password inside the URL inside our form we basically want to have input fields and the user can log in with a username and a password so let's write down input ahead tab let's keep the type equal to text and I want to add a placeholder which is equal to user name space star so the user know that if he wants to log in the input field is required and let's give it a name of username we don't want to add the HTML required right here because I want to do on the validation inside PHP which is way more secure so let's not do that right now right below my input field I want to use something called a span so let's write down span and hit tab let's hit enter and I want my span to have a class of invalid feedback with capital F and what I'm going to do is to add PHP opening and closing tags and I want to echo out data so the array that we created brackets single quotes and I want to write down user name ever and we haven't created this right now but we will do it in a second this basically does is whenever a user enters the wrong username so let's say that he will add special characters that are not allowed inside the username we can give them an error message that the format is wrong and if it is wrong I want to create the PHP opening and closing text I want to echo out our array data with an error message inside of it if we save it and refresh the browser right now you can see that we get a notice undefined index username error and the reason why that is happening is because we haven't defined user name ever so it doesn't exist so let's go to our users controller let me close off all the tabs that we don't need so let's go to the users controller and right inside the write data of the array that's right down well right after title let's add a comma and let's set the username error equal to empty again and let's do the same thing for the password so that's a password error pointer empty and this is the reason why we're working with an associative array right now because we have a name that we can use so the index is well not the 0 1 2 3 and so on and we have a value which is empty right now so if we save it refresh the browser our error message is gone let's copy-paste our input and span one more time because we want the same thing for the password where the type is equal to password the place holder is password star name is equal to password as well and for the span I want to write down the password error we also need a button to submit the page right below our span button hit tab which is an ID of submit the type is equal to submit and the value is equal to submit as well and in between our button let's write down submit save it refresh the browser and this looks good for now but the last thing that I want to add is a paragraph right below my button so let's say P tab and I want to give the class of options because we want a user to always have the opportunity to register so what we want to say is not registered yet question mark and sure and inside the entry we want to say create an account we need to fill in the enter so let's create opening and closing PHP Tech once again let's echo out the URL root semicolon and after the closing PHP text let's write down users - register so we will redirect someone to the register page I need to style this page as well and once again I'm on come in-depth about it so let's go to this title dot CSS page and let's go to the bottom where we will say that our dot navbar has a width of 100% height of 70 pixels a background - color of 1 a 1 a 1 a and a box shadow of 0 pixels 0 pixels 10 pixels and the color is the same as above for our container - login the width is 100% the margin is 0 although the position is relative and the top is 20% and for the wrapper - login the width is 80% the margin:0 auto as well and the text online is center now less time of the input field but we will spacely say that the wrapper - login space input as a width of 200 pixels and height of 26 the border is 1 pixel solid 6 times C the background color is at 5 f5 f5 the font size is 18 pixels the display is block position:relative and the margin is 20 pixels auto save it refresh the browser and well this starts to look pretty good let's style the h2 so repre - login h2 where the font size is 40 pixels and the text transform is uppercase the sine of the input placeholders so let's say that our input double common placeholder as a color of a1 a1 a1 and the font size is a little bit smaller so 14 pixels we need to style our submit button and what we basically could do is to copy our input styling paste it right here let's set the height to 42 pixels the border color to black the background color is black as well and I want a color of ffffff and the font size is 20 pixels I don't need a display:block and I don't need the position:relative I don't need the auto margin so let's say 20 top and the rest is 0 save it refresh it and this starts to look pretty good and I want to style the hover as well so let's copy the submit button paste it right below let's add a hover where our background color is only needed and the border the border has a color of a1 a1 a1 and the background color is a1 a1 + 1 as well and a transition 0.15 seconds - and save it refresh it and this looks good and the last thing that we need to style is this piece of text right here so let's say dot options a has a color of green so zero zero six four zero zero let's copy-paste it and I said a hover where the color will be default so black the transform is zero point twenty is in well the transition excuse me and the text declaration is none and I also wanted assigning for the invalid feedback though you won't see right now but will appear when you make an error and the color is red so ff0000 save it refresh the browser and this looks good now we're pretty much done with the styling but I want to do is to go inside our register dot PHP well let's copy our login form first let's close off our login dot PHP let's go to our register dot PHP let's face it right here and what we're basically going to do is to change a couple of things instead of saying sign in let's say register the action needs to go to users forward slash register after the username were of the Impa to an error message let's copy it let's face it and we need to change the type to email the placeholder to email and the username well the name to email as well and the error message is email ever and for the password we basically need to complicate the same input and span paste it instead of saying placeholder password let's say confirm password and the name of our field is confirmed password again and the error message of course is confirmed capital P password error we don't want to create a new controller because we already have our users that PHP so let's open it and let's create a new function called public function register and this function has an array again so data is equal to empty let's go inside of it and hit enter where we want to add a couple wall values so the first one is the username which is equal to empty let's copy-paste it a couple of times the second one is the email third one is the password fourth one is the confirm password the fifth one is the error messages so email ever both username error the email error and we need two more for the password error and the confirm password ever and let's remove the last semicolon we want our page our register page to have a path to the view so let's say that this view is in single quotes users forward slash register and we want to pass in our data let's save it test it out refresh the browser let's click on create an account and something went wrong right here on line 19 in the controller because we forgot the pointer or the ballbreaker then save it refresh the browser and this is our register page what we're going to do right now is to focus on the backend so let's go to our controller and I want to continue on right below our data array and this is where I actually will go more in-depth because this is what you actually came here to study for and not the HTML and the CSS so whenever the button is clicked on the register form so let's say the submit button we want to create an if statement so let's say that if and what we want to check is the super global surfer brackets inside the brackets we want to check in single quotes for the request underscore method and what we want to check is to see if this is equal to post what we do with super global surfer request method so the first part is determining whether the request was a post or a get request this can help to determine whether to parse income parameters from a get or a post and since we have set this equal to post the outcome should be equal to true what we used to do in other videos if you have checked mine or which is pretty common is to create an if statement and is to say is set and inside the is set function to say super global post and we want to check for the submit button but that's not what you want right now so let me remove that the next thing that we want to do is to sanitize post data so let's say dollar sign post is equal to function called filter underscore input underscore array which will return false if the post variable contains a scalar value this built-in function takes two parameter is the input underscore post and the second one is well after the comma filter underscore of sanitized underscore string and what this basically does is stripping or encoding unwanted characters it will remove data that is potentially harmful for your application and this is some video stuff that I really like to use if we get through the if statement that we created we need to copy paste our data array well I'm actually writing this wrong because I don't want to focus on there long and first so let's face it copy paste it inside the register function excuse me so if we get true the request method and if we have sanitized the post data let's copy-paste the data array that we created and we need to trim the user name so let's add a function called trim and what we want to trim is the dollar sign data bracket single quotes username now let's complete pasted it for the email and the password and the confirm password as well the email is data email password is data password and the confirm password is well let's copy pasted confirm password and what trimming basically does is removing all the white space on the left and under the right of an input field so let's say that someone enters his name inside your input field with double space dari 0-6 what trimming does is basically removing the white space on the left and on the right hand side which is pretty cool what we need to do now is to validate all our input fields and these can be based on whatever you or your client wants I want to add some important validations that I think are pretty necessary to use but there are lots of that you could actually use and I want to focus on the username first so let's go outside of our array now let's create a new variable called name validation and let's set it equal to double quotes semicolon what I want is that someone is only able to enter the letters A to Z and the numbers 0 to 9 so let's say forward slash point or up well I actually don't know what the name is brackets and inside the brackets we want to say that a - C is allowed in lower case and in upper case and we also want to allow 0-9 and after the closing bracket we need to add a star dollar sign forward slash and right below of it let's add a comment which says validate username on letters and numbers but before we do the check let's create an if statement to check whether the username is empty or not so we could use a built-in function called empty brackets and we want to check if the data bracket single quotes username is empty or not whenever someone clicks on the submit button if it is empty we want to set the data break it single quotes username error equal to please enter name or username we need to create an else--if statement because we want to use another built-in function called prick underscore match but we need to add an explanation mark right in front of it because we want to check if it's not true and we'll want to check if it's not true if there is a match or not so right after break match let's add parentheses and we want to see if our name validation and we want to see if there is something else inside data brackets single quotes username and we can copy our error message let's place it inside the elsif let's change the message to name can only contain letters and numbers so now that we're done with the username let's say that we want to validate email now obviously we could actually copy the if statement because we're going to also check if it's empty or not let's change data username to email let's say email error and what we want to say is please enter email address what we want to do then is to create an else--if and we want to say that explanation mark filter underscore VAR and this function filters a single variable with a specified filter and this takes two params the first one is the variable that we want to check in an our case it's data brackets email and the second per am so let's add a comma this filter underscore validate underscore email and this basically removes all illegal characters from an email address so if there is a match so whenever there are characters inside the email input field that do not belong in an email input field we want to set the email error so let's copy the previous one paste it right inside of it too please enter the correct format and then we want to create an else but we want to well let me add a comment to check if email exists so let's create an if statement and inside the if statement we want to check if this user model and we want to create a method inside our models I want to already specify the name by saying find user by email and this is a method because we want to pass in our data email that needs to be checked what we want to do is to print another error message which says well if it is found that basically means that the email already exists so we want to say that the email already taken so let's go to our models so the user models and right below our public function construct let's create a new method called public function find user by email what we want to do is to pass in our email as a parameter and we want to perform a query so let's say that this DB query which is well a function that we created I can even go to show it to you and this allows us to write queries so let's close it off add single quotes inside the parentheses what we want to add our query and what we want to do is to select star everything from the table users that we do not have but we'll create where the email address is equal to a prepared statement so call an email let me add a comment actually which is this is a prepared statement and right above our method let's say find user by email email is passed in by the controller right now our placeholder is empty so let's go right below our query and let's say that the email program will be binded with the email variable and the way we do that is to say that this DB bind well it takes two parameter first one is the placeholder so call an email comma the actual email so let's say email then we need to check if the email is already registered and the way we do that is well let me add a comment is to check if the row count is greater than zero because whenever it's greater than zero the email already exists because it needs to be equal to no so let's say if this baby Pro count which is a function that we it is greater than zero if this is true I want to return true and else I want to return false and the row count has been made in the database at the PHP as well so let's go to it which will basically count the rows that has been selected or where there is a match but I won't cover it right now because we did that in the last episode let's close off the database and let's go to our controller again because we're done with validating the email I want to focus on the password right now so let me add a comment right after it so validate password which is a little bit more difficult than the username and the email but it's alright for the password we want to validate it on length and numeric values because we want our password to have at least a minimum of characters and at least to have one numeric value and once again let's create an if statement and check if the input field of our password is empty or not so let's say empty data break is password if it is empty I want to say that the data password error is equal to please enter password after the if statement let's create an else if statement where you want to use a built-in function called STR Len so string length which basically returns the string length that has been inputted and what we want to check is the data passwords and we want to see if it is less than 6 characters so let's say less than 6 let's copy our error message paste it inside of it and let's change the error message to passwords must be at least 8 characters what we could do is to go right below our name validation and let's create a variable called password validation and I want to well this is a pretty difficult one so inside double quotes let's add forward slash arrow up opening parenthesis punctuation mark curly brackets inside the curly brackets let at zero point seven vertical bar brackets and inside the brackets we need arrow up eight until Z star vertical bar brackets arrow up backward slash D and after the bracket let's add a star again and a closing parenthesis and the dollar sign /i and this looks pretty weird just copy/paste it and this is basically a check for the password validation and what we basically want to do is to copy the else--if of our preg match and paste it right after the elsif of the password validation let's add a space instead of saying name validation let's say password validation the data needs to be password as well and the error message that we want to return is password must be must have at least one numeric value and obviously the data error message must be passwords error actually is that right password error yes and for the confirm password we could basically copy the if statement go outside of the well validating password let's say validate confirm password paste the if statement right here let's change the data to confirm password and the error message to confirm password error and instead of adding an else if we only need to create one else and inside the else we need to create an if statement because we want to check if password of the input field is not equal to confirm password and the way we do that is to say data password explanation mark equal to so not equal to data brackets confirm passwords so whenever the first password and second password are not equal to each other we don't want a user to be able to create an account so the message that we want to give is well let's copy the error message again and we basically want to say that passwords do not match please try again there are a couple things that we need to do before we're done with the register form first off I want to add an either check to see if all the errors are empty so let's go outside of our if statement again let's say make sure that every are empty and what we basically want to do is to create an if statement now let's say empty data username ever after the well first closing parentheses let's add a double ampersand and let's copy everything inside the if statement let's face it right after the double ampersand and let's change the second one to email error and bolas ood one more time for the password ever and we needed one more time for the confirm password ever so let's say confirm pass word ever this is more like a double check up to see if it's empty or not and if it is we want to proceed to hash our password and for hashing our password in PHP I usually use the password underscore hash function which creates a new password hash using a strong one-way hashing algorithm so you can never see what the password actually is you can see if the password matches whenever you log in you're never able to print out the password well let me add a comment ash password we need to set the data brackets password equal to the function password underscore hash this takes two perhaps the first one is the actual password so data brackets password again comma second one is password underscore default and this is a bcrypt algorithm which PHP will automatically change and update over time as new and strong your algorithms will be added if the password is hashed we need to create another if statement so that's a registered user from model function what we want to check inside our if statement is if this is your model and we want to create a new method inside our models called register and we want to pass in all the information that we have so our variable or array data we will be creating the register in a second if this works we want to redirect to the login page so whenever someone just created his account we want to redirect them to another page that can be done with a function of PHP called header inside single quotes let's set the location : let's go outside of the single quotes let's concatenate the URL root one more time because we want to add forward slash users for a slash login if this does not work let's add a die function so let's say it else and we want to say die single quotes something went wrong so let's save it because we're done in our controller but we need to go inside our user model and we need to create a new public function register because we want to add the actual information inside the database and this can be done by inserting data so to use an answer query so let's say that this DB query inside single quotes we want to insert into a table called users space parentheses the value username email and password outside of our parentheses we want to add the values of username email and password so let's say values parentheses again the value is Colin username Colin email comma colon password and obviously we need to pass in our variable data excuse me right below our query we need to find the values again and we can base actually copy the find user by email paste it below bind values two more times actually the first one is username second one is email and the last one is password and instead of saying email we need to pass in data brackets single quotes username now let's compute a set for the email on the second one to email and for the password as well so let's set it equal to password and whenever this is done and it works we want to execute the function by saying that if this DB execute so there is a possibility that these placeholders and actual values can be combined we want to return true otherwise so else we want to return false let's save it let's go to the browser because we need to go to localhost forward slash PHP myadmin let's click on MVC framework I created my MVC framework table in the previous episode if you do not have one let's click on new create a database let's click on your database name that you just created let's create a new table called users with four columns first one is the ID the second one is the user name the third one is the email and the fourth one is the password the type of the username is for char email as well and password as well the length is 255 for them all and our ID has an auto increment so let's click on AI and let's click on go now let's save it let's go back to the browser let's refresh it and well something went wrong on line 75 in our controllers so let's test that out and controller 75 something right here so empty oh yeah we added a double ampersand at the end excuse me save it refresh the browser and on line 19 because it's request with a queue excuse me save it refresh the browser and let's test it out so the user name is Dario zero six seven the email is info at dari nazar calm the password is well I will just add something let's click on submit and something went wrong right here so let's go back to the code editor and let's check where let's start at the top this is right oh yeah we don't need to trim the variable data use your name but on this for post so let's replace it everywhere save it and let's test it down one more time let's say Daria 0-6 email is info hat passwords let's submit and the find property database rowcount online 36 in the models because row count is a function save it refresh the browser and possible password must be at least 8 characters let's test that one now this well well this must be good let's test it out one more time let's $1 is every 6 passwords let's click on submit and this works and let's refresh our users and my ID username email and password has been hashed now that we have finished our register form I want to focus on the login form which will pretty much be the same as the register form but like I said before I don't want to copy/paste it since I think it's good practice for you to go over it one more time as you can see in the browser we have been redirected to the login screen so whenever our user registers we want to login so let's go to the code editor and let's go to our login method that we created right below our well register method and what we basically want to do is pretty much the same because we want to start off by checking for the post by creating an if statement where we want to set the super global server brackets single quotes and we want to check the request method and we want to see if it's equal to in single quotes post then we want to sanitize the data so that's add a comment which says sanitize post data and we need to set the super global post equal to and built-in function called filter underscore input underscore array which will return false if the post variable contains a scalar value the further input array takes to params the first one is input - underscore post comma and the second one is filter underscore sanitize underscore stream so we're sanitizing the data right now it will basically remove data that is potentially harmful for your application so if someone is trying to heck you he could well put JavaScript stuff inside the input field of the username and by saying filter input sanitize string you will prevent someone from doing that because it will only allow it as a string whenever we get through the if statement and whenever we sanitize the data we want to create our array or we want to write down our array where we have a couple values the first one is the username which is equal well to the pointer because it's an associative array to trim parentheses comma and inside the parentheses we want to say super global post brackets and the value that we're passing in so let me go to the login screen and you can see that for the user name the name is equal to username so we're passing in in single quotes username let's copy-paste it let's change the second one to password and what we can actually complicate it two more times because the third one is username error and we can remove the trim and only add single quotes because it's empty and well the second one is password and the last one is password error and we can remove the trim as well and replace it with single quotes now like I said trim is a pretty cool function because it is used to remove the white space and other predefined characters from the left and the right of the stream what we want to do now is to validate our input fields so right outside of our array let's create an if state well let me add a comment validate user name and inside the parentheses we want to use an empty function and we want to see if the variable data bracket single quotes username is empty or not if it is empty we want to print out the error message so we want to assign a value to user name error so let's say dollar sign data brackets and inside the bracket single quotes and it's username ever is equal to please enter a user name now what we could do is to basically copy/paste it because we need to do the same check for the password so validate password we want to see if the data password is empty or not and we want to print out the password error and we want to change the please enter a username to please enter a password the next check that we want to do is to create a new if statement because we want to check if all errors are empty so let's create an if statement again and the values inside the parentheses is well we can copy the empty function of the username paste it inside the if statement it's space a double ampersand because we want to check if the username and password are empty or not so let's face it and let's change the second one to password and what we want to do if it is empty is to basically create a new variable called locked in user which we want to set equal to this use your model because we want to do a request to our models we want to create a new method inside our user model called login and we want to pass in two variables the first one is data brackets single quotes username and the second one is the data bracket single quotes password so let's save it now let's go to our models user that click on it and well right below our register we could basically create a new public function method called login because that's basically what we just defined we want to start off by creating a new query which will search for a match in the database so what we want to do is to say this DB and we want to go to the query again our query is selecting everything from the table users where the username is equal to the placeholder username we also need to add two params inside our method because we want to check for variable username and variable password and this is basically how prepared statements work with PDO you write down a colon and you add a name we can say whatever we actually want it doesn't need to be username and later what we will do in a second is to set the value of the placeholder equal to something by binding it so let me add a comment outside of our query which says find value and the way we do that is to say that this DB binds and what we want to bind is the first one in single quotes which is the placeholder so let's say all in username outside of our single quotes comma the actual value that we're passing in so user name and I recommend you to always use prepared statements instead of a regular statement what's happening now is that the value of the username in Pittsfield will fetch the details so it is not possible to write a query for each value and since we're doing a request for a specific row because we want to select everything from one specific row where the username is equal to well the placeholder username so we need to create a variable called row and we need to set it equal to this DB single because we want to return a single row if you do remember in our register form well let me actually show it to you we hashed our password let me see where I was right here because we use a method called passwords hash and to check if our password is equal to it or not we need to create a new variable called hashed password and we need to set it equal to row pointer password and then we need to verify the password by saying well if parentheses curly braces and we want to say that password underscore verify and which is a built in function so we need to add parentheses again the password verified takes two params the first one is the regular password so variable password comma and the second one is the hashed password so let's say variable hashed password if there is a match with password and hash password which we cannot see but the system will detect if it's true or not we want to return so inside the if statement that specific row otherwise so else we want to return false so there is not a match now that we're done with our models let's go to our controller we'll save it first and let's go to the controller let me scroll down and what we need to do now is to create a new if statement because we need to create another if statement to see if the lock in user is set so inside there in if statement let's create another one and let's just say locked in user and if this is set we want to create a new method outside of our function but we will create a session and what we could do is to say that we want to start the session inside of our if statement but I think it's good practice to create a new method and to call it inside the if statement so let's go outside of our method let's create a new public function create user session which takes one parameter this variable user and inside the if statement we want to start a session so that's a session underscore start parenthesis single quotes and we need to create a new session variable called underscore session brackets and we want to set it equal to a value inside the brackets we want to create a new session with a value so let's say that we want to create a user underscore ID in single quotes we need to set it equal to a value and that can be done by saying that variable user pointer ID because we're referring well let me go to the PHP myadmin to their table ID so number two will be equal to user ID which we will set equal to a session that we will create so let's copy-paste it and do the same thing for the username and let's change the user pointer to user name and if we go up again because right now we created a session whenever a user tries to login let's go to the if statement and what we want to do right here is to say this create the user session which we will call and we want to pass in the locked-in user which is basically equal to our login method so we're selecting everything from tables user so we're selecting the ID username email and password and we're using it by passing it in the session so as a user and we will use the values ID and username well let's actually copy patient and do the same thing for the email if a user is not found obviously a session will not be created so what we want to do is to create an else statement and we want to return every message which says what we want to say data brackets and single quotes the password error is equal to whatever message of password is incorrect please try again well what we're doing right now is well I think wrong because we don't want to say which input field is wrong so after password space or user name is incorrect please try again what we want to do then is to load our view again by saying that this view in single quotes we want to load users /login and we want to pass in data i've actually forgot to do something because if the if statement that we created wall right here or we're checking for the request method does not exist we want to set the array of data so let's copy-paste this one well we need to add user name again to something which is empty and for the password that needs to be added as well let's copy it let's go right below of our function and we need the record of the well that's this one so one above that this view user login let's create an else and what we basically want to do is to paste it right here so we can remove the title because that's already included now if we save it and go to the browser and to our framework let's try to log in well forward my username so Dario 0-6 sorry 0-6 let's add my password now let's click on the submit button and the problem right now is that we're not doing anything if someone is logged in or not what we could do is to go to our navigation dot PHP in our views and inside our button login we could create PHP opening and closing text an if statement we basically want to check if it is is set and what we want to check is superglobal session brackets single quotes user ID which we have set right here if someone is logging in and there are two ways in well object-oriented programming how you could create an if statement what we could do is to say after our if statement space : so instead of curly braces if the user ID is set we want to change the text of the log in to log out right below our enter we need to create another set of PHP tags inside our PHP tags let's write down else space : and this is a way easier form to add PHP code inside your HTML on the line below we want to copy or enter place it right here and we want to say log in because if someone is not logged in we want to write down log in and otherwise we want the user to log out because he's already logged in and to close it off we need to say PHP and if semicolon and to see our session go let's save it let's go to our login dot PHP right below our navigation let's create PHP opening and closing tags and that's far underscore dump our session so it will print be printed out on the screen because it's an array FR dump can only print out arrays let's also change the logout users forward slash log out which we will create in a second but there's actually one more thing that I want to change and that's in our users that PHP of our controller I want to get rid of the session start and what I want to do is to use our helpers folder that we have and I want to create a new file called session underscore helper dot PHP I want to create a PHP opening tag well let's get of rid of the closing tag and what I want to do right here is to say session underscore start and I want to create a function called and I don't want it to be public called is locked in where I want to create a if statement to see if is set and what I want to check is the accession brackets semicolon underscore ID if it is set I want to return true else I want to return false and in order to call this session on the score helper we need to go to our require that PHP and we want to require on the score once helpers for slash session underscore helper dot PHP save it refresh the browser and you can see that the user ID username and the email that we added are created and you could also see that the log in button has been changed to logout if we click on it nothing is happening so what I want to do now is to go back I want to go inside our users at PHP which is the last thing that we're going to do is to create a public function log out for the logout button what we want to do for our logout button is pretty easy we just want to unset our session brackets user underscore ID let's copy-paste it two more times because we want to unset the user name as well and the email and what we want to do is to redirect to well header the location colon space well we don't need to add the space concatenate from the URL root and we want to go to the pages forward slash well let's say that the users for slash login page save it refresh it let me log in this works we have our logout button let's click on it and we have been locked out and the session has been set to an empty array well let me actually get rid of the far dump in my screen so let's go to login let's remove the far dump and this was it for the login and register script using MVC and PDO in the next episode I want to create a block I won't focus on the about projects and contact page because that's not something you would do with object-oriented programming you we will with a block if you do enjoy my content and you want to see more leave this video a thumbs up and if you're new to this channel please hit the subscribe button
Info
Channel: Code With Dary
Views: 42,518
Rating: 4.8993711 out of 5
Keywords: advanced php tutorial for beginners full, how to learn php language, create web application in php, php tutorial for advanced programmers, what are the features of php, learn oop php step by step, learn php step by step with example, php programming language tutorial for beginners, best way to learn php, how to code in php, advanced php tutorial for beginners, php object oriented programming tutorial, introduction to oop php, php mvc login system, php mvc register system
Id: e1oMBaWjye8
Channel Id: undefined
Length: 78min 25sec (4705 seconds)
Published: Mon Aug 03 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.