Start learning with our library of video tutorials taught by experts. Get started
Viewed by members. in countries. members currently watching.
PHP is a popular, reliable programming language at the foundation of many smart, data-driven websites. This comprehensive course from Kevin Skoglund helps developers learn the basics of PHP (including variables, logical expressions, loops, and functions), understand how to connect PHP to a MySQL database, and gain experience developing a complete web application with site navigation, form validation, and a password-protected admin area. Kevin also covers the basic CRUD routines for updating a database, debugging techniques, and usable user interfaces. Along the way, he provides practical advice, offers examples of best practices, and demonstrates refactoring techniques to improve existing code.
We now know how to set a cookie using PHP. Next, we need to learn how to use PHP to read back cookie values. As we saw in the first movie of this chapter, PHP takes all cookie values, sent in the request header. And puts them in an associative array, assigned to the super-global $_COOKIE. That's cookie, singular, not plural. Even though this associative array might contain data from several cookies. It's still going to be singular in it's name, don't let that trip you up. And the way that we'll access our values in there is by using our key to get the value from the associative array. Very much like we did when we were working with get and post requests. Let's try it now.
So, I'm going to start out by working with my existing cookies.php file. I'd already set a cookie here before. If you haven't done that, or if you deleted yours, you'll want to set a new one now. So, go ahead and load this page up in a browser. Reload the page, make sure that you have set that cookie. And then, I'm going to comment it out, so that it's not actually setting it. We're just reading it back, just so that you don't think this has anything to do with what we're about to do down here. And let's just put in pre tags to start and php, print_r, and let's take a peek at cookie.
Let's see what it looks like. We're just going to print out the contents of that associative array. Let's reload that page and you'll see that it brought up that value, test 45. Alright, that's what we had set previously, the test and 45. Now the set, cookie didn't happen here, this is something that was stored. And everytime we reload the page, that gets sent. Every single time, that gets sent to everything that's on local host. Those values are always present with every single request. They're sent with a header of every single request that comes to the web server now.
So, this is just taking a peek at them. I think it's more useful to actually use them. So, let's take these pre tags away. And instead of print cookie, let's change this to actually set the value, test equals cookie. And then, we need the key that we're looking for. So, that is going to be test. That will set the value and then let's just echo it back. Now in truth, you'd probably do something besides just echo it. You might use this as a user ID to look up a user in the database. It's a very common use of it. You might be checking to see someones preference. What language they want to have the site viewed in, or what time zone they're in.
Those kinds of things are good things to store in cookies. Let's comeback and take a look here. Reload the page. And there we go, there's 45. Now, one thing that's very important with cookies is to make sure that they exist before you try and access them. Let's say test2, for example. Let's save that, reload it. See I get an error because it didn't exist. With cookies, first of all, it may not have been set yet. You can't assume that anyone came to your site through any certain path, necessarily. Meaning if there's a log in page, maybe they cant get to anything past the log in page without logging in. You don't know for sure if they went to your home page first or if they came in through some directory link deeper into your website. And also, users can delete cookies, we saw that, we were able to delete cookies by going to our preferences.
So, a user could delete their cookies and then they don't have it anymore. And your site would return an error if you didn't check to see whether it was there or not. We know how to do that checking. We can just do it with if is set and then I'll just copy this rather than type it again. So if that's set, then we'll set it. Else, we'll do test equals, and we'll just make it equal to an empty string. So, let's try that now. Come back and reload it, 45. Same thing if we were to check and see if it is test2, which doesn't exist anymore. Now my page doesn't blow up.
So, it's a little friendlier. This is another place where that ternary operator that we talked about is really convenient. Instead of writing this whole big block of code that takes up five different lines, instead we can do it to one line. We could have test. And then, let me just grab some of these pieces here, isset, question mark. And if it is set, this is the true value. If it's not set, this is the false value. You can see that's a nice way to condense all that down to one simple line, test. If it's set, here's the value. If it's not, here's the other value. We need a semicolon at the end. And I'll just reload the page.
There we go, 45. So, that's really all there is to pulling back these cookie values and accessing them from cookie. However, there's one more important thing that I want you to take a peek at. Let's uncomment this set cookie, and let's now set the cookie for test. And let's make it equal to, let's say something really obviously different, 1,2,3. All right, so we'll save it. Let's come back here and let's reload our page. So, the first thing that's going to happen is it executes the code, it's going to come down and do set cookie, right? And then, it will keep going down and show us the cookie.
Right, ready? Reload. Wait a minute, it still says 45. Reload again. Now it says 123. Let's try again. Let's set it this time to be equal to a string. We'll make it, hello, save it, come back, reload the page, still 123. Reload the page again, now we get hello. Do you understand why that's happening? It's because this cookies super global are the values that are coming in from the request. It has nothing to do with what we set in the cookie here. This is instructing what's going to be in our response headers. It's telling PHP, when you construct the response, here's the cookie that you need to send back with that response.
The two are not connected. So, this is the values that came in from the user, these are the values that are going out to the user. But they're not the same and it doesn't do any kind of syncing between them. So, don't let that trip you up. You set cookie to some different value let's say that you had a, a last login time or something. So, you do set cookie well don't think that you can find that value that you just set inside this super global. This is going to be an old outdated value now. This new value won't be in play really, until that response is back to the user's browser and actually gets set as a cookie.
And then, from then on, every future request then it will be inside our super global for cookie. Cookie holds the values from the previous request. It's not changed when you set the cookie.
Find answers to the most frequently asked questions about PHP with MySQL Essential Training.
Here are the FAQs that matched your search "":
Sorry, there are no matches for your search ""—to search again, type in another word or phrase and click search.
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.