Welcome to the 3rd part of our "User login" tutorial.
Here, we'll create a session in which the user can enter a page and they're allowed to be inside the page as long as they have successfully logged in.
To start any session, we will need a function which is "start session".
Is it "start session" or "session start"? Let's just test it quickly.
Right, an error! So, it must be "session start". Sorry I was a bit confused there.
"Session start", okay? So refresh, resend and "You're in!"
We've started the session and this lets us add a session variable.
So, "You're in!". After this I'll say "Click here to enter the secret... no, the member page." okay?
And this is going to be a link to a page called "member dot php".
Let's just recap. As long as we send the right data we're going to say "Click here to enter the member page" which we haven't created yet.
Let me remind you here that we just created our "session start" here, which is very important.
We're going to create a session and to do this let me start and type here the dollar sign underscore session and then in brackets, in square brackets, we will give it a session name.
I'll call it "username" and that will be equal to our "username". I think I will say "dbusername" because that's a more direct value from the database.
We have our session set.
As long as the user is in their browser not the browser you evoked them with, then when we echo out our session in any page now, this will be set as a session.
To prove this I'll create a new page.
It'll be the "member dot php" page.
So I'll save this as "member dot php".
Here I'll say "echo" and I'll echo the "username session", okay?
In fact, I'll concatenate at the start "Welcome" and at the end, just to make it more expressive let me add an exclamation mark.
As long as we are logged in, this should run this command here, setting our session in our browser to our "username" on any other page we create.
If this is any other page over here and you use this code to set, it will work.
You will, of course, need this function inside here, as well.
So you need "session start" inside every page that you call or declare a session in.
There we restart. Let's go back to the main page.
I'll login with my details as "Alex" and "abc", click log in.
"You're in! Click here to enter the member page". Now as you can see, there is no error. I've successfully created my session.
If I click here, we get "Welcome!" We have got only that. Let's see what happened.
Let's go back and see what I've done wrong. This should be "username".
I'm not using any comparison here but I've put a double equals to sign here. That might be wrong.
Now this should work. Let's go back to our "index" page and let's log in again, as we did before.
Login, okay, "You're in! Click here to enter the member page". Click here and "Welcome, alex!".
Now I'll go back to the login page.
Most people would think all that data has been lost now.
If I go back to the member page which is "member dot php" and press enter it is still saying "alex".
And in some second-starts if I close my browser and reopen it and I go to "local host php academy" then go back to my page which is the "login" session and back to my member page I'm still logged in. okay?
So my user is logged in. If I close the browser I am still going to be logged in when I enter back.
This is a very useful function if you're doing this kind of logging in.
A lot of websites to do this to keep you logged in.
But now I want to create a log out page.
To log out all we need to do is, we need to create a separate page and let's just save it as "logout dot php".
And we need to end our session here.
First of all, before we destroy our session we need to start it.
So I'll type "session start" here. Let me confirm it once.
Ok and then we need to say "session destroy". Sorry, not sestroy, destroy.
If we run this page here, it will destroy our session.
Here I could type a friendly error message "You've been logged out. Click here to return".
Let me create a link back to our "index dot php" page.
Now I'll test this again. For example, lets say.....
Lets put a break there. Here I'll create a link to log out.
Its important to give the link the user to our "logout dot php" page otherwise they'll not know how to logout.
We refresh this and it will create a log out link, to log out from the php page.
Click on this and you get "You've been logged out. Click here to return."
I assume that we've been logged out. Trying to go back to our member page dot php.
We have got no variable here.
Now you don't want the users to get access to this page because they are not logged in right now.
So, here I'll say session start then I'll say "if session and the session name which is username.
Next I'll echo out my data to say "Welcome" else I'll say die.
No! Not me - I want my page to die! So I'll say "You must be logged in".
We're saying, if this session exists or if it has been created by using a correct username and password we can echo out our friendly message to say "Welcome" otherwise kill the page and say "You must be logged in!".
So, this is all in this part of the tutorial. Let me summarise this for you.
Remember I'm not logged in. So let me login.
I am in. There's my member page. I can log out. I return here.
Now as we've created member dot php, press Enter.
It displays, "You must be logged in!".
So for example I'm going to log in but I'll not click here to go.
I'll just forward myself to "member dot php". Message has been created and I've been allowed access.
Okay so that's it for now. This is the last part of this tutorial. If you have any questions on this I'll be more than happy to help.
This is Evan Varkey dubbing for the Spoken Tutorial Project. Bye.