Locked lesson.
About this lesson
Exercise files
Download this lesson’s related exercise files.
Sessions.docx59.1 KB Sessions - Solution.docx
59.2 KB
Quick reference
Sessions
Session variables are superglobals that allow you to pass things between php files.
When to use
Anytime you have data that you want to pass from file to file, you can use Session Variables.
Instructions
To create a Session, use the session_start function:
session_start();
You need to do that for every page of your app that needs to use session variables.
To create a session variable:
$_SESSION["fav_pizza"] = "pepperoni";
To access that session variable:
echo $_SESSION["fav_pizza"];
Hints & tips
- Session Variables are superglobals
- session_start(); //start a session
- $_SESSION["variable_name"] = "Data you want to save";
- 00:04 In this video, I wanna talk about session variables and
- 00:07 session variables are one of the super global we looked at a couple of years ago.
- 00:11 So what exactly are they?
- 00:12 Well, they're sort of like cookies in
- 00:14 that they let us save information across different pages of our app.
- 00:19 But unlike cookies,
- 00:19 we're not actually saving that information on a user's computer.
- 00:22 So here, I've created our index page and I 've
- 00:25 just created this regular variable, favorite pizza named it pepperoni and
- 00:29 then I'm echoing out onto the screen.
- 00:31 So if I come back here and hit reload, we'll see pepperoni.
- 00:33 So then I've created a second web page, index2.php and
- 00:37 I've just echoed out that same variable.
- 00:39 So if we save this, come back here and just go to index2.php.
- 00:45 You can see it's not there, because this variable only exists on this page.
- 00:50 It doesn't exist on this page.
- 00:52 We haven't set this variable yet.
- 00:53 Well, with session variables, we can set it on one page and
- 00:56 use it on all the pages in our app.
- 00:57 So super, super useful.
- 00:59 To do that, what we do is we create a session.
- 01:02 So here at the top of the screen, I'm just gonna do a php tag and start session and
- 01:08 I'm gonna copy this and I'm gonna put this also on our second page, save that.
- 01:14 So that's what you have to do to start a session.
- 01:16 Now like I said earlier to create a session variable,
- 01:19 we use the session super global.
- 01:21 And that's just if you remember back, it's just that.
- 01:24 We can set it and let's call this fav_pizza, all right?
- 01:29 And I'm gonna get rid of that, get rid of that and
- 01:32 I'm gonna do the same thing on the second page.
- 01:35 So we can start from scratch here.
- 01:36 So session equals or session fav_pizza, we just set that equal to whatever we want.
- 01:43 I'm gonna say, lets change it.
- 01:45 Cheese and save that.
- 01:47 Now to echo that out, we just echo $_SESSION and then fav_pizza.
- 01:54 So if we save this, come back here to our first page and click reload,
- 01:58 we see cheese.
- 01:59 Now if we go forward to our second page and
- 02:01 hit reload, still nothing has happened, because we haven't echoed this out.
- 02:05 So let's copy this, come back over here and echo this out.
- 02:09 So echo $_SESSION fav_pizza.
- 02:12 Save this, come back here and hit reload.
- 02:14 And boom, we've cheese.
- 02:15 So you can see on this index2 page, we haven't created this variable
- 02:20 anywhere we haven't assigned anything to this variable anywhere.
- 02:24 We did all that on our first page, our index.php page.
- 02:28 But now any page we create in our app, we can use this session variable and
- 02:33 it will know that it's cheese, because we said it on this index page.
- 02:37 Now like I said, these are like cookies.
- 02:39 These are set for specific people.
- 02:42 So if one person comes to your website, it will set for them and
- 02:45 this session variable will be available to that person as long as they keep their
- 02:50 web browser open.
- 02:51 As soon as they close their web browser, the session ends and
- 02:54 the session variable disappears.
- 02:56 So that's a very sort of important distinction.
- 02:58 Now, there's lots of ways to pass variables around from page to page.
- 03:02 You can use a form and the GET method.
- 03:05 We talked about web forms earlier, but that's sort of clunky and it's hard to do
- 03:09 or you can just use the session variable to pass things from page to page and
- 03:13 it's very very useful.
- 03:14 So that's sessions.
- 03:16 We're done talking about superglobals now.
- 03:18 In the next video, we're gonna start building our math flash card app.
- 03:21 It's gonna be a lot of fun and that's all for this video.
Lesson notes are only available for subscribers.