Hello and welcome to this tutorial on php sessions.
Sessions are quiet similar to cookies.
However sessions only have a temporary time - expiry time.
They will be destroyed as soon as the browser is closed - all the connection to the page is lost.
So sessions are not like cookies because you can't set a particular expiry time.
And they are not stored in the same way.
I mean, the "id" of the session can be stored in the cookie.
Or you might have seen in a browser URL something like this.
I don't remember the name - something equals and a lot of numbers and alphabets as well.
So basically sessions are very similar to cookies.
However they are not stored for a long period of time - only until the user closes the browser.
Okay - So sessions are different.
First of all, we need to declare or call this function called 'session_start'.
Now this needs to be at the top of every page that you use sessions in.
So if you don't have this and you are trying to echo out a session value or you're trying to set a session, it won't work.
You need the session start code there.
Now I'll show you the error that comes up when you don't use this so you'll remember it.
Creating a session is very easy.
Use 'dollar underscore session' and in square brackets give the name of the session.
I will type name and equal this value to something.
It could be a string data or the new written data.
Okay so our session is set here.
Lets run this for the first time.
So lets refresh.
Okay nothing has happened.
I'll comment out this code like I did in my 'Cookies' tutorial.
If you haven't seen that please do so.
Next I'll echo out the value of the session that I have set.
So that's 'name'.
Please note that this is not going to be executed.
This will be on a completely new page, for all you know.
But here I'm just starting my session.
I'm getting a session called 'name' that has already been stored by our server.
So lets refresh we can see that is equal to 'Alex'.
So you can add this and this code onto any page.
So you can have your session start and echo your session name onto your page as long as this has been started on any page of the current session of the browser.
So for example if I create a new page, add my php code here and say session start.
And then echo out the session 'name'.
And I'll save this in my sessions folder as new page or new dot php.
So when we come back to our page here and we click here, we type new dot php.
We get exactly the same value, even though we are not working on the same page that we created our session in, we are still able to access it.
However if I was to close my browser and reopen it, this session probably would not exist.
Hope that is clear. Now let me show you what happens if you don't put the session start in.
You get something similar to this.
Lets go back and check it.
What happened here is that we are not getting any output because we haven't started our session.
When we type 'session_start', you can see we get our value as output.
The reason I don't have an output is because I don't have that type of error reporting on.
But if you did have a specific type of error reporting on, I have a tutorial on that as well, then you would probably get an error.
So you can close this now and I'll show you how to 'unset' a session.
There area 2 ways to do this is.
Either unset and then session in brackets to unset our session.
Or use a completely different command altogether and that is 'session_destroy'.
And the difference between these two commands is 'sessions_destroy' will completely destroy sessions that you currently hold.
And 'unset' will unset a particular session.
So its your choice - You can log the user out and say 'session_destroy'.
That would clear all current session variables that you hold at the present time.
Or otherwise you can just 'unset' a specific one.
So what are sessions useful for?
If you've come into a website and see a box like 'Remember me' and you don't check this box, then you would probably be using sessions.
Because once the users' browser has been closed, you would be logged out.
And when you come back to the website you'll have to re-type your details like your user name and password to login to the website.
But its different if you are using cookies because you set an expiry time - which means that your username will be logged in or this cookie will be held until you decide to destroy it.
And we have to create a code to destroy our cookie, like I showed in my 'Cookies' tutorial.
So really its your choice whether you use sessions or cookies.
Sessions are good for short term - Cookies are good for long term - specific amount of time that you want a piece of data to be held.
But if you've gone through my php Project - 'Register and login', you'll see that I use sessions.
This is because I need to use sessions when I'm creating tutorials.
However you can take any form of this.
It could be a cookie, it could be a session, it is really your choice whether you want to keep the user logged in for a long period of time or not.
So if you've got any questions on this, feel free to contact me.
Make sure you subscribe to phpacademy.
Thanks for watching. This is Evan Varkey dubbing for the Spoken Tutorial Project.