‫All right, guys, so I know that we're skipping around a bit here, but that's kind of the point of
‫these first lessons is to make them modular so you can learn different things.
‫And then when we do our project we'll kind of put them all together.
‫All right.
‫So now we're going to talk about sessions because we're going to talk about the session Superglobal.
‫Now in web development maintaining user data across multiple requests is a fundamental requirement.
‫PHP provides a built in mechanism for the purpose of, you know, having this data that can reach across
‫multiple Http requests.
‫And they're called sessions.
‫And the session Superglobal enables developers to store and manage user specific data on the server
‫side, ensuring that it persists between Http requests.
‫So in this lesson, we're going to explore what session is how it works and how to use it effectively
‫in your PHP applications.
‫Now.
‫A session in the context of web development, represents a series of interactions or requests made by
‫a single user during their visit to a website.
‫Sessions are critical for tasks like authentication, authentication, shopping carts, user specific
‫preferences.
‫Without sessions, web applications would have no way of identifying and remembering individual users.
‫So every time you go to an application or a website, it would be like a brand new experience.
‫You'd have to re log in.
‫You'd have to, you know, it wouldn't save any shopping cart data.
‫Nothing like that.
‫Now we do have cookies which are on the the client side, and we're going to get to that next.
‫But the sessions are more secure in that it's stored on the server.
‫Now when you do store sessions it actually saves a cookie on your on your client or on your browser
‫with that session ID, so it can kind of map you to that session.
‫All right.
‫Now in order to use sessions, we have to run the function session start.
‫So we have to do that before anything if we're going to be using sessions.
‫And I'll get to that in a second before we jump in.
‫I just want to go over kind of the fundamentals of sessions.
‫So we have session initialization.
‫This is where the session ID is created and it's stored in a cookie.
‫As I said we have data storage.
‫So as the user interacts with the website you can store data specific to that user in the session Superglobal.
‫And it can be accessed and updated through the user session data retrieval.
‫So on subsequent requests, the server can retrieve the session data associated with that user using
‫the session ID stored in the cookie and then session termination.
‫So basically they can be terminated either explicitly or they may expire after a predefined period of
‫inactivity.
‫So this releases server resources.
‫And it also ensures privacy.
‫So your session isn't just sitting out there forever.
‫All right.
‫Now let's jump in and just look at the absolute basics on how to basically start a session, save some
‫data, etcetera.
‫So like I said, if you want to work with sessions then you're going to have to call session start.
‫And this is typically called at the very top of the page.
‫All right.
‫Now once we do that we can then save we can say session.
‫And let's say we want to save the user's name.
‫So when they come back later we can say hello John or whatever.
‫So we'll set that to John.
‫All right.
‫Now you can see what's in the session array with var dump or print.
‫Ah let's just do a print.
‫Ah.
‫And let's say session.
‫And I'm going to run this and you can see that we have named John.
‫All right.
‫Now we can create another page.
‫Right.
‫So let's say new I'll just call this page PHP.
‫And again if I want to use the sessions then I have to call.
‫Session start.
‫So usually like what we'll do in our application is we're going to be including files within each other.
‫So at the very top basically the entry point of the application will call session start.
‫That way we can use it throughout all the different pages.
‫So now what I'll do here.
‫And what did I save that as name.
‫So what I'll do is say if.
‫Let's say if is set.
‫Session.
‫Name.
‫Then I'm going to Echo Session.
‫Name and remember I'm on it.
‫This is going to be a different page.
‫So let's go to whatever your domain.
‫Mine's localhost 8000 and then slash page dot PHP.
‫And you'll see I get John.
‫Now this where I, where I set John is not anywhere in this file.
‫It's stored on the server.
‫Right.
‫And the session ID should be stored in a cookie.
‫Now we can check out the cookies if we go to application in our dev tools here.
‫And if we go to.
‫Let's see right here.
‫Cookies.
‫Click on that and you'll see we have this PHP session ID.
‫So this is actually the session ID right here.
‫The data isn't accessible through here.
‫That wouldn't be very secure.
‫But the session ID is just so the server knows where to where to find that data for this particular
‫user.
‫Okay.
‫So hopefully that makes sense.
‫Now if you want to destroy the session we could do that.
‫Let's create another page called destroy dot PHP.
‫And in here again we're going to have to call Session start.
‫And then I'm going to unset.
‫So there's a function called unset.
‫And let's pass in session.
‫And then name.
‫Okay, now you can also destroy the entire session by calling session underscore destroy.
‫And typically you'll do this with like with an authentication system.
‫So I'm going to go ahead and save that.
‫Now let's go to.
‫So if I'm on page dot PHP and I reload, we see John.
‫But if I go to.
‫Uh c destroy dot php.
‫It doesn't show anything, but it ran this code.
‫So now I'm going to go back.
‫To page dot PHP and it doesn't show John anymore.
‫In fact, let's do this.
‫Let's do an else and we'll say echo.
‫Name is not set just so it's a little more clear.
‫Like what's going on here?
‫Name is not set.
‫And then in destroy will echo down here.
‫Name or we'll just say session.
‫Destroyed.
‫Okay.
‫Now, if I go back to index.
‫Let's do this.
‫Let's say if.
‫If the session.
‫Name or if is set, I should say.
‫So if that's set, then I'll echo and say name and then we'll concatenate onto that the.
‫Session.
‫Name.
‫Else.
‫We'll say name is not set.
‫All right, so if I go to.
‫Index.php.
‫It sets right.
‫We're not going to see name not set because we set it right here.
‫So it says name John.
‫Then we go to slash page dot PHP.
‫We see John.
‫Then we go to slash.
‫Destroy session destroyed.
‫We go back.
‫To page dot PHP and name is not set.
‫If I go back to index.
‫Then it's going to get set again, and you'll have all this stuff within specific functions or within
‫methods of classes.
‫And we'll get to that.
‫We're going to create a whole login system with our project that's going to do all that.