Complete Login and Signup System with PHP and MySQL

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
so hello everyone to this new video and this video is going to be the second part for the single page login and sign up video that I've made so go check that out as well so it is this one login and sign up page so this if you haven't seen this video of creating this login and sign up UI that go check that out as well and in this video I'm going to show you how you can write the backend part for this login and sign up page so I'm going to use PHP for getting the data from the user and then sending the data to the on database and storing that and for that we are going to use my sequel so as we are going to go through the backend coding so we need a server so in this video I'm going to use zamp as the local server so this is the Sam control panel and if you haven't installed Sam on your system then I'll give the link in the description down below so go there and download this and install it on your system so after installing the Sam so if you have installed it on the C rail then you are going to see a X am ppz folder so go into that folder and you have to go into the HD docs folder and here you have to paste all the files that you have for that project so these are my files and in and when you have freshly installed this Sam then there will be already some files that are going to be there so copy them and if you want to store them somewhere install them or you can straight away till it them because you will not need then in this video or after this video also you won't require those files so if you want to delete those files then you can do that or you can store them somewhere else then do that as well and copy all your project files here so that we can run those so now I have copied these files here and let's get started with the coding part okay so in this video I am going to use Visual Studio code for this project and if you have installed Visual Studio code and you want to do this PHP coding then if you have installed any of the PHP extensions then they are not going to work so for them to work you have to install PHP and as we have installed zamp so it comes with PHP so what you have to do is you have to go into that Sam folder so this is the same folder and search for the PHP folder that is this click on it and you have this path here so copy this path you will have to set this path in the environment variable so right click on this PC go into properties and then click on this advanced system settings and then here click on environment variables and here select this path and click on edit and here you will have to create new and you will have to paste that so as you can see I have already pasted here and if you want to see if the path is set properly then you will just need to go into the command prompt and then right here PHP space dash V and it is going to spit out the version for the PHP that is installed on your system so this is the PHP so it's working fine and your extensions that you have on Visual Studio code they will also fine ok so now let's start with the project so this is the htdocs folder so these are all the files that I have in this htdocs folder so now first of all I'm going to go through this so if you have seen that UI design video for login and sign up page then you will notice that there are lot of new files and folders here so this is a one folder that I have created that is new that wasn't there in that video and all those PHP files also were not there in that video so I have created those so first of all let's start with the source folder so in this source folder I have two PHP files one is DB and scope connect and another is session so let's first start with the DB underscore connect dot PHP file ok so now let's start with this file so first of all we are going to write the basic PHP syntax and then first we are going to create a variable named username and we will assign it root so to access the database in my admin we have to use the default username that is root and if you have created any other username then you can write that here as well and then the password so I haven't said any password so I am going to keep it blank and then comes this long string so what this is I'm going to tell you so this is required to connect to the database so we have specified the hostname for the MySQL database so that is the localhost and then there is the name of the database so if you have created any other name then you have to specify that name here so DB name is equal to my database so the name of the database that I'm going to use is my DB and then after we are going to write a try block and in the try block I have created con named variable and I have specified all these three variables that I have created in this video so we are going to use video for getting the data and storing the data into the database so PDO stands for PHP data object so I am going to use video and and here with this statement I have set the error for the PDO and then after the catch statement and here I just wanna print our message with or whatever the error that is so with this e variable that I have created that contains all the error messages so I am going to get that using this get message method and they are going to be printed on the screen if there is any error in connecting to the database so that's it for this file let's go on to the next file okay so the next file is session dot PHP so in this session dot PHP we are just going to start the session and we are going to include this file in the PHP files that we have created okay so now we are done with the source folder now let's start with the signup file first so sign up dot PHP okay so in seinem dot PHP let's start with this first line so this is the required ones so here we are giving the path for our DB and go connect dot PHP so now we have two different PHP files for sign up and for login so to not repeatedly write this connection statements that we just saw in the DB on Disko connect dot PHP file so that's why we have created a separate file for that connect one and we are just going to include that file like this so that is one another statement also that is include ones so we are not using that we are using this required ones and why we are doing this is because if we write here include ones then it does the same thing that this this except for if this file is not there or not available then it is just going to throw a warning and the code will execute as it is while what this required once does is it is going to throw a fatal error and the execution is just going to stop there so this is what we want here as if there is no connection file to the database then there is no point in running the code so we are using required underscore ones then moving on we are going to write here the if block so in this if block we are going to check for this button click so I have used the name of the button here that is signup underscore BTN so this is the same name that I have used in the signup form so we are just checking whether that button is clicked or not so if that button is clicked then we are going to execute this statements so first of all we are going to get the data of the username that the user has entered in the in the user input box using this dollar underscore post and we are storing that value in this username variable then after we are storing the value for email in this variable and then the password and then after here we want to store the hash value for the password that the user has entered into the database so we are going to create a hash of that password and then we are going to store that hash into the database not the plain text that we are going to get from that field so to do that I have used password and ESCO hash method so with this method in PHP here you have to specify first the password the plain text of the password that you're going to get from that input field and then you have to specify the algorithm that you want to use for hashing that password so I have used password underscore default this is a default hashing algorithm that the PHP is going to use and then I have stored it into this has a disco password variable and then after I have created this dry block and in this dry block I am going to write the SQL query for inserting the data into the database so let's take a look at this insert query so if you know the SQL little bit then it is the same query that is for inserting the data so there is no change here the query is as it is you just are storing the query into this variable that's it so here insert into users so users is a table that we are going to create in our database and in that these are all the fields that we have to create and these are all the values that we are going to put in these fields so as you can see here these are not the variables that we have specified here so for that we have to write this prepared statement so as you can see here I have created this state line variable and this is the connection variable that we have in our database under scope connect dot PHP file and we are preparing to execute this statement to insert data into the database and here it is the execute method and then array and in that we have specified this reference and then the name of the variable that is the username so you can also pass on this variable directly here but I don't recommend that you have to use this reference to do that so I have passed on this and then after we are going to check whether this query is performed and we have a row count in our database so if we have a row count so that means that the data is successfully stored in the database then we are going to redirect the user into the main page that is our login page so that page is index dot HTML and thereafter we are going to write a catch statement where we are just going to print out whatever the error message is and that's it so this is the sign up dot PHP file okay now let's start with the login dot PHP file so in this file we are going to write two main statements in the start that are the required one statements so these are those two require underscore ones so first of all we are giving the path for this session dot PHP file and then the second one is our DB underscore connect dot PHP file to come to the database so we are using this session or PHP to start a session after the login is successful it done so let's see how we can do that so after that I have created this if block so in this if block we are just checking whether this login - BTN that is a login button that I have created so it is the name for that button so we are checking whether that button is clicked or not so then after if that button is clicked then we are going to get the username that the user has entered using the dollar underscore post and then we are storing it into this user variable then we are going to get the password and store it as well and thereafter in this dry block we are going to write this select query so we are going to select the row from the database that has this username that the user has specified so we are going to write this prepare statement and as you can see execute array and then this reference and we are signing this to the variable where the username that the user has entered is there and then after we are going to check in this wild statement if we fetch any of the rows having this username then this wild statement is going to be executed so we are going to get on the ID for this particular user so the row ID for that is going to be get from using this dollar row variable and we are going to store it into this ID variable and then after they are going to get the hashed password that we have stored into the database and also store it onto of variable and we are going to get the username also and then after in this if statement I have checked whether the password that the user has entered here in the password field and the password that we have yet from the database are they same or not so if they are same then this if block is going to execute so I have checked that using this password underscore verify so this is a predefined method in PHP with which you can check the hash password and the password plaintext password that the user has entered so we are going to check them and if the password matches then we are going to start a session for this ID and we are going to also store this username into this session variable so we have created these two session variables where we are going to store ID and username for the user and then after we are going to redirect the user to this dashboard PHP page and then after in this else statement so if the password doesn't match then this else statement is going to execute and here we are going to just print this error message that the password or the username is invalid and then after in this cache block also I am going to print out the error message whatever event that there is while running this query so that's it for this login dot PHP file now let's start with this dashboard PHP file and as you know that we can also write the PHP syntax and the HTML syntax both in to PHP file so I am going to use PHP and HTML syntax both side by side in the file so first of all I have this file that is the session dot PHP included and then after so these are the basic HTML tags and in this body I have specified this if statement in PHP so as you can see here if then is set so I have written here exclamation mark so this means that if this session variable is not set then the user is going to be redirected to this location that is the logout dot PHP file that we have also created and I am going to show you that file also in a bit so this is that statement and we are also have else and we haven't specified anything that means it is going to execute this file as it is and then we are ending this if statement and then after I have written this a welcome message where I have used this dollar underscore session variable to get the name of the user and print this on to the screen on to the dashboard and then at the end this is an anchor link to the logout dot PHP file so that's it for this our dashboard or PHP file now let's take a look at the logout dot PHP file ok so this is the logout dot PHP file and in this file I have first statement the require underscore once and I have this session dot PHP files path set here and then session underscore destroy what that does is it is going to destroy the user session that is running and then after we are going to redirect the user to this index dot HTML file so this is the logout dot PHP file okay so we are done with all the PHP files now let's go on to the index dot HTML file and I also made changes in that file so in this file so first of all this is the login form and in this login form as you can see we have to specify the login dot PHP file in this action attribute and now in the signup form I have specified the signup dot PHP files path and then if you have seen my previous video then I also had another field here I have removed that field because we don't need that field for this signup form ok so now let's run this project onto our browser okay so for running this we first have to start our Sam server so we are going to start the Apache and then my sequel ok so both are started so as you can see here it is showing green so they are running now ok so now that we have started the sour so let's open our browser and here let's type localhost so this is our login and sign up page so this is the index page that is running right now and let's go on to this sign up so this is the signup form now this form won't work as we have not connected any database for that so as you can see here there are errors for saying that an unknown database is there then the variable that is for connecting to the database is undefined and all those kind of errors are being displayed so first of all we will have to create the database and the table and then we can check these two forms whether they are working properly or not so let's go on to the PHP my admin so localhost / PHP my admin' so PHP myadmin is a open source tool for MySQL and it is written in PHP so here we are in PHP myadmin so now we have to create a new database so we are going to click on this new button or you can also go on to this database tab and here is the form for creating the database so here we have to specify the database name so now let's go into our source folder and be beyond the scope connect dot PHP and here as you can see here the name of the database that I have written is my DB so I have to write this same name here so my DB and then you have to keep everything else as it is and click on create so what is created now let's write the table name so table name is this one that we have written here so this is the table name users so let's write that users and this is going to have this number of columns so I am just going to add one more and you can also add or remove columns after creating this so don't worry about that so let's click on go and here are all the columns so first of all let's go on to our code so okay so here is the SQL query that we have written and in this query as I've said before these are all the column names that we have to set so here the first is this that is username so let's copy and first here I have to specify one another column that is the ID and we want this length of this to be 6 and here it should be Auto incremented so whenever a new user data is inserted then this ID field is going to take a successive number automatically so this is for that and then after here and it is set to primary key as you can see that and then after for this column it is user name and we want it to be off vehicle type and the length is 50 then after password so let's copy-paste it this is also back here and two five five and then email where care 100 and then to date and then here so here we have to set the type to date time so as you can see I have specified this now method so what this is going to do is the moment the user has pressed the signup button and the query of this insertion is fired it is going to take the time and date from that system and is going to store it onto the database so this is what is happening here and now let's click on save okay so our users table is created so this is auto increment and all the columns that we have created is shown here okay so now is the time to check whether it works or not so let's go here so this is our site let's refresh it and let's go into the signup form and I'm going to specify Akash and ABC at the rate Gmail sorry gmail.com and then one two three four five six so let's click on sign up and we haven't have had any errors so that's a good sign and let's refresh it so I'm just going to click on this browse button okay so as you can see here all the data that I just entered is successfully inserted and the password is hashed as you can see here let me zoom in okay so the password is hashed and this is the to date so here is the date and the time when this data was inserted in this table and as you can see here it automatically took the number one we didn't insert this one in our form it just automatically took this and it is going to increment it on every single greater that is going to be inserted in this table okay so now let's try and logging into this let's zoom out okay so the username is Akash and one two three four five six and that's it we have successfully logged into our system so it says welcome Akash to dashboard and here we have a logout button so if we click on this logout button then it is going to redirect us to this index dot HTML page so we have successfully completed creating this login and sign up form in PHP and also using this MySQL database and now I wanted to show that session thing that I talked about so for that what I'm going to do is I'm just going to go into this logout and when it is that and I'm going to erase this all so now let's refresh and let's login so our cache and one two three four five six okay so we have successfully logged in now let's click on logout and now click on this back button on your browser so as you can see we have back into our dashboard or PHP and even if we again click on this logout and again go back we are still hex able to access this dashboard our PHP file which we should not be able to as we have clicked on logout so that's why this is important and let me show you one more thing so even if I have returned here session destroy' and I have not written those lines in this dashboard dot PHP now let's see what happens so refresh Akash one two three four five six and you're logged in so loud out then again let's go back and we are still able to access it and it is just showing that this username is not defined but still we are able to access this so that's not a good thing so now let's just write this again so I have written this back now let's run this again so Akash one two three four five six and login and logout and then back so as you can see here I am not able to go on to the dashboard or PHP file no matter how many times I click back or forward I am just redirected to this index dot HTML file and even if I go directly by writing dashboard PHP still I am being redirected to this index dot HTML file so this is what we want so this is how you can create a login and signup form and if you like this video then click on that thumbs up button and if you want to see more of these videos then do subscribe to this channel also so that's it for this one and I will see you in the next one
Info
Channel: Mr About Tech
Views: 26,120
Rating: undefined out of 5
Keywords: login and registration form in html and css, php login, php, mysql, login and registration form in php and mysql, signup form in php using mysql, php tutorial, complete login and signup system with php and mysql, register form to database in php, create register form with database, connect html form to mysql database with php, connect php form with mysql database, connect html form to mysql database, user registration form with php and mysql with validation
Id: 4sogKyDXj1M
Channel Id: undefined
Length: 33min 49sec (2029 seconds)
Published: Thu Jul 04 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.