در حال بارگذاری ...

Introducing Session Superglobal1:03

So this is my session. This is their session. Okay. That's good enough to get us started. So let's go into our project here and yeah, let's just play around. I'm going to go into the IndexController for the homepage. Now you've learned about super globals. There's a $_POST super global. There's a $_GET super global. There's a $_SERVER.

There's a get super global. There's a $_SERVER. There's a $_REQUEST. There's also one related to sessions and this will take the exact same shape of $_SESSION and we can interact with it as an array. So why don't we start by putting my name into the session and real quick, I will warn you this isn't quite going to work the first try, but I still want to do it this way so that you have a better understanding of why we must do one step prior to this to get everything working. Okay.

Storing and Reading Session2:34

When I visit the homepage, I'm going to put something into the session and then when I go to the about page, I want to grab that information out of the session. So why don't we do this? Let's just dd and dump and let's see if I can grab the name out of the session and let's see if that works. All right. I'm going to go to my browser. Okay. So now at least we think we've put my name into the session. So now when I go to the about page and we try to fetch my name out of the session, does

Starting Sessions with session_start3:16

Okay. Well, here's the issue. Anytime you intend to work with sessions in your application, the first thing you have to do is start up the session. You can't interact with that super global until you have started the session. And generally you want to do that as early as possible. So why don't we do it in index.php? You know, it doesn't have to be the first line, but just for now, let's put it right up here at the start. I will call a function session_start().

up here at the start. I will call a function session_start. Okay. So with no other change, let's give it another try. I come back to the homepage. We give it a refresh. We started up a session. We then put my name into the session. And now when I go to the about page, we fetch it out of the session. Okay.

And yeah, I've reopened Firefox from scratch. And sure enough, all of that prior session data has been destroyed. So again, I don't mean to reiterate, but this is a key thing to understand. Session data is temporary. Okay, great. So let's go back to the homepage where we put something into the session. And then when I go to the about page, we can grab that data. All right. So let's do this. So let's go back to our AboutController and get rid of this die-and-dump call.

Displaying Session Data in View4:40

So let's do this. So let's go back to our AboutController and get rid of this die-and-dump call. And instead, I want to echo the person's name within the view, maybe within the banner. So we'll go into views/partials/banner. And yeah, at the moment, we are dynamically displaying a heading. But yeah, just temporarily, let's swap that out with hello. And then I will reach into the session and grab the person's name, just like that. All right. So let's go back to the browser and refresh. And sure enough, I do get hello, Jeffrey.

So let's go back to the browser and refresh. And sure enough, I do get hello, Jeffrey. And that will persist across every page for the lifetime of my current session. So just keep in mind, once again, if we close out Firefox and bring it back, we get a warning. And of course, if you think about it for more than five seconds, this makes perfect sense. Think about it. Within index.php, we started a session, and then we hit our about controller. That loaded a view. The view loads a banner. And then we tried to fetch name out of the session.

The view loads a banner. And then we tried to fetch name out of the session. But at this point, there's nothing in the session. Because when I closed the browser before, it was destroyed. So now we're starting from scratch. And yeah, php's saying, you don't have anything in the session for name. The point where we put it into the session is within the homepage. And you may not always load the homepage first, of course. So yeah, notice if I go to home, then we put Jeffrey into the session. It's saved on the server and as a cookie on the browser, and I'll show you that in

So yeah, notice if I go to home, then we put Jeffrey into the session. It's saved on the server and as a cookie on the browser, and I'll show you that in just a minute. And then for repeated page requests, it is remembered. So yeah, in plenty of situations, what you'll need to do is check, well, is this available? Otherwise, use a default or render something different, like guest. Hello session name or guest. OK, so now my name is in the session. So we say, hello, Jeffrey. Once again, let's restart.

Finding Session Files on Server9:08

Usually, the default is going to be some kind of temporary temp directory on your server. But yeah, again, whether you're on a Mac or Linux or a PC, it might be a little different, and you might want to Google for five minutes to figure out how to find this file. I'm going to run php and then the -i flag. And this will spit out a lot of information related to my current server. So yeah, now I want to figure out what is the path to where these session files are being saved. And I just happen to know it's called session.save_path. Yeah, there it is. Now in my case, it's set to no value.

Yeah, there it is. Now in my case, it's set to no value. On your machine, it might be the same, or it might just tell you exactly where that path is. And if that's the case, simply copy it and cd to that directory. Otherwise, if there's no value set, then it defaults to the default. And that default is, again, the temporary directory. So let me see on my computer where that is. I'm going to echo. And again, this is probably unique to Mac, but I'm going to look for temp directory.

OK, let's drag that into my editor. And there we go. Name is Jeffrey. OK, so now we know. This is pretty cool. Now we know when you write to the session, those attributes and that data is literally being written to a file on your server. Let's try another one. Let's go back to, actually, let's just stay on, how about contact.php. And I'm going to write something else to the session.

Let's go back to, actually, let's just stay on, how about contact.php. And I'm going to write something else to the session. How about we'll do my last name, Wei. OK, let's come back to Firefox. And we'll go to Home, About, Notes, Contact. And at this point, our session should now have two keys, Name and Last. All right, let's come back. Let's return to that session file. And there we go. We wrote to the session.

Resetting Sessions via Cookies12:54

well, let's just go ahead and do it. Delete the cookie. And now, when we start up a new session, well, we don't send a cookie to the server. So php picks up on that and it says, OK, I'm just going to start up a new session. And notice that this one has a different value. So if I go back to my Finder, notice that, well, yes, we still have the old session file, and we'll talk about that in the next episode. But yeah, now, because the cookie was deleted,

SessionsSession FilesCookies

دوست دارید گاهی خبرهای Laracasts را ایمیل کنیم؟