PHP OOP - Complete Login, Register & Logout System w/ Authentication Check using Session in PHP OOP

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys welcome back so guys in this video we are going to start with the new series that is uh the object oriented programming in php so guys we will be covering the topic of login register and the database connection with authentication so let's let's get started okay so guys uh i have already set up the files which you can see here that is our includes header navbar footer everything i have just set up over here so if you want to know how to set up this you can watch out the video which i have given the link in the description so this is the video guys where we can see that how to set up a front-end panel and start working as the navigation bars so now guys let's begin with our first step that is the database connection in php oops okay so let's begin so let's go to our file so guys over here you can see that only includes an assets assets consist of your css js and image that is logo and yepguys and simpleindex.php file so let's close all the files and begin with index.php and here is the output so now let's create one folder with the name config that is our configuration and inside this we will create one app.php file okay so in this we are going to define our database connections and his host and all so let's begin with php open and php close so type define function where inside this you'll tell db host comma inside single quote we are going to give local host so copy and paste the next data will be your database user so for the localhost it's a root and for the password we keep it null underscore password and let's this will be a database db underscore database and now guys what is the database we have to create one database so i have just already created one admin panel okay so this is my database right now it's empty so let's copy this database name and paste here done so our database defining is done and yep let's create a database so we are going to create a class guys so let me type database connection dot php file done so make sure you are giving the correct spelling and let's create a class with the same file name that is database connection okay and now guys inside this you are going to start with your database connectivity so let's create that public function function double underscore i mean underscore underscore construct function okay so which will whenever we call this class it will automatically call the connection so let's now let's write the connection in object oriented so mysqli new mysqli where you can mention your the file name okay i mean this app.php define so let's copy this db host user password and database so you just need to copy comma db underscore user then database underscore password first and then your db underscore database all set so now guys let's check if this is connected means you have to give so not symbol dollar co n that is connection if it's not formed if the database is wrong let's give dye over here and inside double quotes let me just print in h1 tag that database connection failed okay and if it is connected then you are going to return dollar this of connection with your dollar con so let's copy and paste all set guys so now we need to check this if it is connected or not and you can go to your app.php and include that database connection over here so let's include once open and close very well database connection dot php file and now you can call your database is equal to new database connection so let's go to our index.php and include that file let's copy this paste here let's go to the config folder you are going to app.php let's save and get back refresh okay so nothing happens but we want to show the message so you can go to your database connection and before that let's echo and that is database connected successfully refresh perfect so you can see here data was connected successfully and if i try to make mistake i mean wrong database so let's give some random database name now refresh you can see that database here you find unknown database okay so you are finding the error but you should not view all these pages so let's go database connection so here we will give one connection let's remove this because we are using object oriented so you can you have to just give connect error okay so now let's let me just comment out this echo and let's go and try refresh perfect so you can get that guys over your database connection failed so now let's go to app.php and give the correct database name that is admin panel and now you can refresh done so you can access your homepage when you find any error inside your database you'll face that database connection failed done guys so this is how we do database connection in php oops so guys let's see next with the registration part how to make a registration in php using oops object oriented programming and now we are going to start with registration system so guys now let's get started so first step guys you have to create a database and a form so first let's begin with creating the form and then we'll move on with the database so let's create one register.php page and yep so let's include our header and footer and our app.php where we have given the database connection okay so yes let's copy everything and paste let me close all the files yep we'll create one row column md6 and justify content center where you can start with the card card header h4 register and i want the card body all set so now guys let's create one div margin bottom three and inside this a simple label where we'll tell first name yep let's create a input dot form hyphen control f o r m it is okay so yep now we have to create the name attribute where we will tell f name so let's copy this and paste let's finish finish it soon this will be the last name and let's give this as lname then email id so let's take this as email and then your password and then guys you can take the confirm password all set so let's change this confirm password and here you keep your password all set so a simple button let's copy div close the div and a submit button so button hit enter so let's tell submit and this button type will be as submit let's give a class where button button hyphen i want to give a primary and let's name this button with the name as register underscore button done guys and now you have to start with the form where this is our total form and start as form tag where you can give the action and method is equal to post yep let's close the form done so now guys let us see the output for this refresh let's click on register here we get our register page all set so now guys i just wanted to show you one a base url so let's go to includes navbar where here is your register.php right so let's control x open php is equal to close php and give a base underscore url function and inside this you are going to paste your base url code i mean register.php so now let's copy this function and go to your app.php okay so you can paste that i mean create a function base url open and close and here you are going to echo the site url dot slug so this log will be your value which you are getting from your base url okay done and this site url we have to create that so let us copy this and paste here let's copy the site url let's create and here you have to call your path so let's go and you can copy this and paste all set let's give a forward slash and end that so this will be a site url dot concatenated and slug yep now you can move to your desk page and go to the login page also so same way you can create for the index also so this is how we are going to create a base url in our object oriented series so let's refresh here you can go on so we don't have a login page no issues we have home page and register page perfect so now let's let's begin with the backend coding part so first step when you click on the submit button the time you are going to form and let's call a path codes where i want this is about the authentication right so login register all comes under authentication so let's tell authentication authentication underscore code dot php file so first step let's create a folder with name codes so let's type codes and inside this code all your module will be okay authentication code.php and now let's include this code.php inside this your register page done as it is included open php and close php and let's begin with the database connection and all so so you need to copy this include app.php inside this and now let's check with the if condition where we will tell a set function dollar underscore post method of the button name so let's copy this button name and paste here all set so now guys you are going to get all your like fname data dollar underscore post method done so same way you are going to call all the form fields lname email password and confirm password okay so let's paste here this will be your l name and then email password done and now we have to validate this input fields so let's create one simple function guys because in this series we are going to use many times of your mysqli underscore real escape string function okay so this function we are will be using again and again so for this case guys we will take this and create a function so let's control x and go to your app.php at your configuration let's still create a function validate input okay and you can just echo this i mean not echo we have to a return okay so return what first your db connection okay so db connection and then obviously your input so let's copy this and send from this function so you will send over here so let's copy this and go to your authentication code and paste here okay so your database connection let's take this first input field inside and now first your database connection so dollar db open co and then give comma so from where is this coming your database connection guys so this is coming from your app config.php so you have to watch out the first part so you have to watch out the part one of this database connection let's go to app you can find this dollar db so which we have placed as global so let's get back and here from this database connection you are getting this so this unn is coming from your database connection over here all set so let's close and same way you're going to call your input fields so let's call this so now we have completed this okay so let's begin with the coding part of your oops concept so let's create an object with the variable as a register is equal to new your register controller okay so in this register controller we will create our code so yep now for this you have to input its class so you have to include that let's include once function where you will go to let we are making a controller right so let's create one folder controllers forward slash and then your register controller dot php all set so let's copy and now you have to create one folder yep guys our outside only will create it let's tell controllers and inside this controllers inside this controller we will create a file with the name register controller dot php done now let's move to our authentication code so now you will be accessing this so let's copy this class name and go to your register controller php close create a class register controller and now you have to give the database connection in this post so to give the database connection you have to include your app.php file so let's include clute include where we'll tell config folder forward slash app.php file all set so now let's create a construct method public function under underscore construct where inside this your database connection so let's create an object of new database connection done or else you can remove directly you can give so not an issue and now dollar this of your conn is equal to database connection of your connection okay so let's get an from this you're getting a database connection and storing here now let's create a function for the registration function so we are going to get all the input fields so how many input fields are there will directly get it so you can find that f name l name email phone and confirm password right so let's get that let's go to register controller fname dollar l name dollar email dollar password okay so for the confirm password we are not going to put here but we will be checking in next function so you can just start your query over here so let's tell a register query is equal to in certain double quotes you have to give insert into your database that is database table okay so table will be users and inside that users where we'll create one fname l name email password that's it okay and then then values open and close where inside single quotes let's give this dollar dollar f name and then inside single quotes your dollar l name so same way you're going to call your email dollar email and then let me just minimize this sidebar comma and then dollar password all done and now let's execute this query so let's create a variable called result is equal to dollar this of your database connection of query method and your register query so once it is success you are going to return this result okay all set and now let me just minimize minimize here for the password and confirm password we have created right so this password and for the confirm password let's create a function so public function confirm password okay so here inside this you are going to pass dollar password comma and your confirm password all set and now inside this you will write directly the code of using if condition else you are going to show so dollar password is double equal to dollar confirm password okay so this your variables so this variable and this variable okay if you're getting confused you can just put c password so let's take and paste here and now inside this if it's correct means you are going to return true else you will return false okay so now one more thing we have to check if user is already registered so at that time what you are going to do that is your user is existing or not so let's create that function also public function where is user exists okay so let's check that so for that guys you you're going to get creator check user is equal to from select email column from the users table okay and give a where condition that email column name inside single quotes you are going to pass dollar email okay so let's pass this variable from your function and where we are going to check the limit of one okay done and yep let's give a dollar result is equal to dollar this let me just copy it this of con and your query guys so let's give your query that query is check user query so let's copy and pass that all done so now we have to check if in this the value exist or not so you can use if condition where we will tell dollar result of number of rows okay is greater than zero so done here you will get your return true else written false done so now guys let's get back to our authentication code.php file and here is our object of register so now first step we are going to check the confirm password okay so let's create one variable called a result of password is equal to your object dollar register and you're going to call the function so let's call that confirm password function done and insert this you are going to send your dollar password variable and then your confirm password variable okay make sure you're getting the correct function so let's copy this function name and paste here done and you are getting a result of true or false so if else condition obviously gives the true or else false condition so you can just put in this way if the password is correct if it is true then you are going to check that you have to enter inside and do the condition if it does not like password does not match then you will redirect to where by showing the message so let's tell password and and confirm password does not match okay and we will redirect back to which page to register.php page okay so this redirect function let's create it in our app.js file so let's create that function redirect open and close here we are going to get the your message comma and then your page so let's tell that as page and let's use header location and we want to show the message also so you are going to use session so let's use session will make us message only and get the dollar message inside double quotes will get it okay and for the location we are going to use location which is the redirect one dollar page okay so but it's not working let's put this in double quotes and send here and after this i want to run the exact function so once it's fine it should go off but this dollar page will like search for index or whatever so we just need to modify this let's take this dollar page over here as redirect to where so this will be your page and then this redirect you are going to send over here and over here you are going to add your site url so yep this will concatenate and give you the exact path you are going for yup and we are using session so make sure you are starting the session at top of your app.php page okay and this page is obviously called in all the pages and codes okay so no issues yep let's get back to our register controller and go back to your authentication code over here so let's check this condition first let's get back refresh okay so you can find that cannot re-declare the base url previously declared on line number 14 okay let's get back so let's go to register.php page so we have already the app.php we are calling that from there and you're going back to authentication oops let's comment this because we are calling that this code in that page itself and let's see in our register controller also okay so not an issue let's comment out this right now and refresh okay fine so now guys let's check for the password and confirm password so i'm going to enter some random digit yeah let's enter wrong password submit okay we didn't get the message why because we have not said that so let's go to register and a simple code guys just create one message.php file to show the message so message.php let's use open php close php user if condition where you can check with the asset function dollar underscore session of your message if this message is given then you're going to echo in h5 tag let's echo what you can give that dollar underscore session of message whatever we have printed and after showing this message i want to just unset that okay so let's unset it save and now let's call this message.php in your register.php file so i just wanted to show above this card open php close php and yep use include function here you can tell message.php set so now let's refresh here you get password and confirm password does not match refresh the message has gone so guys now let's get back to our authentication code perfect so our confirm password is working and now guys once it is success you are going to come inside and start with your user exist or not so let's call our object called register register object where is user exist function where inside this you are going to send your dollar email variable done and this user exist is from your register controller so let's see that over here let's copy this and paste here done so now we are getting a true or false condition inside this so you can tell result of what user so result user let's give in small case so now you can just check it out if it is true you are going to show else you are going to show it different so lets redirect paste here and you can tell already email exists and you can go back to register.php page if user does not exist you are going to start with the registration process so register query is equal to dollar your object register of registration function this registration function is from your register controller so copy this and paste here done and inside this you are going to send your variables dollar f name then next to your l name and then email and then your password guys all set so this is if this is success you are going to check in if condition that is true or false you will get by default from the database so you can just show that copy and paste you can give a else condition over here and paste here so show this as you can tell if it is success registered successfully and you can go back to login page for the login process if it is not then you can tell like something went wrong in your query okay so you can just get back to your register.php file itself to show that something went wrong message so yep guys and now let's create the database which we have not created the table for the users so let's create a table what is the table name we have created over here users let's copy that and we require how many columns so one two three four by default and we need one id increment and one more will create the current timestamp we will create it so let's create a six columns okay so let's get the id and 11 digit and you can move on with the auto increment primary key so yes and let's select with the ai that is auto increment okay and then next field f name so same characters i'm going to give and 191 l name where care191 email varcare191 and then password so make sure you are getting everything correct spelling over there and one extra column we have created for created add which will be the time stamp and by default it should enter the current timestamp guys okay and now let's save the table so yes done our table is created so now guys let's try to register let's get back refresh let's begin with prakash with at gmail gmail.com one two three four five six seven eight one two three four five six seven eight and click to submit submit perfect so you redirected to login page okay so let's get back to register page and we can see that message is registered successfully let's refresh done and now let's see in your database browse cool guys so you can see that we have successfully registered with our php oops concept and now guys let me just register again with the same email id so we can find out our user exist code let's enter the same four five six and now let's submit you can see already email exists okay so you cannot register with that you can just see the code done already email exists so guys we have successfully seen that how to create a registration page in php oops concept okay so guys let's see next how to create a login system in php using oops objected oriented programming and nowadays in this which we are going to do about the login system in php using object oriented programming guys so now let's get started so let's go to our login page and create a simple login form and start with the code so now let's click on this login okay so let's copy this url and create a code guys so let me close all the files done so let's create a login.php and let's call the register code as same thing okay so we are just going to edit from this form itself because we need this email and password only for the login purpose so let's get back let's go to register.php copy the full code if you don't know how to create this register please watch out the previous video so you can find that link given in the description guys so let's paste here and we are going to remove the first name and last name and that confirm password and make this submit login button and change the button name to login button all done so now let's change the login register to login form and let's go top here you go on config app.php and your authentication code.php okay so let's move to login perfect so login form email and password login button yep guys and now when you click on this login button login button you go to form action using post method so we are going to codes authentication.code.php okay so in this form we are going to write the code of this login form so let's go to codes authentication code.php so this was the code for register page let's ignore this let's begin with if condition over here and start to check with the asset function if it is clicked with the post method the button name login button if it is clicked you are going to check that so let's when you click this you are going to get the email and password so let's get this dollar email is equal to your dollar under post method of email guys okay so for the password also let's copy and paste here you check with the password so now guys before getting your data we have to sanitize this right so for the validation like if try if someone tries to inject your sql so to prevent that we have created a validate input function okay so over here your db connection and then your input so let's get here same way you're going to copy so let's copy this and paste here ctrl x and paste let's remove this so now guys from where is this coming you can go to your let's move to login page as you can see that we have included config app.php and your authentication so let's move to app.php file so you have to watch watch out from the part one so you can understand this concept so let's go over here and you can see we have created a validate input where to escape all the injections sql injection so let's call this validate input field in your authentication code okay all done so it's clear and this db connection is your object oriented database connection so let's get back to your app.php where you have included the database connection okay so here is your database connection and that database is called in your object as db so this object dv inside that we are calling the connection okay so all set and now guys let's begin with the code so let's create an object of um auth is equal to new login controller done and so let's create a controller guys for this so let's copy and paste this login controller let's go to controller folder and create a logincontroller.php so where is the controller here and let's create a logincontroller.php and start with the code so let's begin let's create the class with the name login controller open and close bracket where you are going to call your database connection first so you can call the construct method so public function where underscore underscore construct and your database connection so let's call db is equal to new database connection and then guys you are going to call the object in your dollar depth of connection is equal to your object of conn done and now you can start with the user checking so let's create a function public function where user login function so inside this function we are going to pass dollar email email comma dollar password password done and yep let's write the query over here so you can check login query is equal to select star from the user table so let's tell users give a condition where email is equal to insert single quotes tell dollar email okay so this dollar email you are getting over here and for the password let's give and operator and check for the password is equal to insert single quotes let's type dollar password done and your limit will be 1 done guys and now over here you are going to execute this query so before that we have to create a result of this so result is equal to your connection so let's take this connection and your mysqli query function so you are not going to include mysqli only just query function you are going to include okay and let's send this query inside this query done and now you are going to check if a record comes over here we have to set something else also so like let's check with the if condition let's check this if any record exists or not inside this with the help of num rows okay is greater than zero if at least one record comes means you are going to get the data so let's store that in a variable called data and dollar result of your fetch associate fetch associate function done so now you can just you have to get all the data and store into an session so we can manage all the front-end sides to show the user is logged in or not okay so as for that let's create a separate function for it so let's create this user authentication authentication function and inside this user authentication we will check with the session right so let's pass this data inside this user authentication and now let's get outside let me just return true and if it's not means you can just tell else written false all set so let's create a public function not public let's keep it private because we want to use inside only so pub private function that is your user authentication function okay so this user authentication and over here guys you will be starting with your session data so let's create the session open and close where first will be your authenticated if obviously we are going to check if the user exists only then only you are going to check it is authenticated so let's create a variable in the session as authenticated okay authenticated is equal to true so if it is true that means you're logged in and now let's take his role if you have a role you can just get the role also okay so right now we don't have a role over here so we will just comment and keep but in next further videos we will check it out so for that guys you can you are getting the data from here okay which we have passed over here right so that data you will get it so this data you are passing in this function and that data you will get from here so dollar data of your role underscore as if you have it okay so we don't have right now as you can see in our database we don't have that role as so if we have we will keep it so right now we are going to comment that now guys we want the user details so you can mention that authenticated user also so let's create a session variable with the authenticated user okay so let's create a array inside this so we will use an array yep so first will be user id if you required you can store that dollar data of dollar data of your id so this id is nothing but your input field name okay this id f name l name email so in this format let's give comma copy and paste let's tell user fname and here fname copy and paste let's take l name and paste that l name user email and paste your email that's it so now let's get back to our authentication code dot php and start over here at the login code so now guys let's tell dollar check login is equal to dollar auth that is your object of login controller where you are going to check that user login function okay so this user login function is nothing but your function created in your login controller so let's copy this and you have to paste here so inside this you are going to pass the dollar email and dollar password done so if this is success like if it is true you are going to check and redirect to you can give the message logged in successfully and go back to let's give here else you can show the message as invalid email or password invalid email id or password okay on login.php so save guys and now let's check it so yep when you redirect to index or login page you have to show the message right so let's go to login first you have a message so make sure you are having the message at index page also to check that so let's get back to your authentication code if you don't understand what is redirect that is nothing but a function which i have created uh to redirect the page okay so let's get back done and now let's test it so we right now have a way that gmail.com data let's refresh giveaway that gmail.com and now i'm going to give a wrong password so let's login invalid email id or password so let's give a correct email id and password 7 8 let's click login and here you are logged in successfully done guys and now let's click to login and you are able to login the page so guys now let's write the code if you are logged in so it should not be able to access this login page instead you should redirect with the message you are already logged in okay on the home page so let's get back and on the login.php page guys over here we have to write the code so let's go to the codes authentication authentication.php let's go over here and here is our code right so let's take this out so i just want to access this auth variable in our login page okay so from here i want to access that dollar auth of object i want to create a function called is logged in function okay so we will check with this function name so let's copy this and go to our object of auth that is to a login controller let's move to the login controller over here and let's create a function guys so let's move below public function the is logged in function and over here let's check out with the if condition else written false okay so over here guys you will check a set function if this session is set of what your authentication so this authenticated if it is logged in only you're going to check that is logged in or not so let's verify three times triple equal to true if it is success then you are going to show as directly or going to redirect you are not going to call anywhere else also so let's use redirect function so yep and you can show you are already logged in okay and i want to move back to index.php page all set so let's save this and now guys let's see the output for that so yep let's copy this function and go to your is logged in so now let's get back now guys i'm going to refresh you can see you are already logged in let's go to login you cannot move so let's go to register we can go okay so let's copy this code is logged in function and just paste that and this is going to call from your authentication code.php file okay your auth object of login controller refresh you are already logged in done so if you are already logged in you just have to don't show this instead you have to show something else also so you can use this same code or else you can just create a custom one let's move to navbar you can directly check over here let's open php and close php give a if condition check with the set function so same code you are going to copy and paste over here let's move to our login controller and just copy this code so yep paste here done and give colon and open php and php tag and end if and gives semicolon okay so this is how we are going to check if it is authenticated then only you are going to show let's see what happens over here refresh okay we should not show right so just add a else condition php open else colon and tag and you can show the name here you get and instead of login let's give a hashtag over here and show your full name so php open tag and close tag where you can tell copy this paste here auth of user and the f name okay so what is this f name auth user as you if you remember just five minutes of video we have set over here authenticated user user fname user llm okay so auth user fname let's go to our navbar.php auth user of user lname and let's concatenate done let's copy this and show the last name also all set so now with let's try to refresh and see the output perfect so it's showing with why let's give l name save refresh perfect done guys so we have successfully done with our login system in php using object oriented programming guys and now guys we will continue with the logout system so we need a logout over here so guys you just need to go to get bootstrap and copy the drop down menu so let's copy this and paste inside your name so the name where you have showed it all set let's control x and paste here let me just remove this and yep let's remove all this and here in a form method is equal to post and a button that will be logged out so type is equal to submit and name will be log out button okay all set and just design this button using instead we are going to copy this item so make it as drop down item and this will be your something called my profile so which will be seen in further videos so yep no action over here and if you click on this let's go to our all the pages you need to include your action code.php i mean authenticationcode.php so let's copy from the logout i mean login page and paste here so copied and paste now let's get back refresh so you can see that logout button appears over here yep so whenever you click your navbar let's go to earthcode.php and write the code so you can just follow up over here if where is set function dollar underscore post method of your log out so let me just copy from the nav bar copy the button name and paste paste here and now we want to log out all things so now let's create one variable called as checked logged out okay is equal to where you are going to check with the auth of log out okay so we'll create a logout function in your login controller of auth so let's copy this and go back to your login controller and create a function so public function with the function name log out open and close where you are going to unset your session values so let's call unset function where you check the your authenticated unset copy and paste and your authenticated user details so these two variables we are set right so that only we are going to unset it as simple so before unsetting you need to check it is already there or not so you can just confirm from here so let me copy this let's paste here and in the else also you can return false so you can just follow up in this way all set guys so if it authenticated is true then only you're going to unset that and then you're going to return true here you'll return to true so let's get back to your authentication code and here you can just tell if it is true which is successfully set then you can redirect back so let me just copy this redirect code from below and you can tell logged out successfully and you can go to login.php page and then guys now let's check the output refresh now let's click log out and here your message logged out successfully okay let's login once again way that gmail.com three four five six seven eight log in and try to access login.php here you go you are already logged in so guys we have successfully completed how to make our login and logout system in php objected oriented programming language and let's continue going to learn how to make a user authentication or how to check the user authentication in php oops okay so guys now let's get started the first step let's login one two three four five six seven eight login okay logged in successfully and now guys we are going to start with the authentication part so authentication part means like if you're logged in or not logged in you can access the index page but if you go to like your my hyphen profile dot php so at this page you cannot access this page until unless you are logged in so you should check the authentication right so for this case guys we are going to build the authentication part so let's copy this file and go to our project here create a file and now guys we need the header and footer to show that design so let's copy from the index page let's copy and paste and you can show the data as my profile page save and let's run perfect okay so you're on my profile page let's log it now okay and now you can go to my profile dot php even though you're not logged in you can access this page so let's make this authentication so yes let's get back first step guys let's go to our controller and create one authentication controller okay so authentication controller dot php all set so now let's begin with creating the class so class where authentication controller and let's begin with the construct method so public function where underscore underscore construct function or a method and insert this i want to call the um logged in or not so for that guys we'll create one function let's this of check is logged in okay function where we'll create a function with this let's create a private function with checked logged in let's give a if condition over here and inside this we have to check out with not symbol is set open and close dollar underscore session so which we have created in the part 3 video of your login so over here what you're going to check let's go to a login controller and here when we are logging in we are setting this authentication to true so we need to check this if it is logged in only you are going to check that okay so here if it is not set this authentication is not true you are going to redirect to login to access the page okay or access this page you can just mention that and give the page name so that will be login.php okay and let's return false over here and in the else part we are going to return true done okay so now guys we need to call this authentication controller in our my profile so we need to check that it is logged in or not so for that case you are going to call this object so let's call below only so authenticated is equal to new authentication controller so this is called now and now you can go back to your profile and include at this place include underscore once let's call this page inside controller folder your authenticationcontroller.php file that's it and we will take this and import in our auth controller okay so config app.php and now let's get back refresh okay login to access this page now let's go to my hyphen profile you can see that login to access this page let's login with gmail.com password let's login logged in successfully and now let's go to my profile so you can see my profile dot php page perfect so let's get back and here you can see guys one this is our code authentication code dot php file which need to be included in every file again and again so which we saw in previous two parts that is your index page also we are including okay and your login also we are including it and then in your register also we are including right and now in our my profile also we have included so let's cut this and include in our app.php file so which means let's move to config app.php after the database connection let's include that that's it so you can just remove from your folder i mean file that your config code then go to your login and just remove this so you don't require this that's it and in your register also you can remove that let's go to my profile we already removed it so as you can see that we have included config app.php but we did not include in our register page why because we have included that file into our authentication controller so as you can see over here so all the authentication controller will be over here okay so let's get back and now let's refresh everything is working fine go to home page and let's try to log out perfect locked out successfully everything is working fine guys and now let's try to get the logged in user data so let me login once again so you're logged in right if you go to my profile you have to get your profile data right so for that you are going to create in authentication controller only because that is your authenticated user data so let's get here and yep let's create a function so public function where auth detail we will tell okay so first step you are going to check it is logged in or not so you can just copy this or call the function of your check logged in so let's tell check authentication is equal to that logged in if this auth is true it will come in else you can written false yep and once it is correct you are going to get the user id so let's type one dollar user underscore id is equal to your data so from where are you going to get that user id so when we are logging in logging in at that time we are storing that inside the user i mean auth user session variable with the array format that is user id okay so let's get this from this user id will get okay let's call that user underscore id all set so with this help of user id you're going to call the data exact whatever data you require from your database so let's tell get user data is equal to inside double quotes select all the fields so like if you are if you are creating complex table of your user like address respon pin code for number dt etc so you can just get from that so select star from users table where id is equal to insert single quotes you can give your user id limit on and now let's execute this so you need to give your database connection first so first let us create one variable called result is equal to dollar this of your connection and then execution of the query so what is your query get user data so let's copy this and paste here all set so you need to create this database connection you need to import the data so let's create a db is equal to new database connection and let's download this dollar this of connection is equal to db of connection okay we are copying this and we are printing here okay so you need to check that so once let's check if a record exists or not inside this with the help of number of rows so masculine number of rows row w s is greater than zero you are going to check over here else you will redirect something went wrong okay so these are the very rare cases you will get like this like when session expires or something goes wrong on your net so in that case you might get this or else it's very rare case to get this else condition of this and this okay so when you get you are obviously going to login dot php and yes so when your data is success you are going to get the data into a data variable of dollar result you are going to fetch the data from that single record so we can use fetch associate data okay so associate function and now you can just return dollar data so we'll get the data from it and yes so we are going to check this auth details or detail or detail means authenticated user detail or you can keep authenticated user detail in this anyhow okay so auth also is means authenticated user so let's keep a small auth details so now you can copy this very function and get to your profile my profile let me close all the files it's getting confusion so this is my auth controller my profile yep so your included this authentication controller at top and now you can just call that php open php close we need to call the variable so let's call that dollar authenticated is equal to i mean authenticated of your function of detail so how are you getting this authenticated so this authenticated means this variable okay so which i have declared it over here if you want you can just use this one inside your each page or i'll just declare that and remember that you have created authenticated variable after authentication only you're going to come all set so let's keep this in a variable called data is equal to and inside this data you are going to get the data and print over here so let's tell f name let's get back refresh perfect so you are getting the weight data so let's try to keep this horizontal row and h4 tag so first name colon your code let's refresh cool so this is my first name and let's print the last name also okay so let's just concatenate it over here give a space copy and call the last name perfect so this is how we can complete our authentication or a user authentication in php using object oriented so let's copy and paste and here instead of first name let's give full name and now guys you can just print your email id also if you require so email how much ever record if you want you can print it so let me just show you in my database right now we have first name last name email password okay so refresh and now let's tell when this email is created at so you can just copy and paste and created at you can give and call your column field so created at let's save refresh perfect so it's telling over here if you want you can convert this using a datetime function so date function where we will tell as date month half a year and give comma string to time function and pass the variable so yep refresh here is your output so guys in this video we have successfully completed about the check of user authentication and make the user authentication so as we can see that we can check if the user is logged in then only he can access his page so let's try to access my profile hit enter login to access the page so now let's give one two three four five seven eight click login and here logged in successfully let's go to my profile you can see you can access the my profile page so guys in this video that's it we have successfully completed about our user authentication part and let's continue next video with the new topic and that's it in this video guys please subscribe like and share
Info
Channel: Funda Coder
Views: 11,903
Rating: undefined out of 5
Keywords: fundaofwebit, funda coder, Complete login register & logout system with authentication in PHP OOP, php oop login, php oop login/register system, php oop login register, php oop login tutorial, login and registration form in php oop, login and registration form in php, php oop login/register system source code, php oop login logout system with session, login system with session in php oop, php oop login register form using session, php oop login authentication in php, login in oop
Id: WMZrLdCAfKc
Channel Id: undefined
Length: 79min 40sec (4780 seconds)
Published: Sat Nov 20 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.