From Script | Spoken-Tutorial
Revision as of 21:17, 1 December 2012 by Pravin1389 (Talk | contribs)

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

Contributors and Content Editors

Chandrika, Pravin1389