PHP-and-MySQL/C4/Sessions/English-timed
From Script | Spoken-Tutorial
Time | Narration |
00:00 | Hello and welcome to this tutorial on php sessions. |
00:05 | Sessions are quite similar to cookies. |
00:08 | However, sessions only have a temporary time i.e expiry time. |
00:12 | They will be destroyed as soon as the browser is closed and all the connection to the page is lost. |
00:19 | So, sessions are not like cookies because you can't set a particular expiry time |
00:24 | and they are not stored in the same way. |
00:28 | I mean, the "id" of the session can be stored in the cookie. |
00:34 | Or you might have seen in a browser URL something like this. |
00:40 | I don't remember the name - something equals and a lot of numbers and alphabets as well. |
00:47 | So, basically sessions are very similar to cookies. |
00:50 | However, they are not stored for a long period of time; only until the user closes the browser. |
00:57 | Okay. So sessions are different. |
01:00 | First of all, we need to declare or call this function called 'session_start()'. |
01:09 | Now, this needs to be at the top of every page that you use sessions in. |
01:14 | 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. |
01:22 | You need the session start code there. |
01:24 | Now I'll show you the error that comes up when you don't use this, so you'll remember it. |
01:30 | Creating a session is very easy. |
01:34 | Use 'dollar underscore SESSION' and in square brackets give the name of the session. |
01:40 | I will type 'name' and equal this value to something. |
01:44 | It could be a string data or the new written data. |
01:48 | Okay, so our session is set here. |
01:50 | Let's run this for the first time. |
01:53 | So, let's refresh. |
01:56 | Okay, nothing has happened. |
01:58 | I'll comment out this code like I did in my Cookies tutorial. |
02:01 | If you haven't seen that, please do so. |
02:04 | Next I'll echo out the value of the session that I have set. |
02:08 | So that's 'name'. |
02:11 | Please note that this is not going to be executed. |
02:15 | This will be on a completely new page, for all you know. |
02:19 | But here I'm just starting my session. |
02:21 | I'm getting a session called 'name' that has already been stored by our server. |
02:26 | So, let's refresh we can see that is equal to "Alex". |
02:29 | So, you can add this and this code onto any page. |
02:33 | 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. |
02:44 | So, for example, if I create a new page, add my php code here and say session_start(). |
02:49 | And then echo out the session 'name'. |
02:56 | And I'll save this in my 'sessions' folder as new page or 'new dot php'. |
03:03 | So, when we come back to our page here and we click here, we type 'new dot php'. |
03:10 | 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. |
03:18 | However, if I was to close my browser and reopen it, this session probably would not exist. |
03:25 | Hope that is clear. Now let me show you what happens if you don't put the session_start() in. |
03:31 | You get something similar to this. |
03:33 | Let's go back and check it. |
03:36 | What happened here is that we are not getting any output because we haven't started our session. |
03:44 | When we type 'session_start', you can see we get our value as output. |
03:51 | The reason I don't have an output is because I don't have that type of error reporting on. |
03:56 | 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. |
04:06 | So you can close this now and I'll show you how to 'unset' a session. |
04:10 | There area 2 ways to do this is. |
04:12 | Either unset and then session in brackets to unset our session. |
04:16 | Or use a completely different command altogether and that is session_destroy(). |
04:27 | And the difference between these two commands is 'sessions_destroy' will completely destroy sessions that you currently hold |
04:35 | and 'unset' will unset a particular session. |
04:40 | So, it's your choice, you can log the user out and say session_destroy(). |
04:46 | That would clear all current session variables that you hold at the present time. |
04:50 | Or otherwise you can just 'unset' a specific one. |
04:53 | So, what are sessions useful for? |
04:55 | 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. |
05:03 | Because once the users' browser has been closed, you would be logged out. |
05:09 | 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. |
05:17 | But it's 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. |
05:30 | And we have to create a code to destroy our cookie, like I showed in my 'Cookies' tutorial. |
05:35 | So, really it's your choice whether you use sessions or cookies. |
05:40 | 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. |
05:49 | But if you've gone through my php Project - 'Register and login', you'll see that I use sessions. |
05:56 | This is because I need to use sessions when I'm creating tutorials. |
06:00 | However you can take any form of this. |
06:03 | 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. |
06:11 | So, if you've got any questions on this, feel free to contact me. |
06:16 | Make sure you subscribe to phpacademy. |
06:20 | Thanks for watching. This is Evan Varkey, dubbing for the Spoken Tutorial Project. |