‫All right guys.
‫So now we're going to get into sessions.
‫We're able to to register a user and add them to the database.
‫So once they register we want to log them in.
‫And when I say log them in I basically just mean create a session okay.
‫So it'll store a session on the server.
‫And they'll also be a cookie in the browser that has that session ID.
‫So let's create a class that handles just about everything that we do with sessions.
‫So that's going to go in our framework because that's a core part of this application.
‫So let's create a new file called session dot php uppercase S.
‫And this is going to be in the namespace frame framework namespace.
‫So let's say namespace framework.
‫And then our class session.
‫And these are all going to be static methods, meaning we don't have to instantiate them.
‫We just call them when we want.
‫There's no need to create multiple instances.
‫So the first one is going to be to start a session.
‫And what we're doing right now is in our public index.php which is our entry point.
‫We're calling session start.
‫But I want to have a method that we can call that will do that.
‫So let's create a little comment here.
‫We'll say start the session.
‫And it's going to return.
‫Void.
‫So say public static.
‫Okay.
‫Make sure you do static function and we'll call it start.
‫And what we'll do is, is first we check the status.
‫So basically let me just show you this real quick.
‫If we I'm just going to go to my index.php.
‫You guys don't have to do this but I'm going to do an inspect.
‫Inspect and die and I'm going to pass in here.
‫Session.
‫Underscore status, which is a function, and let's just see what that gives us.
‫So if I reload I don't know if you guys can see that, but it says two.
‫It's an int of two.
‫Now this session state or session status can be zero 1 or 2 zero means it's disabled means that sessions
‫are disabled on the server.
‫One means that sessions are enabled, but no session has been started.
‫And two means that there's an active session.
‫Okay, so since we have session start and then we call session status, it's a two.
‫If I comment this out and then come back and reload, then it's a one because session there is no session.
‫So what I want to do is get rid of that, inspect what I want to do in our class before we start the
‫session is just make sure there's not one already started.
‫So let's say if and then session underscore status.
‫And we can actually.
‫Check that with a constant called PHP underscore session underscore none.
‫Now see this active would be two.
‫None would be one and disabled would be zero.
‫So we can we can check to see if there's no session.
‫And if not then we'll call session underscore start.
‫All right.
‫Now I want to use this this method in our index.php.
‫So there's a couple things we're going to do here.
‫We're going to get rid of the session start here.
‫And then.
‫Where we have our use.
‫I'm actually going to move the helpers down a little bit, because I want the session start to be as
‫close to the top as possible, but in order to use that session class, we have to say use.
‫Framework slash session.
‫And remember, we have our auto loader set up so that we don't have to require the session file.
‫That class is created automatically.
‫Or I should say that file is included automatically.
‫Now underneath that is where I'm going to call session.
‫So that's our class.
‫And then start which is our method.
‫And now just to check to see where we're at I'm going to put that inspect and die back and look at session
‫underscore status.
‫Right.
‫And we should see a two because it should have started with this.
‫So let's reload and we have two.
‫If I comment this session start out then we have one.
‫Good.
‫So that's the first change I want to make here.
‫And.
‫And you don't have to do this like you don't have to have a session class.
‫But I think it really makes things much cleaner and much neater.
‫If we can do this, rather than just using the super global willy nilly throughout the application.
‫So let's see, the next method that I want to create is going to be a method that will set a session.
‫So and all these methods are going to be really simple just like most of them are one line.
‫So let's we'll add a comment here and we'll say set a session key value pair.
‫And it's going to take in a pram.
‫String.
‫Which will be the key.
‫And then let's say peram.
‫And we'll say mixed for the value.
‫And then return.
‫Will be void because we're not actually returning anything.
‫We're just we're just setting a session variable.
‫So let's say public static.
‫Function and we'll call it set.
‫It's going to take in a key and a value.
‫And we're just going to simply do session.
‫So our superglobal session and then whatever the key is and set that equal to the value.
‫So very very simple.
‫So that will create a session key value pair.
‫And then we want to be able to get it as well.
‫So let's say get a session value by the key.
‫And we'll say param.
‫And this is going to be a string.
‫Key.
‫And Peram.
‫We'll say mixed.
‫And default.
‫And the return.
‫So this is going to return the actual value.
‫We'll just say mixed here.
‫And then.
‫To create our function.
‫Make sure it's static.
‫And we'll call this get.
‫And it's going to take in key and then default.
‫Now default I'm going to make that default to null.
‫So basically if it's not there it's going to return null.
‫But you could pass something else in if you wanted to.
‫So let's return and we'll say is set.
‫And then use our session superglobal and pass in whatever that key is.
‫And then we'll use a ternary.
‫We'll say if that's set close that up.
‫So if it's set then we want to return it.
‫So session.
‫Session and the key.
‫Else then.
‫Default, which is null by default.
‫Okay, now the next thing we want to do is, is check to see if a session exists.
‫So we'll call that one has.
‫So let's say check.
‫If session key exists.
‫And it's going to take in the key.
‫And yeah, that's it.
‫That's all it's going to take in and it's going to return a boolean.
‫So let's say public static.
‫And we'll call it has it's going to take in the key and we're just going to simply return.
‫Is set.
‫And then session key.
‫And that's it.
‫That'll be a true or false.
‫So next I want to be able to clear a session by the key.
‫So let's say.
‫Clear session by key.
‫Takes in the key and.
‫For return that will be void.
‫Call this clear takes in key.
‫And we're just going to first check to see if it is set.
‫So if is set.
‫Then we're going to unset.
‫Unset session.
‫Key.
‫Okay, so that's clear.
‫And then finally I just want to have one to clear all sessions, basically all session data.
‫I'll say clear all session data.
‫And that's not going to take anything in.
‫But we'll put the return.
‫A void.
‫So let's call this one.
‫Clear all.
‫And I'm just going to call session underscore onset.
‫And I'm not going to pass anything in.
‫And then I'm also going to call session destroy.
‫Okay, so that's all I'm going to do for now.
‫We are going to add a couple class, a couple methods later to deal with flash flash messages.
‫But this is good for now.
‫So let's now make it so when a user registers let's just clear this up and we'll go to register.
‫Once a user fills this out and registers, we want to then create a new session that has the user information
‫in it.
‫Now what you put in it is is really up to you.
‫Sometimes you'll put just the user ID, but I'm going to put the user ID, the name, email, city and
‫state basically everything except the password obviously, because you might need to access that stuff.
‫And yeah, you could make a request to get it, but I think it's just easier when it's just right in
‫the session.
‫So now what we want to do is go into the user controller.
‫So app.
‫Controllers, user controller, and at the very top we want to make sure that we use framework slash
‫session.
‫Okay.
‫Make sure you add that and then down at the bottom in the store method.
‫Right.
‫So here's the store method which is our register where we store the user.
‫We're already checking to see if the email exists.
‫Validation made the query.
‫It's going in the database.
‫But we want to create a session.
‫Now in that session we want to have the user's ID.
‫And since this is a new user right now, it's not available to us.
‫But there is a way that we can get it through the connection property.
‫There's actually a method called last insert ID.
‫So right here let's say get user ID or get the new user id.
‫And we're going to put it in a variable called user ID.
‫And then we can say this.
‫DB.
‫And then the connection.
‫So con remember that's a property we have.
‫And there's a method that we can use called last insert ID.
‫And it does just that.
‫You can see the last inserted row or sequence value returns the ID of the last inserted row or sequence
‫value.
‫So once we get that user id then we want to save it to the session.
‫So here let's use our session class and use the set method.
‫And we're going to set it as user.
‫That will be the key.
‫And then the value is going to be an array.
‫And you could set this value to anything.
‫You could put it to just the user ID.
‫But I want to have some other information available in here.
‫So I'm going to set the ID to that user ID that we just got.
‫And then I'm going to set the name to name.
‫And I'll just copy that down a couple times.
‫So name email.
‫So then we have easy access to the logged in user's email.
‫And then what else the we'll do the city and state in case you have some map functions or something
‫like that.
‫So city and state.
‫So you can put anything you want in this session.
‫Data.
‫I think that'll do it though.
‫Okay.
‫So.
‫This should create the session.
‫So what I'm going to do is before we redirect.
‫Or instead of redirecting, let's say inspect and die.
‫And let's see we have our get.
‫Yeah.
‫So our get will.
‫Return the value.
‫So let's do that.
‫Let's say session.
‫Session get and pass in user.
‫So now let's go to our register form.
‫And actually I'm going to delete.
‫You can register whatever user you want but I'm going to register this Brad user again.
‫So I'm going to delete this row.
‫And if you delete a row here you make sure that you click apply or else it won't actually run the delete.
‫All right.
‫So now if I select all users again you can see that these three John, Jane and Steve are the only users
‫in my database right now.
‫So now I'm going to add.
‫Myself.
‫Say Brad at gmail.
‫And city state.
‫Password.
‫Of course, the password will be hashed.
‫Register and there we go.
‫So this is actually the data that is in my session right now because I'm showing this the get whatever's
‫in user.
‫Okay, so I'm essentially logged in now I'm just going to get rid of that inspect and die and then.
‫Let's just go back to the home page.
‫Now, there's really no visual way to see that.
‫I'm logged in and there's a session created.
‫So in the next video we're going to make it.
‫So if you're logged in these links will change.
‫And you'll see a log out link.
‫And you'll only see the log in and register if you're not logged in.
‫So we'll do that next.