[Week 9] [David J. Malan - Harvard University] [This is CS50. - CS50.TV] All right. Welcome back. This is CS50, and this is the start of week 9. Today we focus in particular on design, no longer in the context of C but in the context of PHP and a bit of SQL and a bit of JavaScript, particularly toward an end of both pset 7 and also your final project. In fact, if you are at that point in your final project where presumably as of an hour or so ago you at least started to give some thought to your final project and you're thinking you'd like to collaborate with 1 or 2 classmates, if you're having trouble connecting with said classmates, feel free to fill out the form at cs50.net/partners/form. It just asks you who you are, what kind of project you're thinking about, where you live just for logistical reasons. And then if you want to keep an eye on over the next week or so the spreadsheet URL there, you can then see a read-only version of the Google doc in which we're collecting that information. So if you want to work with someone, by all means feel free to reach out to people via that mechanism. But the majority of folks do work solo. That's totally fine. So don't feel that this is in any way obligatory. On Friday it was just me and a few of the team in here, empty theater for the most part. There were 3 tourists sitting up there, so that was a little awkward. What we talked about was databases and we talked about pset 7 a little bit. And if you didn't happen to catch that on video just yet, that's fine. I'll try to define any terms that we would otherwise take for granted based on Friday's lecture. But today we're going to try to get you to the point of not just being able to do something like pset 7 but really understanding what's going on underneath the hood, particularly some of the abstractions that we put in place in the functions.php file to make your lives a bit easier but so that you ultimately understand so that when the training wheels come off in a few weeks you can still survive in the real world and do this stuff without any CS50 framework underneath you. This $_SESSION, for those of you who are familiar or who already caught the video on Friday, what does SESSION let us do in a PHP-based web application? This is a superglobal variable, which means it's similar in spirit to GET and POST and a few others, but what is this thing useful for? What is SESSION used for? Yeah. [student] Logging in. Sorry? [student] Logging in. Logging in. Indeed. In pset 7 we're using this SESSION superglobal to facilitate logging in. And what's nice about this superglobal is that it's an associative array. An associative array, recall, is just an array but whose indices no longer have to be numbers like 012. They can be numbers or they can be even strings. And so if you've dived into pset 7 yet, you may recall that we are storing a key called ID inside of this associative array whose value is something like 123-- whatever the currently logged in user's ID is. The motivation for this is that even after the user has visited localhost or my website more generally and then they've logged in, even if they don't click a link or return to my website for 5 minutes or even an hour or even a day but they leave their browser window open, via this superglobal can I remember that they are logged in. In other words, it allows me to store slightly long term anything I want about a user. And you can think of it really as the incarnation of a shopping cart. Places like Amazon obviously let you put things into a shopping cart, but HTTP, the protocol that powers the Web, is stateless in the sense that when you visit a website, for the most part you don't have some constant network connection between your browser and the server. As soon as you've downloaded the HTML and the JPEGs and the GIFs and all that, the connection goes away and you just have a copy of the HTML and whatnot from the server. But if the server wants to remember something about you, the burden is on the server to actually record that information. And so you the programmer who have control over the server can put most anything you want inside of this superglobal associative array and it will be there the next time the user comes back, whether it's minutes or even days later, unless they close their browser window, at which point SESSION disappears. So it's ephemeral storage, it's non-persistent, and it's meant to go away as soon as the user closes their browser--not just that tab, often the entire browser, thereby effectively logging the user out. So how is this thing actually implemented? Let's take a quick look at a simple example we looked at on Friday. For those unfamiliar, it was as simple as this. This is a web page whose sole purpose in life is to tell me how many times I have visited this page. This is the first time here on Monday that I visited it, so it says 0 times. But if I start reloading this page, it says 1 time, 2, 3, 4, 5, and this will eventually just keep on counting up, up, up, up, up for each time I actually click Reload on it. So how is this working? Let me go inside of this file called counter.php. The top part of it is all blue comments, but the interesting part is here. On line 13 we call this function session_start, and that is literally all you need to do if you want to have access to this special superglobal called $_SESSION. That makes it all possible, and we'll see in a moment how that's all possible. In line 16 notice what I'm doing. If the key, called counter--in other words, the index value--"counter" exists inside of this array called SESSION, then what am I doing with it in the line below? What is line 18 doing? [inaudible student response] What's that? [student] Storing the value. Good. It's storing the value that's in SESSION right now in a new local temporary variable, $counter in all lowercase. Notice that PHP is already being a little lazy here. Notice we don't have any mention of int or float or string or anything like that because PHP is weakly typed, whereby you don't have to specify the type of a variable, and in this case here I've not even declared it yet. I'm declaring it inside of these curly braces and unlike C, this is actually okay. No matter how deeply nested a variable's declaration is in PHP-- inside of curly brace, inside of curly brace and the like-- it will at that moment in time exist for the remainder of the program, for better or for worse. So it immediately becomes global as soon as you define it as we're doing here. Otherwise, if I do not find that there's anything in the SESSION superglobal, I'm apparently initializing this variable counter to 0, thereby just assuming the user has never been here before. And then this of course is incrementing the counter how? I'm updating the value that's inside of this associative array by setting it equal to whatever counter currently is + 1. If I scroll down here to the HTML of the page, it's actually pretty simple. All I have in the body of this page is, "You have visited this site so-and-so times." And this is a PHP construct. If you do