 Hey everybody, this is Brian and welcome to the 28th tutorial with LAMP. Today we're going to be discussing sessions. So what is a session? Well, a session is like a cookie in the sense that you can store data about your user. But instead of storing it on the user's computer, you're storing it on the server itself in memory. Now the thing about a session is it's not persistent, meaning when the user logs off your website, the session goes with it. So let's dig right in here. The first thing you should know is we need to start the session. So we'll say session start. And notice where I'm putting this. I'm putting it right above before we do anything. Before we write anything to the browser, we want to do session start. And every single page that you want to use a session, you need that. Now the premise of this tutorial is I'm going to write the session information on tutorial 28.php and actually read it back on session.php just so you can see that the session will last multiple pages. So let's just dig right in here and we'll say, oops, session. Notice how there is a global variable called session. And as you might have guessed, it's an array. So we're just going to add an element here and we're just going to add our name. And then we're just going to add our age. So what's going on here is we're calling session start. And then we use the global session variable to actually add elements. And I've got a hyperlink here that when we click, we'll just go to the session.php page. So I'm going to save this. And when I save it in Komodo ID, it automatically refreshes the browser. So that did write to the session. Notice in session.php, the page, we're going to read it back. We have to use session start. And then we're just going to echo these back out. Having a bad typing day. It's been one of those days, you know. Bad day at the office. Glad to be home. And we'll save that. We're going to click there to go to the session and you see how it says this is page two. Because this is our session page. And we're just reading back the age, or I'm sorry, the name. Now you can unset and check for set. Now how do you do that? And what am I talking about? Well let's say you want to check to see if a user is logged in. For example, you have a session variable and we'll call it user ID. But we don't know if it's actually been set or not. So you'll say if is set. And the is set function will just return a bull indicating whether or not this has actually been set in memory. If it hasn't, returns false. So we're just going to say, let's see here. Just going to say user is logged in. Wow, I must accidentally hit tab by mistake there. Now some of you are chuckling at my typing. This virtual machine is actually a little bit slow so I get some lag. So what we're going to do here is we're just going to say if is set and then we're checking the value of user ID in the session. If it is set, we're just going to say this user is logged in. If it's not set, the user is not logged in. Now how did we get this user ID you're asking? Well this would be our authentication method. We would just simply set a session variable. So if you were at your main login page and they logged in successfully, you would simply set the session variable. Then when they hit your landing page, you check to see if they are logged in. If not, you spin them back to the login page. If they are, you do whatever logic you want to do. So let's actually refresh this. User is not logged in. Because we've never set the user ID. Let's actually set it. Let's go back to Tut28 here and let's actually set this. That was bad typing. Wow. And I'm just going to use the word yes as a user ID. I don't know why, but it sounded good. So we're going to save this and then when we go back to our session, user is logged in because the user ID is set. Now to unset a variable, like let's say the user logged out, you can actually just do unset. Unset will, well you guessed it, unset that value so it no longer exists in the session. Now there is another command, session destroy. So if you really wanted to just kill the session altogether, you would say session destroy and that will actually delete everything for that session. This is done automatically when the user closes their last browser to your website. So really there's no need to call session destroy. I've seen some people where they will do session start, set some session variables and then do session destroy thinking that from a C++ perspective they need to destroy it from memory. Now when you do that, you're erasing the entire session so don't do session destroy unless you absolutely want to log the user out or get rid of all your settings. That's all for this tutorial. I hope you found this educational and entertaining and thank you for watching.