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