User Registration System Using PHP And MySQL Database | PHP MySQL Tutorial | Edureka

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] hi guys my name is Aria and I welcome you all to this MySQL and PHP tutorial well in this tutorial I'm not really going to teach you the basics of PHP and MySQL because that's more like a prerequisite for this video in this video I'm gonna teach you how to actually integrate MySQL databases and PHP scripts so that which PHP you can actually insert values into tables or retrieve values form them okay so the best way to learn something in my opinion is always build something using what you are trying to learn so in this case we are using MySQL and PHP today to build a user registration system now user registration systems are very common and popular these days because it allows people to have their own unique user profiles so things like Facebook Netflix reddit or more or less 99% of the websites out there have user registration systems and I'm going to teach you how to create one today using MySQL and PHP so with that said and done I've got a lot to do so let's jump right in okay so now one of the prerequisites for this tutorial is that you need to have samp installed so what exactly is Sam well Sam stands for cross-platform apache mysql or mariadb Perl and PHP so it's basically a very easy to install software and it also installs and multiple software's on your computer just for you so it will install an Apache server it'll install MySQL services and it will also install Perl and PHP on your computer now we will be using PHP and MySQL heavily in this demonstration so I recommend you download zamp to actually set up your local server if you are not using zamp I leave it upon you to however you want to actually localhost your PHP files and set up a server but for this tutorial I'm gonna be just referencing samp now you can download Sam from this site out here which is WWE patchy friends org and you should download the Windows one because I'm using your own Windows but it's also available for Linux and Mac OS ok so I already have zamp installed on my computer as you guys can see out here I've the control panel open now all you need to do is start up your Apache service and start up your MySQL service and we're good to go now we also need to create our folder where we will be putting our code inside the Sam folder so my Sam folder is in my D Drive so let me just head over there okay now it's in zamp and you go into the htdocs folder out here and all you have to do is start creating your files so I already have this file out here called PHP and MySQL integration let me just delete these files that are already there because we'll be creating everything from scratch okay so once that's done let's open up our folder in our favorite text editor so I'm gonna be using sublime text for this demonstration and you can use anything that you want notepad plus plus visual studio code atom text editor are all really cool text editors and you can choose whichever one you want okay so the first thing that we are going to do is we need to create our database first so firstly we are going to be creating a database for this demonstration so to create a database all you have to do once you have your server up and running is go on to localhost and this will give you this dashboard which is the samp dashboard now you can create your database by firing some SQL queries but if you want you can also use the PHP my admin dashboard and this will give you a GUI to create your databases and tables now once you are on the PHP myadmin all you have to do is click new and create your database so I'm going to be calling my database practice and just go ahead and press create now once that's been created you go ahead into practice and we need to create a few tables actually we will be creating only one table but in most industrial situations you'd be dealing with multiple tables because most table in the industries tend to be normalized if you know what that means okay so I'm gonna be calling my table user and it's gonna have four columns so let's go ahead and create that now our first column is gonna be ID it's gonna have all in types variables and it's also gonna be auto incremented so you just go ahead and check mark that and press GO now our next is gonna be user names so it's gonna be username it's gonna be AB type var car and it can have lengths of 255 this can have a length of 11 now we're also going to be having a field called password and email so let's create those so this is gonna be email it's also gonna be around 255 characters long and now we all need is password this is also gonna be Parker and it's also gonna be 255 characters long now all you have to do is save that and your table has been created so you go out here if you click on user you see that it's got nothing inside it so we are gonna be actually putting all the values inside a table using PHP now first of all what we're gonna do inside our PHP is create our pages firstly we will be creating registration page so this is going to be the much easier part of the project okay now let me first of all go ahead and open my folder so you're going to htdocs and my SQL integration so select that folder now the first thing that we're going to create is actually a PHP file which will take care of the registration okay so let's set our file type to PHP out here and let's get started now first of all we are going to be adding in our HTML boilerplate let's give this page your registration title now firstly we need a few inputs so that we can take in the inputs to be actually stored into the database so we need four inputs that is one is the username the second is the email and the third is the password now the fourth type is gonna be the confirm password because in most user registrations you need to input your password twice because that's one of the ways of a 10th occasion so let's go ahead and create that firstly let me create a div now this div is going to have a class called container we are gonna be using this class and all the stuff to actually style our webpage a bit because it should look a little neat now let's give it another div this is gonna have a class called header and it's gonna have an h2 let's put an h2 because each one's are really big for this and we're gonna say register so this is where it tells the user that yeah you're on the registration page okay now let's go ahead and create our phone now our form is gonna take two actions and a method so the action part will decide which script to run when your form is being submitted so we are going to be running this script called registration dot PHP and we will also be using the post method so method will be equals post let me just save this once so htdocs go into PHP and MySQL integration because that's where we are actually storing all this so let me just name it registration dot PHP so yep that's right spelling okay so now once that's done all we need to do is create some labels and some inputs so let's say let's put all labels and inputs in separate divs now let's create label and first label is going to input a username now let's create an input so this input will be of type text and its name is going to be username okay so once that's done we could also say that this label is for username okay so let me just copy that down now the second type of input is going to be email and this is also gonna be of type email and the name will also be email the next type is going to be password so the label is going to be for password okay let me just change this to email here then we password and this is also gonna say password let me just meet leave Inc them once then the fourth one is gonna be so let's call this password one and this is also going to be of type password and let's call this password do let's save that okay so in the end we are also be needing a button so this button is gonna be of type equals submit and it's also going to say submit and in this one we want to say confirm password so let me just save that now let's see what this looks like so see what this looks like all you have to do is let me just close out these tops I'm gonna localhost / my SQL PHP into creation okay so I'm just gonna change the name off my folder okay let's see does this work PHP my SQL integration okay that's object now found so let me just change this so let's rename this to demo and all you have to do is go to demo slash registration dot PHP okay so this is what our form looks like at this moment it doesn't really have a lot of styling or anything attached to it because we are just building the basic structure of everything now we will also have a sign-up page so let me just add something quickly so this will say already a user and we are gonna put an H ref a tag rather so this is going to go to login dot PHP because we are going to be creating a file called login dot PHP and it'll say login now let's make this bold so make this bold just use a P tags so since I changed the folder name this is having some problems saving so let's go to demo and you save it you're gonna replace it yep so that's done now so let's save it and now it also has is already a user and login but if we click on login we don't really have a login dot PHP so let's go ahead and create that first so we are gonna be making a new PHP file Spurs PHP and it's gonna be very similar to this let me just copy a bit of the code and it's gonna say username and we don't really need the email and I'm gonna be using that and it's gonna be using user names for actually logging in we also don't need to confirm the password and out here I'll say login and so we are also be going to referencing the registration page so for that will be just redirecting to the registration dot PHP page now in this action out here we are going to be using login dot PHP could save this and this is going to be our login dot PHP script and this is not an HTML file we are gonna be saving it as PHP so yeah this is gonna be our general login page so let me save that and let's see if that's working now so if you go to login now we have a login page okay it says register out there so you fix that and say log in save that let's reload and it should say login now yep okay so another thing we need to add some names to our button because we need to be selecting our button in our server logic so let's give this name so register user and out here in the login page our name for our button is going to be it's gonna be login okay so now you've set it up so our both basic pages have now been set up so one is the login page and the other is the registration page now it's time that we create the logic for registering our new users into the database so another thing I actually forgot to do out here is saying that all these fields are required so else they would throw errors if we just didn't do that so required just leave them as required so let's put these here to just make them required let's do the same in the login page so all you say is now required here to save that let's say that - now if you go out here and you just try to submit it'll say you need to fill in this field so for that we need that required but right now let's create a new file let's make this type of PHP and let's save this and we're gonna call this server just open up your PHP tags now we're just going to be building up on our logic so first things that we're gonna do is firstly we're gonna start up our session then we're gonna initialize a few variables then we're gonna actually connect to the database and then we're gonna write the logic for registering the user onto the database okay so let's get started so firstly to start up your session all you have to go and say is session underscore start that will start up your session and every time you log into a web page you are actually being tracked and that tracking period is called a session okay so now we're gonna initialize available so let's put in some comments so initializing variables so our first variable is gonna be a username and it's gonna be empty and the second one is gonna be an email now once that's done we are gonna try and now connect to the database so to connect to the database we are gonna be using this function called MySQL I connect so my SQL I connect yep and all you have to do is now specify the IP address first of all where your database is so ours is hosted locally so you all you have to say is localhost then you have to specify the user you are going to login with so that is route next is the passwords but I don't have any password so all I'm gonna say is blank and next what do you want to specify is the database that you want to target so let's go ahead and check our database name so our database name is called practice so you're gonna target the practice database now you also want to check if your connection was successful so you could do this easily by saying or now you could just give the session by saying die and you could print out a message you could not connect to database okay now once this is done we are going to try and register our users so this part of the code is just gonna be for registering users so first of all we need to retrieve whatever is being input in the form using PHP so we have actually four input so we need four variables so the first one that we did is username so to receive our values from the form we are going to be using this function called MySQL real escape string so what it does is actually it's caps all the escape characters that can cause security issues and give you back the value from there was form so let me just show you how that's done so MySQL I and you say real escape and string so that's going to be our function so the first parameter it takes is the connection that you made with the database so it's stored in this variable called DP and next is the variable that you are trying to access so let me show you how that's done so it's basically we going to store in the post array and we had also named our username with the name of username if you see the spot it's gonna be named with username so that's how you select it so it's going to be called username so that's their next is gonna be the email so you first select the email so again we're gonna be using the same password repeat so again we're gonna be using the same function so let me just copy paste that this is gonna be email now they're gonna be two others like these so let me just copy these both down and it's gonna be password one and this is gonna be passed for two and these are gonna be what did I name them oh yeah I name the password one password two okay so let's save that now okay so now once we have all the usernames and passwords and variables to be actually played around with it's time to make the user registration or the form validation logic so first of all we are going to be tackling some errors so suppose somebody doesn't actually input their username so for that we want to have an errors array so let's just create that quickly so you go error and you may get an error so this is gonna be form validation now first of all we need a few if statements so let's say if we have an empty user name all you want to do is say Ari push so we want to put it into the error sorry first of all so error we want to say that user name is required now this needs to be done for all the things so firstly for the email and then for the password let's not forget our semicolons yeah so out here we want to say if email is empty and next if password is empty or rather if password one is empty because that's what we're gonna be checking so first of all we say we're gonna say email is required and we're also gonna say something like password is required now we also want to check if password one is equals to password two that is when your validation is gonna take place so let's quickly do that so we have them stored in these variables called password one and password - so if password one equals two equals two rather if since we're doing it for errors they're on equals two so password two polygons say is you want to push another error into an array so this is gonna be the error sorry again errors saying passwords need to be the same okay so now we have our errors our is setup now once we have our error set up what we want to check next is if the person who's trying to sign up is already trying to use a username that already is existing in the database so we want all users to have unique usernames so for that we're gonna check if the username that is currently being inputted already exists in our database so we can do that very easily so let me just show you how so first of all let me just define or let me just comment out once so what are we trying to check DB for existing user the same username right firstly let's set up a query to check this out so user check query so this is gonna be select star or that means all prom over the name of the table it was user so select all from user pher username equals two now this has to be in single quotes so what about variable we have stored in so dollar sign username or if somebody is already with same email so let's say email equals two and we go dollar sign email now we also want to limit this to one because only one instance is enough to let us know that this is not a valid username or email so limit one so that save that so now that we have set up all our variables to actually register a user into our database and then we've also made the errors array which will help us check if the user is doing anything wrong during form validation now all you need to do is check the database for any existing user with the same user name because we want the usernames to be unique and also the email ids that are being used for signing up so we do that by actually setting up a query checkup so in that we will actually try and retrieve all the user names and if anything matches we are gonna try and negate that and push an arrow saying that the username already exists so how do we do that well I've already set up this user check query now we have set the limit to one also so let me say results will be my sqli query and then we're gonna use it on the database and what query are we passing we're just passing user check query so this will actually fetch us all the username that might match in this so it will fetch us all the Association as the function name says what you wanna say if the user exists that means if that's true and if the user from the username so it's users are re so we want to check if its existing to the username that is being provided so this one out here so we're checking with that so we want to push our error into the errors array so what we wanna say is username already exists you know let's do the same thing for emails also so let me just copy that down and come down and paste it so if users change the email change this to email so why do I say this email ID has already been used okay so that's what we want to say if our email returns match of similarity now if it passes all this we can finally register the user if there are no errors in the form so register the user if no error so what we're gonna say if error sorry has nothing in it so hurry count or does it just count okay it's just count and I'm gonna say error so if that is zero what you wanna do is run a query now now before you store a password in a database it is very much advised that you encrypt your password so because in many cases someone might maliciously gain access to your database and then he can just view all the passwords as plain text so that shouldn't really happen now in PHP we can use the md5 function to actually encrypt our passwords with the md5 hashing algorithm so this is how you do it so we take our password okay so this is a new value called password and what we want to do is md5 and we won give password one and say that so let me just put a comment and this will encrypt password I'll show you what I mean when we actually register a user after we are done with the registration logic so all you want to do is now setup a query which says so insert into user that's the name of our database insert into user and now we want to tell them what we insert so user name then email and then password and now we need to pass in the value so you go values and in brackets with single quotes the name of hi beautiful so first you will pass in username then we want to pass in the email and then we're gonna pass and hashed password so which is the variable password now it is very important that you maintain the order because it's username email and password so you have to do user name email and password you can't do it in some other order so that's close off our coats and with our semicolon our query has been set up now all we need to do is MySQL I that's how you run your queries MySQL I and query then you want to pass in where that is DB and what that is our query okay so that will actually run the query on the database now all you want to do is say session because meter close off our session so our session will begin with our user names and this will also happen for success so we want to say success and let's say you are now logged in so whenever somebody actually registers we're gonna lock them in automatically and we're gonna actually head them to another page called index dot PHP which are we creating soon so all you say is head and then you pass in the location as a key value pair the location is going to be index.php okay so dad has successfully setup our registration so to just show you guys let me just create our index dot PHP really quickly okay so our index dot PHP will also have some logic in it that somebody who's not logged in will not be able to see it because it's it's that's the whole point of user registration or reuse the authentication system where your email and password is checked before you can actually see a certain page for this let's first set our language to PHP now just open up the PHP tags and now let's build our logic so first last session started and what you want to say if you can use this set function to see if the username is set so we go session and you know check for the particular username you use the name yep so that returns true you want to say that you're gonna print a message that says you must login to view this page you must login first to view this page so that's what we want to print out and then we can just head him over to the login page so you can go location login dot PHP let's not forget the double quotes and then we also want to check if he's trying to log out right so if is said you want to use the get method I'm gonna pass and logout so when somebody logs out all you have to do is destroy their session so you can do that with session destroy' and you can unset everything in the session sorry so that can be done with just passing in the sessions are a and with the username and we can again redirect him to login page okay now that sets of logic that we need to just make our index login only so if you're logged in then and we'll be able to see it now let's just add some HTML to this so you go HTML let's get the title of home page come in here let's say let's make an h1 so this is home page you can also write some PHP here to make it more personal so let's see if see this at function you wanna say if he's logged in so if it's a successful session so only if the session is successfully established well the below HTML be loaded so let me just show you how that happens if you're a colon then you start writing your normal HTML so you go do let's put a div inside this div and let's put a couple of entries and inside this you're gonna write a PHP it's a PHP you want to echo out that it's been a successful session so you got all the sign and session and you want to say success and all you have to do is unset it now you want to also see if so now we also want to print out the logged in user information so that's if the user logs in friend information so if said I want to say is user name yeah so that's all y'all print out and we also have to create a logout button so let's see button now we need to link it so you say index.php and you are logged out and that is gonna be true you can also give this a little bit of styling so let's say let's leave out the styling for now so we just make a button and let's end our div so all we want to say is PHP and if so that completes our index dot PHP so let me just save that let's call this index dot PHP okay so now that we've set up our index dot PHP all we need to do firstly is include our servers logic into our registration page for that is to say PHP can include server is that how we spell - yep server dot PHP and we want to do the same thing in our login page so let's copy that down because we will soon be adding our logic of logging in which is much easier than registration and all we now need to do is create an errors file so firstly let's see so we're creating this errors file so that we can include it before checking some stuff so if the error count comes back to be zero then only will we proceed with some action so this would be clearer just in a moment so we wanna say if count of the errors so counter ferrars is greater than zero so what do we wanna do you wanna say a printout of Dave and say okay so there are multiple errors right so we say for each error so out here we are gonna check out the air so he tags for that we need to colon again sorry I did that mistake so you go question Bob and you end the tags then you want to say echo out the error also end that and then you want to end your P tag and then end for each other so how do you do that you just say PHP and for each all you have to do now is just tell PHP that this is the end of the if statement so again PHP and okay so let's save that now so we're gonna call this errors dot PHP now let's head over to our registration page once so out here also we want to include errors or PHP before he starts in putting anything into the form so the first thing after the form begins we're going to say PHP include dot php' save that okay so now that we've set up our errors file our index file our servers and we've included them in the right places now it's time and actually try and register a new user into the database okay okay so let's try that out so our username is gonna be Aria our email is gonna be wanted to reactivate XYZ our password is gonna be one two three let's confirm that okay the server encounter and was unable to complete a request yes okay so seems have we've encountered an error but our user should actually have been registered because it had gone to the index as PHP so let's see yep so we have our first user out here and you can see that I user name is Arya the email is one two three at the rate xyz.com and our password has also been set up so we have successfully connected our PHP registration page and our server logic there seems to be some error I'll get to it right now okay so our registration part is done so let's get to the login part first okay so now that we've created the logic for registering our new users into our database successfully because as you guys can see out here we have a user called Arya and I also added a user called sati they both have the same password so the harsh looks the same but they have unique email ids they have unique user names so these are just two new users in our database so now what we need to do create the logic so that somebody can log in so login user so let's see now so first what we want to check is let's see if it's that now you want to see in the post array login user so our button was called login user out here if you go and check so our button out here has the name login user so that's what we're trying to retrieve out here so if that has been set we want to check a few things firstly you want get to user name so we do that if you remember we had a real escape string thing MySQL a real escape string and we want to take in from the DP and we also want to post so you want to get the username I forgot the single quotes use name now we also want to get the password so let's copy that down name this password and also out here this is gonna be password so was that called password out here it's called password one so we try and retrieve password one it was called password one and usually nice so that's retrieved now you wanna also do some common error checking so if empty so if our username is empty you wanna say if you wanna push another error into our arrays called error so re push so this is called errors and you wanna say username is required maybe you've already actually handled that they're just putting required or but this is some additional extra checking for more security so it's never bad to be just double sure about everything no you also want to check the same thing for the password so let's see let's copy this down now say if the password is empty you want to push another error into our array and say password is required now if this is actually successfully passed so if neither username nor password is empty we want to check if our error count in the errors array is zero so count and errors is equals to equal to zero and you want to check the password with the existing password so firstly we have to hash this password also that is being passed in because our passwords in the database are actually stored in a hash format so again what we're gonna do is say password is going to be md5 and we want to pass in password now we have to set up a query to actually check so now we're going to be selecting from the database so you're going to be using the Select keyword select so we want to select everything from the user table there the username is let's say the username the equals to whatever username that we have retrieved and password also has to be equal so password equals the password we just hashed and found out so once that has been set up we need to fire this query on to our database so again we need our results so this is gonna be my sqli query so we wanna fire query you weren't Farah query on the database link that we had created at the start of this all and we want to fire the query keyboard which is yeah not keyword so you want to fire the query then stored in the query variable so yeah that's how you do it so now what we want to do is see the results so you go that with my SQLite number results and you want to pass in the results of whatever we get from the query so if that is true you want to start our session which has been started now we want to just pass in some stuff so in the username where we equals to the username and we also want to boss in the success so you say success an equals two you wanna say you are now logged in or something like that so it's passion string so logged in successfully and now you want to just redirect them to the index page so again this is just going to be a key value pair you know say location is index dot PHP so those are single quotes and if this doesn't happen you want to say else you want to say that they have entered the wrong use of name and password so re push it's going to go into the errors re and you want to say wrong use the name slash password combination please try okay so that wraps up our login logic so now let's try and login so let's go here first so login okay so that seems to be a syntax error outline 68 okay I forgot put a semicolon here and a semicolon here save that three don't okay line 88 so yep forgot a semicolon here any other semicolons I'm forgetting line 94 unexpected so that should just delete that out save see okay so a login was actually happening so we had set up a user called Aria with the password of one two three okay undefined index password one online 63 okay I spelled password wrong so let's save that it's also called password here down okay so let's try that again now are and one two three cause one defined function my CLI nom results include that saying throw okay so I did some error there so light 81 let's see okay this is my SQLite numrows my bad and let's try it login again so one two three yep so now we have set up our login successfully so you see homepage and logged in successfully welcome Arya and if you click this logout button it'll also log herself so we can try and register some other people so let's see let's call a new user named Annie your email ID is sunna and her a gmail.com password is our no birds and birds let's submit that okay so we have logged in a new user called Annie and that's registered her and now we can lock her out also so out here if you go again and check we have a new user called Annie with new password that is hashed and this is called Sinatra gmail.com okay so that wraps it up for our demo today so what did we learn we learn to create a user registration system we have a server store PHP that actually handles the logic for registering a new user and logging in a new user when logged in he also created the login page we also created the registration page now you can also create some CSS to make this all look very neat and tidy but I'm not really gonna do that because that was not the point of this video this videos point was teach you how to connect MySQL database with some PHP scripts and we have successfully done that we first created our database then we created our registration page and we created the login page then we actually proceeded to create the logic for registering a new user properly with proper form validation and we created the index page then entry to the login page logic and we've also created an errors file that will actually check for any sort of error that is happening so in my opinion this was a pretty good exercise for actually practicing your most mice Kuehl skills and your PHP skills so that sums up our video for today and I hope you enjoyed this video as much as I liked making it until next time goodbye I hope you have enjoyed listening to this video please be kind enough to like it and you can comment any of your doubts and queries and we will reply them at the earliest do look out for more videos in our playlist and subscribe to Ed Eureka channel to learn more happy learning
Info
Channel: edureka!
Views: 430,798
Rating: undefined out of 5
Keywords: yt:cc=on, User Authentication System using PHP, PHP MYSQL integration, PHP database integration, Registration System, User Registration, PHP Encryption, PHP Tutorial, MySql Tutorial, Error Handling, PHP Form Validation, PHP Authentication, login in PHP, PHP login form, php mysql, php mysql project, php mysql tutorial for beginners full, user registration system using php and mysql database, user registration using php and mysql, user registration system, php edureka, edureka
Id: qjwc8ScTHnY
Channel Id: undefined
Length: 45min 24sec (2724 seconds)
Published: Wed Nov 14 2018
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.