Welcome. In this tutorial, we will talk about how a user can change their password.
We will learn how to give the user the option to change their password if they want to.
This shouldn’t take too long. It will be covered in 3 parts.
We’ll give the user a form and ask the user to type their old password and their new password twice.
We will check their old password, the one that’s in the database.
Remember, they’re encrypted.
Then we will compare the two new passwords to see if they match, to see if they’ve made any mistake.
Next we will update the database using the new sql codes.
So first of all I will start my "session" on my "member" page. As you can see we’ve got our "session_start" here.
I’ll just copy and paste that to the top of my page. So, we’ve started our session.
We need to use a variable called "user" which will be equal to the session that we’ve set over here.
First, we need to check that the users have logged in - this is the code I was talking about - before we start to change their password or let them change their password.
I’ll set this "user" variable as our "session" name which is here.
Okay, now we will say “ if the user exists”, we’ll let them change their password otherwise we kill the page and say “You must be logged in to change your password".
This is the block for “User is logged in”. So, assuming the user has logged in, after we’ve checked for the existence of the login , we need to give them a form to fill up.
I'll echo out our code here which will be our form. It’s a self- submitting form so it’ll to go back to "change password dot php" and end the form here.
So that's the page we are already on and so I’ll use a check to verify all the details.
The method of the form is POST because we don’t want to let any password information into the URL.
Next we’ll create a few input boxes. First “Old password:” which won't be a password type, so the entry won't be hidden. So input type will be "text" and name will "oldpassword".
I’ll put a paragraph break. Next is "New password:" and I will create an input type as "password" so that it is hidden from everyone. The name will be “new password”.
I'll put a line break here. Now copy paste this sentence and make a few changes. The label here will be “Repeat new password” and the name will be "repeat new password" followed by a paragraph break.
Lastly we need a “submit" button. The name will be "submit" so we can check if that’s being pressed and the value will be "Change password".
Okay, so let’s go to our page. I’ll stick a link in the members’ page, in order to help us change our password.
For now, I’ll just login using the details. My password at the moment is “abc and my username is “Alex”.
Click on login. It says "Welcome Alex". Here is the member page. The session has been set. If we need to log out, we can log out. But we need another option to change our password.
So we go back to our "member dot php" page and I’ll create another link.
And that will be “Change password”.
And this will link to “change password dot php".
So if we refresh this we’ll get another option. Click here and we get our form that we had created earlier. I'll type my old password here and my new passwords in here.
Click "Change password" but nothing happens. So we want to check whether this has been submitted or not. Delete this extra line here.
What we need to do is create an If statement to say “if POST submit” which just means "has the user pressed this submit button?". The name is submit, therefore we’ve got submit written here.
And if the user has submitted then we start changing our password here.
Otherwise, if the user hasn’t submitted we’re going to echo out this code here.
If the user hasn’t already submitted, then they’ll have to present with the form here, in order to submit the form.
Let’s go ahead and test this. What we’ll do here is echo out “test” to see if this works.
Lets go back and fill in. Actually we don’t need to fill anything. We will just click the submit button. And we get an echo of “test” to show that our form has been submitted successfully.
Ok so we need to start changing the password. Delete this and here we will say “check fields”.
We’ve got a few variables that we need to set - our old password which will be equal to POST variable name “old password”. We have given that name down here in our form.
And I’ll just replicate these for each value that we are submitting.
Next one is “new password” and then we have “repeat new password”. We’ll just change these.
To make sure these are working, and I do recommend you do this all the time, echo out “old password”, “new password” and ”repeat new password”.
It check for the form existence, whether the form has been submitted, and then we get our variables and our post variables into the variable name.
I’ll echo out what we’ve typed in the box just to see if everything is working.
So my old password is “abc” and my old password is “123”. Click "Change password" and we get abc, 123 and 123.
So that form information has been submitted. There are no spelling mistakes. I can be confident that I can get my user to set up a new password.
I’m going to stop the tutorial now. In the next part I'll teach how to check the old password against the new password in the database, how to check if the new password and the repeated password match and then to change the user’s password.
Thanks for watching. This is Joushwa Mathew dubbing for the Spoken Tutorial project.