User And Admin Login System In PHP Mysql Step By Step | PHP Tutorial For Beginners (2021)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
welcome to webtech knowledge this is yamin and in this tutorial i'm going to show you how we can create a admin and user login in php now i'll start from the very basics so that you can understand it uh really clearly okay so first of all uh we are going to locally see uh then zamp then htdocs and inside this htdocs we are going to create a uh folder okay so let's create a folder let's say our folder name will be project okay so in this tutorial i'm going to use uh sublime text you can use notepad plus plus or any editor you want so first of all we will open the folder so click file then open folder then uh htdocs and here you can see our project folder that we created okay now just click select folder and over here uh let's create a login dot php ctrl s then uh htdocs then project will save it over here so save so here you can see login.php okay so first of all we are going to create a login view a very simple login view okay so let's over here sorry html html and inside this first of all uh let's see over here div and over here first of all we will say level level will be username username and we need the input field so we will server here uh input type input type will be text and name is equal to [Music] username and it will be required require means a user must pull this input field okay otherwise user cannot submit okay after username will uh we need password field okay so over here we will say password and for password input type will be password and name will be password and after that we just need a login option okay so let's for login we don't need level then also we don't need to require a name field for login okay so the input type will be submit and the button name button value will be login okay and let's have everything in the center so i'll see over here uh center i'll just cut it from here ctrl x and paste it over here okay so let's save it ctrl s now uh one more thing uh you need to have your uh stamp running okay just make it sure i will show our hair localhost and here you can see that our project name is uh project okay folder name is project so we'll show our here project project and our file name is login so just press enter so here you can see username and login okay so i'm just going to make some a little changes so that it looks a little better so uh i'll say over here another div and let's cut it ctrl x ctrl v now we'll serve her in this tip uh style inside style with background color it will be gray and with let's say width will be 500 px okay so let's save it so it will uh add a background color with five with 500 px so let's refresh over here so here you can see it looks like this now we are going to add some br just to create some gap so let's over here br br actually create a gap okay uh ctrl v and after this another pr okay and let's also just say over here uh inside h on tag we'll say uh login form okay so let's save it now if we refresh now it will look like this okay let's add appear over here as well s okay now it looks better okay now we are going to uh do the database part so now we'll create a database and a table for a user to login user and admin to login okay so first of all we will go to our phpmyadmin then click new and over here we'll say our database name let's our database name will be user you can name it anything you want so let's click create so here you can see a user database is created so we are going to uh create a table inside this database so let's say our table name will be login okay and we'll have total of four column okay it will be first to be id then name username password and the third one will be user type or user role okay now click go it will create a login table so the first one will be id second one will be user name third one will be password and photon will be less user type user type means easy to uh user or admin okay so id will be integer username will be parker password will be varchar and user type will be barker as well okay but user type will have a default value default value will be let's say by default its value always will be user and we also need to give the length and we will have id as primary key so we'll just show our click this ai means auto increment here you can see it's become a primary key so everything is set so i will just mention user type again user type will be by default always user okay so whenever you insert a data it will be by default user so let's click save okay our table is created now insert some value over there so let's over here first one will be let's say admin 1234 now here you can see that by default its value is user if you click go and now let's click login so here you can see uh you admin username password and by default username is user okay so now let's input another field this one will be for user there's a password one two three four and it's user okay click go okay that table is inserted now click login again now we are going to uh change the user type for admin so click edit for admin user type will be admin okay now click go so here you can see that uh the user type for admin is changed okay now let's go back to our code and let's do the backend code for our database so uh so we write this uh username and password okay and we want to we need to have this inside a form size over here form here our form will start and our form will end over here after submit okay so and we our form will have some action and method okay method will be post since uh will be uh okay and the action will be hash now why hash because uh we will do the authentication part or the or the validation part in the same place if you want to do the validation but on on the other page then you just have to give the uh validation page name over here okay since we are going to do it on the same page so that's why we will use hash so let's write the ph for writing php you just need to do this okay now you can we can write php uh inside this okay so first of all we need to connect uh our page to the database okay so for this uh we need to save our hair dollar host is equal to two second bracket and then inside it will say localhost okay after host we need to we'll see our dollar user equal to uh we is over here root okay after do user we'll say we need to say password password will be always null okay so just to uh second quotation uh two quotation now after this the last thing which is our database name so we'll say dollar dv is equal to what is our database name our database name was user so i'll just show you here you can see our database name is user that we created so we need to see over here user okay so after this uh for connecting to the database we just need to declare a variable dollar let's say you can declare any variable you want i am declaring dollar data is equal to my sql i connect mysql li connect if you are using pc version 7 or 8 or higher you have to use uh mysql i okay but if you are using pc version less than seven or eight then uh you can just write mysql connect okay so inside this uh since we are using ps version seven and eight uh higher than seven and eight so we need to use mysqli okay just uh just to tell you okay now inside did we we need to write those variable okay so we'll say our dollar host then comma again dollar user after this dollar password okay so let's save it so right now uh our database is connected so but if you want to check it if our database is actually connected or not then you can just write a if condition over here and inside it will just say dollar data triple is equal to let's say false it means uh if our database is not connected sorry second it will be second bracket is not connected then it will show us a error let's say die uh connection abroad okay connection uh error let's say just say error okay ctrl s now if we refresh uh we shouldn't see any error because our database is connected successfully but let's say uh you change something over here db name user 12 now we don't have this database so if we refresh right now it will show us this zero connection error that uh database name not match unknown okay so let's just remove this 12 so let's save it so our database is successfully connected so right now uh we will write the condition for login okay for user and admin login now if we say a condition if dollar underscore server then third bracket then to double quotation and over here we say request request request underscore method okay then equal equal to double quotation post now write everything in the capital letter p because if you write it uh in smaller letter then it might throw you a error okay so that's why you better write it in capital now give two second bracket now over here we will catch uh declare a variable let's say username you can declare any variable you want is equal to dollar underscore post then two third bracket and inside the start bracket here you can see that i declare variable username so over here you can see our name is equal to username so you just have to keep this name uh over here exactly like it okay so username okay after username we'll declare variable dollar password is equal to dollar underscore post then the same way here you can see that our name is password but if our name is pass then you have to give it over here pass okay but i'll just keep it password the way it does because it's easy to understand okay so we are receiving the uh username and password from here okay so after this uh we we need to write sql uh sql is equal to we write a qd so inside we need to give two double correlation and our here we'll say select uh select star form what is our table name our table name was uh actually login right our table name was login here you can see login so we need to see over here select star from login higher we say username is equal to now over here you can see this username is actually coming from our database uh from this username okay username is equal to then is equal to two single quotation and inside this uh single quotation we need to write double quotation we need to write two double quotations so i just so okay now over there after that double quotation we need to add two dot over here now inside this two dot we need to write this dollar username so i'll just copy it and paste it over here okay so after this we need to write after username we need to write and for password okay so i'll just copy it from here ctrl c and paste it over here and just change the username to password password is equal to password i will uh reply people it again uh this password is actually coming from database and this password is this one okay so that's all after this uh we'll just verify it uh if this username and password exist uh it's true or not so we'll just sell a dollar result is equal to my sqli underscore qd then inside this query or we need to write uh this data this dollar data so i'll just copy it and paste over here comma this sql ctrl c ctrl v okay after this uh we will declare another data which is dollar row is equal to my sql i page fetch array underscore array a doubler a y array okay now inside this array first bracket uh we we need to say dollar reason so i'll just copy it and paste it over here dollar result okay now we just need to write a condition over here if uh dollar row then sorry it will be third bracket and inside the start bracket inside this double quotation will say user type user type is equal to is double is equal to then uh two again two double quotation then over here we'll say let's say uh user okay so now we'll say this second bracket now this user type is coming from this database from here user type so if the user type is user let's say echo uh user okay now after this i'll just copy it and paste over here now over here user type if the user type is admin then it will print admin okay so it will be elsif after this if nothing is right then uh it will just say over here i'll just copy it it will just say a username or username or password incorrect correct okay so let's save it control s now let's try to log in let's refresh now let's click admin then on two three four now click login so here you can see admin now let's try for user let's click use write user then on 234 then click login so here you can see login so let's make some changes so instead of printing we will send them to another page uh this one will uh send a user to user page and this one will send send it to admin page okay so for user let's say over here header header then after this uh we need to write two double quotation and insert this double quotation will say location uh it will be let's say user home dot php now we don't have this user home so we have to create it now i'll just copy it and paste it for admin and for admin it will be admin home admin home okay so let's save it now we have to create this those two pages so i'll just click right click on the project then new file and over here let's say user home dot php okay let's save it control s save now over here uh we'll say html and inside this body uh we'll just say inside h on tag this is user home page okay so let's save it and let's create another page for admin home so let's over here admin home dot php so let's save it ctrl s save so i'll just copy this one ctrl a ctrl c and paste it over here so over here we just said this is admin home page so let's save it control s okay now let's try to login to admin 1234 click login so here you can see this is admin homepage so now uh let's try for user let's one two three four login so here you can see uh this is user home page okay so let's uh so after login user should be able to see their name and then log out option so let's add those option so for it uh we need to use session so we just so over here uh session uh session underscore start okay now we'll come over here this con over here and then we'll just say dollar underscore session then we need to declare a variable sorry we'll just over here to double quotation and inside this double quotation we'll just say username is equal to dollar username now this username is actually coming from here okay okay after this uh i'll just copy it ctrl c and just paste it for admin okay now we just need to go to user home and over here we just need uh first of all we need to add session for user home so we need to write this php okay inside this uh we'll say session uh session start underscore start okay now we'll just show our hair php then echo echo and i'll just copy this part dollar underscore session username okay ctrl c and ctrl v okay so i'll just uh do the same for admin so i'll just copy it and paste it to the admin home as well ctrl s and ctrl s okay and we have to add session over here so let me just copy it and paste it okay uh let's just save it ctrl s now let's go to our browser let's try to login for admin let's click login so here you can see this is admin homepage admin this is the username okay now let's try to login for user let's say user on 234 click login here you can see user now let's add a uh let's add a uh log out option for them so we just add a logout option over here so that user and admin can log out so for this uh we'll just serve here anchor tag so we'll serve here log out dot php and we start here log out now uh i'll just copy this one and paste it to user home now we don't have this logout.php so i'll just create a logout.php so what will happen is that if someone click logout then it will take us to this page logout.php so over here so over here we'll just write some php code so we'll just say this okay and over here we'll just say session wsion session start underscore start and then we'll destroy this session session destroy and then we will after log out we will send the user to header uh to the location location key login dot php will send the user to login.php okay and one more thing okay we need to give double quotation over here ctrl v ctrl s now let's try to refresh over here okay it doesn't show me probably i didn't save it for admin ctrl s okay if i refresh here you can see logout option if you click log out it will take us to home page now let's try it for user on 234 click login here you can see user homepage click logout okay now let's try to go to let's say admin home and press enter so here you can see that we can actually go to admin homepage but this is not right so we will use session over here so that uh people cannot go to admin home page without login so this is very easy we just need to add an online code over here uh okay we just need to say a condition if then uh not is set then first bracket and over here we'll say dollar underscore session s e sorry double s i o n session then uh username okay then it will uh header i'll just explain this line don't worry header and the header i'll just copy this from here get a location ctrl c just to save some time okay ctrl v so here you can see that if uh there is no username then it will always send us to location uh login.php okay so people cannot uh come to this space without login so i'll just copy this and paste it for username ctrl v ctrl s now let's try to refresh over here and you can see that it took us to the login home login page if you drive over here user home it will always take us to login.php but we can actually go to that base when login click login so you can see we can go to user home page and let's try for admin can we go to admin page after login yes we can so that's all about how you can login with user and admin in phd mysql so i hope i was able to help you uh this student was a little bit longer but because i tried to explain everything so that you can understand it really easily everything is clear to you so i hope i also want to help and if you are satisfied with this tutorial then please subscribe my channel and if you have any problem then leave a comment i will surely surely reply back and thanks again for watching you
Info
Channel: Web Tech Knowledge
Views: 25,477
Rating: undefined out of 5
Keywords: single login page for admin and user in php, multi user role based login in php with mysql, admin and user login in php mysql, php login different user levels, admin login php mysql, login multi user, multi user login php mysql, multi user login html code, multiple user role based login in php and mysql, multi user role based application php & mysql, make multi user role in php mysql, php and mysql in multi user login, multi role user login system php mysqli
Id: wODW8RLBPt0
Channel Id: undefined
Length: 33min 14sec (1994 seconds)
Published: Thu Apr 29 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.