From Script | Spoken-Tutorial
Jump to: navigation, search
Time Narration
0:00 Hello everyone, welcome to this Spoken Tutorial, which is more of an update tutorial and not a full length video.
0:08 Some one has pointed out to me that in my register script, I need some kind of check to note if the user has been registered or not by the username that they specify.
0:19 Let us go back to our form which is here. Here you can type your fullname. You can choose a username and a password.
0:28 I have had these values here before. Let us get rid of them for now.
0:33 But, what we want is, when we are choosing the username...
0:37 For example, lets say I'm registering with the username "alex". In the database we can see here that username "alex" already exists.
0:44 So what we will do is check the existence of the username.
0:49 If the username already exists, we are not going to let the user register because we don't want a double username.
1.01 If I were to register here, let me put the password in and choose the username as "alex". The username "alex" is already in the database.
1:13 Let's just change this for namesake and click register. I have been successfully registered.
1:23 Let us look inside our database. We can see that we have two usernames with alex. Now this causes problems while logging in.
1:33 The 1st occurrence of name, this one here will be logged in. And this one will be ignored. So this person really will never be able to login into the database.
1:45 So let's just delete this.
1:48 You need to create some kind of check to see if the username already exists.
1:53 This is incredibly easy. There are more that one method for doing it.
1:59 But I am going for the simplest and probably the most effective way which is going to work.
2;04 The first thing I want to do is, take my code to connect to my database.
2:11 Selecting my database. I want to take this up to just where the submit button is checked.
2:21 So, it is just connecting to the database. I am inside here.
2.26 Then, under here I can start my code to check my username.
2:31 Now bear in mind that you can't put your check anywhere. For simplicity I am just going to put it here and kill the rest of the script.
2.30 If the username has been found, I can put it in anywhere. Take care when you are using a full length page in your website, the die function will cut off the rest of the code. So I don't recommend using this.
2:51 I recommend casing the checks that you already have inside the next statement and not really to kill the script.
3:00 But you will get the general idea here on how to work on what we are trying to do.
3:06 We need to just type a query that specifies taking a record with a particular username.
3:18 So I will say "namecheck query" here. I will call the variable "namecheck" and this will be a mysql query.
3.28 I will select "username" for simplicity. This is not going to select all the data.
3:40 So I am selecting username from users, since that's our table name here.
3:47 I am going to say where username is equal to... If we look up here username of the person that submits the form is in the variable name "username".
3:57 So we can just come down here and type "username" now.
4:03 Now if we choose the name "alex", this would select every record in the database that has the username "alex" and we can see there's one at the moment.
4:13 Now if I were to specify in this case, with only one record...
4:17 If I were to specify the username as "Dale", for example, no records will be returned.
4:25 So therefore, the username won't exists, if no records would be returned. So we need a function to check how many records are returned.
4:32 You can do this by creating a count variable. Its "mysql num rows". It just returns the amount of records or rows that are contained within your query which is called "namecheck".
4:50 So lets just test this. I am going to echo out count and then kill the script. The rest of the code does not execute.
4:57 Lets go back to register and I'll type my fullname as "alex". Fullname, then choose a username. I am going to choose "Dale".
5:10 The password won't be checked so we could skip that.
5:18 But I will just put them there for the sake of it and click Register. We see that we have got a value of zero that is returned.
5:29 That's because "Dale" is not actually in the data base as a username.
5:35 However if I change this to "alex", that will be a small "a".
5:43 We have got some... strip tags. The way to deal with case sensitivity as well, is.... so this is another pointer....
5:53 When we are taking the username into account what we are going to say "str to lower" here, just to make sure that this will always convert to lowercase.
6:07 Next we are going to... let me find it.... Click Register.
6:13 We see that the value of one is returned. So the check that we were looking for here is - if this variable we are echoing out, is not equal to zero,...
6:25 ...then we need to tell the user that the username is already registered.
6:30 So here we will create a simple if statement and our block.
6:36 Then we can say, if our count doesn't equal zero, meaning there is a record present under this condition where the username is already specified...
6:47 ...then we can just kill the script and say "Username already taken" or any other message. Coming back here, lets refresh.
6:56 We can choose "alex". Let me type in a password and click register. You can see that we have got "Username already taken" error.
7:07 If I was to type "Dale" and choose a new name and password and click register, we can see that it has been successfully registered into the database because the username does not exists.
7:24 So I will leave it at that. You can see that we have got our registered user in. Add a "str to lower" function, which is a really useful thing to keep everything simple.
7:35 Or you can just use a "str to lower" function in your if statement. However, to keep it simple I would recommend that you convert all usernames to lowercase.
7:52 You need to incorporate that into a login script as well. You need to convert whatever the user is typing into the login box to lowercase.
7:55 I encourage you to play around with it. That's a good way to find some errors.
8:02 Try them but if you do need any help, please email me. Make sure you subscribe for updates.
8:07 Thanks for watching. This is __________ dubbing for the Spoken Tutorial project.

Contributors and Content Editors

Chandrika, Pravin1389