Difference between revisions of "PHP-and-MySQL/C4/User-Login-Part-1/English-timed"
From Script | Spoken-Tutorial
Sandhya.np14 (Talk | contribs) |
|||
Line 4: | Line 4: | ||
|- | |- | ||
|00:00 | |00:00 | ||
− | |Welcome to | + | |Welcome to this tutorial on '''user login''' and '''sessions'''. |
|- | |- | ||
|00:03 | |00:03 | ||
− | |This tutorial will give a few aspects of php that will focus on how an html form can be submitted and how to check for user name and password. | + | |This tutorial will give a few aspects of php that will focus on how an '''html form''' can be submitted and how to check for '''user name''' and password. |
|- | |- | ||
|00:14 | |00:14 | ||
Line 13: | Line 13: | ||
|- | |- | ||
|00:16 | |00:16 | ||
− | |I'll show you how to set-up a database with your user name and password, how to connect to a database and also to process a logout function. | + | |I'll show you how to set-up a database with your 'user name' and password, how to connect to a database and also to process a logout function. |
|- | |- | ||
|00:25 | |00:25 | ||
− | |Since we're using sessions, the user will remain logged-in until they press the logout button. | + | |Since we're using '''sessions''', the user will remain logged-in until they press the logout button. |
|- | |- | ||
|00:32 | |00:32 | ||
− | |To start with I'll create an html form. | + | |To start with I'll create an '''html form'''. |
|- | |- | ||
|00:35 | |00:35 | ||
− | |I'll take you through some of the | + | |I'll take you through some of the MySQL features that we will set up. |
|- | |- | ||
|00:42 | |00:42 | ||
− | |In our html form, we have the action going to a page called "login dot php". | + | |In our '''html form''', we have the '''action''' going to a page called "login dot php". |
|- | |- | ||
|00:47 | |00:47 | ||
− | |We'll keep separate pages to keep it simple. | + | |We'll keep separate '''pages''' to keep it simple. |
|- | |- | ||
|00:49 | |00:49 | ||
− | |Our method is going to be POST. | + | |Our '''method''' is going to be "POST". |
|- | |- | ||
|00:50 | |00:50 | ||
− | | | + | |Let's end our '''form''' here. |
|- | |- | ||
|00:54 | |00:54 | ||
− | |I'll start creating our input type which will be "text" and the name will be "username". | + | |I'll start creating our '''input type''' which will be "text" and the '''name''' will be "username". |
|- | |- | ||
|01:06 | |01:06 | ||
− | |A line break here. | + | |A 'line-break' here. |
|- | |- | ||
|01:09 | |01:09 | ||
Line 46: | Line 46: | ||
|- | |- | ||
|01:15 | |01:15 | ||
− | | And | + | | And it's called "password". Depending on which operating system we are using, this will appear as stars or circles. |
|- | |- | ||
|01:24 | |01:24 | ||
− | |And finally we'll create a "submit" button and its value will be "Log in". | + | |And finally we'll create a "submit" button and its '''value''' will be "Log in". |
|- | |- | ||
|01:31 | |01:31 | ||
− | | Let's try this. Refresh and we have a page here. | + | | Let's try this. Refresh and we have a '''page''' here. |
|- | |- | ||
|01:36 | |01:36 | ||
Line 58: | Line 58: | ||
|- | |- | ||
|01:39 | |01:39 | ||
− | |I'll log in and it goes to a page that doesn't exist. | + | |I'll log in and it goes to a '''page''' that doesn't exist. |
|- | |- | ||
|01:43 | |01:43 | ||
− | |Now let's make it a bit more user friendly and type out labels here. | + | |Now, let's make it a bit more user friendly and type out '''labels''' here. |
|- | |- | ||
|01:54 | |01:54 | ||
Line 73: | Line 73: | ||
|- | |- | ||
|02:04 | |02:04 | ||
− | |If you are using "xampp" then it will be installed by default using the local host for "php my admin". | + | |If you are using "xampp" then it will be installed by default using the '''local host''' for "php my admin". |
|- | |- | ||
|02:11 | |02:11 | ||
− | |If it isn't installed yet, I would suggest you google it and install a copy on the local host directory and start using it. | + | |If it isn't installed yet, I would suggest you google it and install a copy on the '''local host''' directory and start using it. |
|- | |- | ||
|02:21 | |02:21 | ||
Line 82: | Line 82: | ||
|- | |- | ||
|02:25 | |02:25 | ||
− | |So here, create new database called "php login" and click | + | |So here, create new database called "php login" and click '''Create'''. |
|- | |- | ||
|02:40 | |02:40 | ||
− | |We can see it appears here and we can now create tables. | + | |We can see it appears here and we can now create '''tables'''. |
|- | |- | ||
|02:46 | |02:46 | ||
− | |In case you're not familiar with sql, let me brief you. | + | |In case you're not familiar with '''sql''', let me brief you. |
|- | |- | ||
|02:50 | |02:50 | ||
− | |A basic structure is a database which stores tables | + | |A basic structure is a database which stores '''tables''', tables store '''rows''' and rows store '''values'''. |
|- | |- | ||
|03:00 | |03:00 | ||
− | | | + | |Let's name it "users" and click on '''OK'''. |
|- | |- | ||
|03:06 | |03:06 | ||
− | |An error - the | + | |An '''error''' - the "Number of fields"! |
|- | |- | ||
|03:10 | |03:10 | ||
− | |When I create a new database, I open up a notepad | + | |When I create a new database, I open up a notepad and note down all the '''fields''' that I'll use. |
|- | |- | ||
|03:20 | |03:20 | ||
Line 109: | Line 109: | ||
|- | |- | ||
|03:36 | |03:36 | ||
− | |But for now we're using these 3 fields making it a total of 3 fields. | + | |But for now, we're using these 3 '''fields''' making it a total of 3 fields. |
|- | |- | ||
|03:42 | |03:42 | ||
Line 115: | Line 115: | ||
|- | |- | ||
|03:49 | |03:49 | ||
− | |Now we proceed with typing in the the field names. | + | |Now we proceed with typing in the the '''field''' names. |
|- | |- | ||
|03:53 | |03:53 | ||
− | |We type "id" and we will make this an integer. | + | |We type "id" and we will make this an '''integer'''. |
|- | |- | ||
|03:57 | |03:57 | ||
− | |This is the primary key and we want it to make | + | |This is the '''primary key''' and we want it to make '''auto_increment'''. |
|- | |- | ||
|04:02 | |04:02 | ||
− | |Now, every | + | |Now, every time a new '''record''' is created, the "id" values will increment by one. |
|- | |- | ||
|04:07 | |04:07 | ||
− | |So, for example, the first user who registers will have an id of one, the second user who registers will have an id of two and so on and so forth. | + | |So, for example, the first user who registers will have an "id" of one, the second user who registers will have an "id" of two and so on and so forth. |
|- | |- | ||
|04:15 | |04:15 | ||
− | |Okay, next one will be the user name and last one is going to be the password. | + | |Okay, next one will be the "user name" and last one is going to be the "password". |
|- | |- | ||
|04:23 | |04:23 | ||
− | |Next we'll set them as VARCHARs and I'll set this as 25 characters and the password as 25 characters, as well. | + | |Next, we'll set them as '''VARCHARs''' and I'll set this as 25 characters and the password as 25 characters, as well. |
|- | |- | ||
|04:31 | |04:31 | ||
Line 139: | Line 139: | ||
|- | |- | ||
|04:34 | |04:34 | ||
− | |Let us scroll down and | + | |Let us scroll down and let's click on '''Save'''. |
|- | |- | ||
|04:40 | |04:40 | ||
− | |Okay so once I save here, we can come down and see this here. | + | |Okay, so once I '''save''' here, we can come down and see this here. |
|- | |- | ||
|04:44 | |04:44 | ||
Line 166: | Line 166: | ||
|- | |- | ||
|05:16 | |05:16 | ||
− | |Okay so user name is "Alex" and password is "abc" - easy to remember. That's what has been stored. | + | |Okay, so user name is "Alex" and password is "abc" - easy to remember. That's what has been stored. |
|- | |- | ||
|05:26 | |05:26 | ||
− | |To browse, just click the | + | |To browse, just click the '''Browse''' tab. |
|- | |- | ||
|05:28 | |05:28 | ||
− | |Let's scroll down. We have user name and password as "Alex" and "abc" and the id has already been set to 1. | + | |Let's scroll down. We have "user name" and "password" as "Alex" and "abc" and the "id" has already been set to 1. |
|- | |- | ||
|05:37 | |05:37 | ||
Line 178: | Line 178: | ||
|- | |- | ||
|05:46 | |05:46 | ||
− | | | + | |Let's '''save''' this quickly - "login dot php". |
|- | |- | ||
|05:51 | |05:51 | ||
− | |Let us see how to create our php tags. | + | |Let us see how to create our '''php tags'''. |
|- | |- | ||
|05:55 | |05:55 | ||
− | |I'll take into account some POST variables now. | + | |I'll take into account some '''POST''' variables now. |
|- | |- | ||
|05:59 | |05:59 | ||
− | |In "index dot php", we used the method as POST. | + | |In "index dot php", we used the '''method''' as "POST". |
|- | |- | ||
|06:01 | |06:01 | ||
− | |We'll set | + | |We'll set '$username' as 'dollar sign underscore POST' and rename the variable which is "username". |
|- | |- | ||
|06:11 | |06:11 | ||
− | |It is found here and.... password will equal a POST value and that will be "password". | + | |It is found here and.... $password will equal a POST value and that will be "password". |
|- | |- | ||
|06:25 | |06:25 | ||
− | |First of all, we will check whether both the | + | |First of all, we will check whether both the username and the password were entered. |
|- | |- | ||
|06:30 | |06:30 | ||
− | |We won't start validating the form. Its unnecessary to do so since we know the user has entered both these fields. | + | |We won't start validating the '''form'''. Its unnecessary to do so since we know the user has entered both these '''fields'''. |
|- | |- | ||
|06:38 | |06:38 | ||
− | |Now, I will type my | + | |Now, I will type my '''if''' statement. |
|- | |- | ||
|06:40 | |06:40 | ||
Line 208: | Line 208: | ||
|- | |- | ||
|06:45 | |06:45 | ||
− | |So here I'll say if "username" which means if "username" has a value, it will return TRUE and I'll say "password". | + | |So, here I'll say '''if''' "$username" which means if "username" has a value, it will return '''TRUE''' and I'll say "$password". |
|- | |- | ||
|06:56 | |06:56 | ||
− | |So this requires the "username" and "password" for this to be TRUE and to execute this block of code here. | + | |So this requires the "username" and "password" for this to be '''TRUE''' and to execute this block of code here. |
|- | |- | ||
|07:04 | |07:04 | ||
Line 217: | Line 217: | ||
|- | |- | ||
|07:08 | |07:08 | ||
− | |To do this we create a variable called "connect" equal to "mysql_connect". | + | |To do this, we create a variable called "$connect" equal to "mysql_connect()". |
|- | |- | ||
|07:20 | |07:20 | ||
− | |And inside this the first parameter will be a "host" which is "localhost" for me. | + | |And inside this, the first parameter will be a "host" which is "localhost" for me. |
|- | |- | ||
|07:28 | |07:28 | ||
Line 229: | Line 229: | ||
|- | |- | ||
|07:37 | |07:37 | ||
− | |After this we can say "or die" and give an error message. | + | |After this we can say "or die" and give an '''error''' message. |
|- | |- | ||
|07:39 | |07:39 | ||
Line 235: | Line 235: | ||
|- | |- | ||
|07:44 | |07:44 | ||
− | |I am not sure about my password. I think | + | |I am not sure about my password. I think it's something else. |
|- | |- | ||
|07:48 | |07:48 | ||
− | |We'll try something then it will say "Couldn't connect". | + | |We'll try something, then it will say "Couldn't connect". |
|- | |- | ||
|07:51 | |07:51 | ||
− | |Now we need to select our table, sorry our database. | + | |Now we need to select our '''table''', sorry our database. |
|- | |- | ||
|07:58 | |07:58 | ||
− | |We'll say " | + | |We'll say "mysql_select_db" which is another built-in function when you have the '''php module''' installed. |
|- | |- | ||
|08:06 | |08:06 | ||
Line 256: | Line 256: | ||
|- | |- | ||
|08:30 | |08:30 | ||
− | |Refresh page. Click | + | |Refresh page. Click '''Login'''. Nothing happened. |
|- | |- | ||
|08:37 | |08:37 | ||
− | |Let's edit our "if" statement and say "else" echo or instead the best function is "die". | + | |Let's edit our "if" statement and say "else" echo or instead the best function is "die()". |
|- | |- | ||
|08:47 | |08:47 | ||
Line 271: | Line 271: | ||
|- | |- | ||
|09:08 | |09:08 | ||
− | |Refresh this. Resend the data and we get this error message. | + | |Refresh this. '''Resend''' the data and we get this error message. |
|- | |- | ||
|09:13 | |09:13 | ||
− | |Next I'll type "Alex" and "123", sorry "abc" and click | + | |Next I'll type "Alex" and "123", sorry "abc" and click '''Login'''. |
|- | |- | ||
|09:18 | |09:18 | ||
− | |No error message | + | |No error message which means we are connected to the database. |
|- | |- | ||
|09:25 | |09:25 | ||
− | |That's the end of this part. In the next one I will show how to connect to our database and check for the | + | |That's the end of this part. In the next one I will show how to connect to our database and check for the username and password. |
|- | |- | ||
|09:34 | |09:34 | ||
− | |Thanks for joining us. This is Royston dubbing for the Spoken Tutorial Project. Bye. | + | |Thanks for joining us. This is Royston, dubbing for the Spoken Tutorial Project. Bye. |
Revision as of 20:59, 6 June 2015
Time | Narration |
00:00 | Welcome to this tutorial on user login and sessions. |
00:03 | This tutorial will give a few aspects of php that will focus on how an html form can be submitted and how to check for user name and password. |
00:14 | The entered values will be checked against a database. |
00:16 | I'll show you how to set-up a database with your 'user name' and password, how to connect to a database and also to process a logout function. |
00:25 | Since we're using sessions, the user will remain logged-in until they press the logout button. |
00:32 | To start with I'll create an html form. |
00:35 | I'll take you through some of the MySQL features that we will set up. |
00:42 | In our html form, we have the action going to a page called "login dot php". |
00:47 | We'll keep separate pages to keep it simple. |
00:49 | Our method is going to be "POST". |
00:50 | Let's end our form here. |
00:54 | I'll start creating our input type which will be "text" and the name will be "username". |
01:06 | A 'line-break' here. |
01:09 | Copy-paste this line and change "text" to "password". |
01:15 | And it's called "password". Depending on which operating system we are using, this will appear as stars or circles. |
01:24 | And finally we'll create a "submit" button and its value will be "Log in". |
01:31 | Let's try this. Refresh and we have a page here. |
01:36 | "index dot php" with a user name and password. |
01:39 | I'll log in and it goes to a page that doesn't exist. |
01:43 | Now, let's make it a bit more user friendly and type out labels here. |
01:54 | Refresh and there we are. |
01:59 | Now let us create our "login dot php" file. |
02:01 | First I will open "php my admin". |
02:04 | If you are using "xampp" then it will be installed by default using the local host for "php my admin". |
02:11 | If it isn't installed yet, I would suggest you google it and install a copy on the local host directory and start using it. |
02:21 | Now, we'll create a new database. |
02:25 | So here, create new database called "php login" and click Create. |
02:40 | We can see it appears here and we can now create tables. |
02:46 | In case you're not familiar with sql, let me brief you. |
02:50 | A basic structure is a database which stores tables, tables store rows and rows store values. |
03:00 | Let's name it "users" and click on OK. |
03:06 | An error - the "Number of fields"! |
03:10 | When I create a new database, I open up a notepad and note down all the fields that I'll use. |
03:20 | I'll use "id" for start, next "user name" and lastly "password". That's all we want at the moment. |
03:28 | We can also add the "first name", "date of birth" etc. depending on your program. |
03:36 | But for now, we're using these 3 fields making it a total of 3 fields. |
03:42 | Let's go back here. So, three fields and that will create that first. |
03:49 | Now we proceed with typing in the the field names. |
03:53 | We type "id" and we will make this an integer. |
03:57 | This is the primary key and we want it to make auto_increment. |
04:02 | Now, every time a new record is created, the "id" values will increment by one. |
04:07 | So, for example, the first user who registers will have an "id" of one, the second user who registers will have an "id" of two and so on and so forth. |
04:15 | Okay, next one will be the "user name" and last one is going to be the "password". |
04:23 | Next, we'll set them as VARCHARs and I'll set this as 25 characters and the password as 25 characters, as well. |
04:31 | There is nothing else that we need to set for these. |
04:34 | Let us scroll down and let's click on Save. |
04:40 | Okay, so once I save here, we can come down and see this here. |
04:44 | And you can insert values in them. |
04:48 | We'll do it because we're testing. |
04:50 | I have created some tutorials on how to make a user registration form. We can discuss this further there. |
05:01 | The value of "id" will be auto-incremented, so we don't have to put anything. |
05:05 | It'll go to 1 straight away. |
05:07 | In user name, I'll say "Alex". |
05:10 | My password will be "abc". I would recommend you a better password, though. |
05:16 | Okay, so user name is "Alex" and password is "abc" - easy to remember. That's what has been stored. |
05:26 | To browse, just click the Browse tab. |
05:28 | Let's scroll down. We have "user name" and "password" as "Alex" and "abc" and the "id" has already been set to 1. |
05:37 | Now, we'll create the "login dot php" page. |
05:46 | Let's save this quickly - "login dot php". |
05:51 | Let us see how to create our php tags. |
05:55 | I'll take into account some POST variables now. |
05:59 | In "index dot php", we used the method as "POST". |
06:01 | We'll set '$username' as 'dollar sign underscore POST' and rename the variable which is "username". |
06:11 | It is found here and.... $password will equal a POST value and that will be "password". |
06:25 | First of all, we will check whether both the username and the password were entered. |
06:30 | We won't start validating the form. Its unnecessary to do so since we know the user has entered both these fields. |
06:38 | Now, I will type my if statement. |
06:40 | This will be a big block because all the code that I require after I check this will go in here. |
06:45 | So, here I'll say if "$username" which means if "username" has a value, it will return TRUE and I'll say "$password". |
06:56 | So this requires the "username" and "password" for this to be TRUE and to execute this block of code here. |
07:04 | What should we write here? We need to connect to our database. |
07:08 | To do this, we create a variable called "$connect" equal to "mysql_connect()". |
07:20 | And inside this, the first parameter will be a "host" which is "localhost" for me. |
07:28 | The second one will be "username" and I'll use "root". |
07:31 | The third one is the "password" which I believe I don't have. We'll check on that. |
07:37 | After this we can say "or die" and give an error message. |
07:39 | So, for example, we can say "Couldn't connect". |
07:44 | I am not sure about my password. I think it's something else. |
07:48 | We'll try something, then it will say "Couldn't connect". |
07:51 | Now we need to select our table, sorry our database. |
07:58 | We'll say "mysql_select_db" which is another built-in function when you have the php module installed. |
08:06 | It also comes with XAMPP. |
08:11 | Here I'll put a double quote and say "phplogin" |
08:19 | So assuming that everything is okay, I can add my error message here "Couldn't find db". Okay?. |
08:30 | Refresh page. Click Login. Nothing happened. |
08:37 | Let's edit our "if" statement and say "else" echo or instead the best function is "die()". |
08:47 | Here it will just stop executing anything after this point, after this function is called. |
08:54 | And it'll also pass a message of your choice. |
08:58 | So here I'll say "Please enter a user name and a password". |
09:08 | Refresh this. Resend the data and we get this error message. |
09:13 | Next I'll type "Alex" and "123", sorry "abc" and click Login. |
09:18 | No error message which means we are connected to the database. |
09:25 | That's the end of this part. In the next one I will show how to connect to our database and check for the username and password. |
09:34 | Thanks for joining us. This is Royston, dubbing for the Spoken Tutorial Project. Bye. |