Difference between revisions of "PHP-and-MySQL/C4/User-Registration-Part-6/English-timed"
From Script | Spoken-Tutorial
Sandhya.np14 (Talk | contribs) |
|||
(One intermediate revision by one other user not shown) | |||
Line 5: | Line 5: | ||
|- | |- | ||
|00:00 | |00:00 | ||
− | |Hello everyone, welcome to this Spoken Tutorial | + | |Hello everyone, welcome to this Spoken Tutorial which is more of an update tutorial and not a full length video. |
|- | |- | ||
|00:08 | |00: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. | + | |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. |
|- | |- | ||
|00:19 | |00:19 | ||
− | |Let us go back to our form which is here. Here you can type your | + | |Let us go back to our '''form''' which is here. Here you can type your 'full name'. You can choose a 'username' and a 'password'. |
|- | |- | ||
Line 24: | Line 24: | ||
|- | |- | ||
|00:37 | |00:37 | ||
− | | | + | |for example, let's say I'm registering with the username "alex". In the database we can see here that the username "alex" already exists. |
|- | |- | ||
|00:47 | |00:47 | ||
− | |So what we will do is check the existence of the username. | + | |So, what we will do is check the existence of the username. |
|- | |- | ||
Line 40: | Line 40: | ||
|- | |- | ||
|01:13 | |01:13 | ||
− | |Let's | + | |Let's change this for namesake and click '''Register'''. |
|- | |- | ||
Line 48: | Line 48: | ||
|- | |- | ||
|01:23 | |01:23 | ||
− | |Let us look inside our database. We can see that we have two usernames with alex. | + | |Let us look inside our database. We can see that we have two usernames with "alex". |
|- | |- | ||
Line 56: | Line 56: | ||
|- | |- | ||
|01:31 | |01:31 | ||
− | |The 1st occurrence of name, this one here will be logged in | + | |The 1st occurrence of name, this one here will be logged in and this one will be ignored. |
|- | |- | ||
|01:39 | |01:39 | ||
− | |So this person really will never be able to login into the database. | + | |So, this person really will never be able to login into the database. |
|- | |- | ||
|01:44 | |01:44 | ||
− | |So let's just delete this. | + | |So, let's just delete this. |
|- | |- | ||
|01:48 | |01:48 | ||
− | |You need to create some kind of check to see if the username already exists. | + | |You need to create some kind of check to see if the 'username' already exists. |
|- | |- | ||
|01:53 | |01:53 | ||
− | |This is incredibly easy. There are more | + | |This is incredibly easy. There are more than one method for doing it. |
|- | |- | ||
Line 84: | Line 84: | ||
|- | |- | ||
|02:12 | |02:12 | ||
− | |Selecting my database | + | |Selecting my database, I want to take this up to just where the 'submit' button is checked. |
− | + | ||
|- | |- | ||
|02:20 | |02:20 | ||
− | |So, | + | |So, this is just connecting to the database. I am inside here. |
|- | |- | ||
|02:26 | |02:26 | ||
− | |Then, under here I can start my code to check my username. | + | |Then, under here I can start my code to check my 'username'. |
− | + | ||
|- | |- | ||
|02:31 | |02:31 | ||
− | |Now you can't put your check anywhere. For simplicity I am just going to put it here and kill the rest of the script. | + | |Now, you can't put your check anywhere. For simplicity, I am just going to put it here and '''kill''' the rest of the script. |
|- | |- | ||
|02:39 | |02:39 | ||
− | |If the username has been found, I can put it in anywhere. | + | |If the 'username' has been found, I can put it in anywhere. |
|- | |- | ||
|02:44 | |02:44 | ||
− | |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. | + | |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. |
|- | |- | ||
|02:53 | |02:53 | ||
− | |I recommend casing the checks that you already have inside the next statement and not really to kill the script. | + | |I recommend casing the checks that you already have inside the next statement and not really to '''kill''' the script. |
|- | |- | ||
Line 118: | Line 116: | ||
|- | |- | ||
|03:06 | |03:06 | ||
− | |We need to just type a query that specifies taking a record with a particular username. | + | |We need to just type a '''query''' that specifies taking a '''record''' with a particular username. |
|- | |- | ||
|03:12 | |03:12 | ||
− | |So I will say "namecheck query" here. I will call the variable "namecheck" and this will be a mysql query. | + | |So, I will say "namecheck query" here. I will call the variable "$namecheck" and this will be a '''mysql query'''. |
|- | |- | ||
|03:21 | |03:21 | ||
− | |I will select "username" for simplicity. This is not going to select all the data. | + | |I will '''select''' "username" for simplicity. This is not going to select all the data. |
|- | |- | ||
|03:27 | |03:27 | ||
− | |So I am selecting username from users | + | |So, I am selecting username from "users" |
|- | |- | ||
|03:35 | |03:35 | ||
− | |since that's our table name here. | + | |since that's our '''table''' name here. |
|- | |- | ||
|03:39 | |03:39 | ||
− | |I am going to say | + | |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". |
|- | |- | ||
|03:50 | |03:50 | ||
− | |So we can just come down here and type "username" now. | + | |So, we can just come down here and type "username" now. |
|- | |- | ||
|03:55 | |03:55 | ||
− | |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. | + | |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. |
|- | |- | ||
|04:09 | |04:09 | ||
− | |Now if I were to specify in this case, with only one record | + | |Now if I were to specify in this case, with only one '''record'''. |
|- | |- | ||
Line 158: | Line 156: | ||
|- | |- | ||
|04:20 | |04:20 | ||
− | |So therefore, the username won't | + | |So therefore, the username won't exist, if no records would be returned. So we need a function to check how many records are returned. |
|- | |- | ||
|04:29 | |04:29 | ||
− | |You can do this by creating a count variable. | + | |You can do this by creating a '''$count''' variable. It's "mysql num rows". |
|- | |- | ||
|04:36 | |04:36 | ||
− | |It just returns the amount of records or rows that are contained within your query which is called "namecheck". | + | |It just returns the amount of records or rows that are contained within your query which is called "$namecheck". |
|- | |- | ||
|04:47 | |04:47 | ||
− | |So | + | |So, let's just test this. I am going to '''echo''' out '''$count''' and then '''kill''' the '''script'''. |
|- | |- | ||
Line 178: | Line 176: | ||
|- | |- | ||
|04:57 | |04:57 | ||
− | | | + | |Let's go back to '''Register''' and I'll type my full name as "alex". |
|- | |- | ||
|05:03 | |05:03 | ||
− | |Fullname, then choose | + | |Fullname, then choose an username. I am going to choose "Dale". |
|- | |- | ||
Line 189: | Line 187: | ||
|- | |- | ||
|05:16 | |05:16 | ||
− | |But I will just put them | + | |But I will just put them here for the sake of it and click '''Register'''. |
|- | |- | ||
Line 197: | Line 195: | ||
|- | |- | ||
|05:28 | |05:28 | ||
− | |That's because "Dale" is not actually in the | + | |That's because "Dale" is not actually in the database as a 'username'. |
|- | |- | ||
|05:32 | |05:32 | ||
− | |However if I change this to "alex", that will be a small "a". | + | |However, if I change this to "alex", that will be a small "a". |
|- | |- | ||
|05:39 | |05:39 | ||
− | |We have got some | + | |We have got some strip tags. The way to deal with case sensitivity as well, is.... so, this is another pointer.. |
|- | |- | ||
|05:49 | |05:49 | ||
− | |When we are taking the username into account what we are going to say "string to lower" here, just to make sure that this will always convert to lowercase. | + | |When we are taking the 'username' into account, what we are going to say "string to lower" here, just to make sure that this will always convert to lowercase. |
|- | |- | ||
|06:01 | |06:01 | ||
− | |Next we are going to... let me find it.... Click Register. | + | |Next we are going to... let me find it.... Click '''Register'''. |
|- | |- | ||
|06:08 | |06:08 | ||
− | |We can see that the value of one is returned | + | |We can see that the value of one is returned. |
|- | |- | ||
|06:12 | |06:12 | ||
− | |So the check that we were looking for here is - if this variable we are echoing out, is not equal to zero,..then we need to tell the user that the username is already registered. | + | |So, the check that we were looking for here is - if this variable we are echoing out, is not equal to zero,..then we need to tell the user that the username is already registered. |
|- | |- | ||
|06:25 | |06:25 | ||
− | |So here we | + | |So, here we can create a simple '''if''' statement and our '''block'''. |
|- | |- | ||
|06:29 | |06:29 | ||
− | |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... | + | |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... |
|- | |- | ||
|06:40 | |06:40 | ||
− | |...then we can just kill the script and say "Username already taken" or any other message. Coming back here, | + | |...then we can just '''kill''' the script and say "Username already taken!" or any other message. Coming back here, let's refresh. |
|- | |- | ||
|06:50 | |06:50 | ||
− | |We can choose "alex". Let me type in a password and click | + | |We can choose "alex". Let me type in a password and click '''Register'''. |
|- | |- | ||
|06:56 | |06:56 | ||
− | |You can see that we have got "Username already taken" error. | + | |You can see that we have got "Username already taken!" error. |
|- | |- | ||
|07:00 | |07:00 | ||
− | |If I was to type "Dale" and choose a new name and password and click | + | |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. |
|- | |- | ||
|07:15 | |07:15 | ||
− | |So I will leave it at that. You can see that we have got our registered user in. | + | |So, I will leave it at that. You can see that we have got our registered user in. |
|- | |- | ||
|07:22 | |07:22 | ||
− | | Add a "str to lower" function | + | | Add a "str to lower" function which is a really useful thing to keep everything simple. |
|- | |- | ||
|07:29 | |07:29 | ||
− | |Or you can just use a "str to lower" function in your if statement. | + | |Or you can just use a "str to lower" function in your '''if''' statement. |
|- | |- | ||
Line 276: | Line 274: | ||
|- | |- | ||
|07:58 | |07:58 | ||
− | |Thanks for watching. This is Aravind dubbing for the Spoken Tutorial project. | + | |Thanks for watching. This is Aravind, dubbing for the Spoken Tutorial project. |
Latest revision as of 12:33, 10 June 2015
Time | Narration |
00:00 | Hello everyone, welcome to this Spoken Tutorial which is more of an update tutorial and not a full length video. |
00: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. |
00:19 | Let us go back to our form which is here. Here you can type your 'full name'. You can choose a 'username' and a 'password'. |
00:28 | I have had these values here before. Let us get rid of them for now. |
00:33 | But, what we want is, when we are choosing the username... |
00:37 | for example, let's say I'm registering with the username "alex". In the database we can see here that the username "alex" already exists. |
00:47 | So, what we will do is check the existence of the username. |
00:50 | If the username already exists, we are not going to let the user register because we don't want a double username. |
01: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. |
01:13 | Let's change this for namesake and click Register. |
01:20 | I have been successfully registered. |
01:23 | Let us look inside our database. We can see that we have two usernames with "alex". |
01:28 | Now this causes problems while logging in. |
01:31 | The 1st occurrence of name, this one here will be logged in and this one will be ignored. |
01:39 | So, this person really will never be able to login into the database. |
01:44 | So, let's just delete this. |
01:48 | You need to create some kind of check to see if the 'username' already exists. |
01:53 | This is incredibly easy. There are more than one method for doing it. |
01:59 | But I am going for the simplest and probably the most effective way which is going to work. |
02:05 | The first thing I want to do is, take my code to connect to my database. |
02:12 | Selecting my database, I want to take this up to just where the 'submit' button is checked. |
02:20 | So, this is just connecting to the database. I am inside here. |
02:26 | Then, under here I can start my code to check my 'username'. |
02:31 | Now, you can't put your check anywhere. For simplicity, I am just going to put it here and kill the rest of the script. |
02:39 | If the 'username' has been found, I can put it in anywhere. |
02:44 | 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. |
02:53 | I recommend casing the checks that you already have inside the next statement and not really to kill the script. |
03:00 | But you will get the general idea here on how to work on what we are trying to do. |
03:06 | We need to just type a query that specifies taking a record with a particular username. |
03:12 | So, I will say "namecheck query" here. I will call the variable "$namecheck" and this will be a mysql query. |
03:21 | I will select "username" for simplicity. This is not going to select all the data. |
03:27 | So, I am selecting username from "users" |
03:35 | since that's our table name here. |
03:39 | 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". |
03:50 | So, we can just come down here and type "username" now. |
03:55 | 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. |
04:09 | Now if I were to specify in this case, with only one record. |
04:15 | If I were to specify the username as "Dale", for example, no records will be returned. |
04:20 | So therefore, the username won't exist, if no records would be returned. So we need a function to check how many records are returned. |
04:29 | You can do this by creating a $count variable. It's "mysql num rows". |
04:36 | It just returns the amount of records or rows that are contained within your query which is called "$namecheck". |
04:47 | So, let's just test this. I am going to echo out $count and then kill the script. |
04:53 | The rest of the code does not execute. |
04:57 | Let's go back to Register and I'll type my full name as "alex". |
05:03 | Fullname, then choose an username. I am going to choose "Dale". |
05:10 | The password won't be checked so we could skip that. |
05:16 | But I will just put them here for the sake of it and click Register. |
05:24 | We see that we have got a value of zero that is returned. |
05:28 | That's because "Dale" is not actually in the database as a 'username'. |
05:32 | However, if I change this to "alex", that will be a small "a". |
05:39 | We have got some strip tags. The way to deal with case sensitivity as well, is.... so, this is another pointer.. |
05:49 | When we are taking the 'username' into account, what we are going to say "string to lower" here, just to make sure that this will always convert to lowercase. |
06:01 | Next we are going to... let me find it.... Click Register. |
06:08 | We can see that the value of one is returned. |
06:12 | So, the check that we were looking for here is - if this variable we are echoing out, is not equal to zero,..then we need to tell the user that the username is already registered. |
06:25 | So, here we can create a simple if statement and our block. |
06:29 | 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... |
06:40 | ...then we can just kill the script and say "Username already taken!" or any other message. Coming back here, let's refresh. |
06:50 | We can choose "alex". Let me type in a password and click Register. |
06:56 | You can see that we have got "Username already taken!" error. |
07:00 | 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. |
07:15 | So, I will leave it at that. You can see that we have got our registered user in. |
07:22 | Add a "str to lower" function which is a really useful thing to keep everything simple. |
07:29 | Or you can just use a "str to lower" function in your if statement. |
07:32 | However, to keep it simple I would recommend that you convert all usernames into lowercase. |
07:39 | 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. |
07:48 | I encourage you to play around with it. That's a good way to find some errors. |
07:53 | Try them but if you do need any help, please email me. Make sure you subscribe for updates. |
07:58 | Thanks for watching. This is Aravind, dubbing for the Spoken Tutorial project. |