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.
Now that we know how to set cookies, we know how to read back their values, we should talk about how we unset cookies. How do you get rid of them when you're done? A classic case of this could be when someone logs in you want to record their user ID but then when they log out you want to get rid of it. You want make them not logged in anymore. Well to do that we need to know how to unset them. Let me first show you the wrong way to do it. You do not want to do it by using the unset function. That's what we use on variables when we want to unset them, it'll unset their value. But think about it for a second, why would you not want to use unset on the cookie global parameter? Why would that not work? The reason why is because those are the values that came in from the last request.
Says nothing about what the user has on their browser. This is just a result of what came in on the last request. Unless we provide the user with something different, that's what's going to keep coming in for every future request. Unsending it isn't going to change it, it's still going to be sent from the user's browser. We have to tell the user's browser hey browser, you have to change something on your end. And so to do that, we have to do it one of the right ways, which is by using the set cookie function. Set cookie with no value after it will set it to nothing.
It will empty it out. Now I personally don't like this way. To me this looks too much like it's a mistake and it's not clear what your intent really was. I think that it's better to be explicit about it and to say that the value is null. So set cookie with whatever name to the value null. And that will set it equal to nothing. Another very common way to do it is to set the expiration date to a date in the past. So that will tell the browser, hey browser this cookie's no good anymore. You need to chuck it, get rid of it. Now my preferred method is to combine the second and third ones. Just do both.
Tell it to set the cookie to null so that the value is gone. And tell it by the way that cookie's no good anymore, you can throw it away. And then the cookie should just disappear. Both of them should work on their own independently. We can do both together and make it super clear that that's what we're trying to do. Let's try it and see. So here I am on my cookies.php page again. And let's just bring up our browser so that we see that we've already got hello there. Reload the page. Hello is the value that I last had there. Make sure that you have a cookie set for this. So now let's try a couple of these different approaches.
So let's comment out this line so we can use it in the future. Let's just do set cookie name. And then let's reload the page. So here we go reload the page one time. It's still there. We talked about this in the last movie, remember? It's because what we're seeing here is sort the, the echo of it. The fact that it was sent in the last request. We still at the end of our response. We sent back the fact that this ought to be erased. So we need to reload the page a second time, and we'll see that it's actually gone because when the request came back to us after that response, the next request did not include in the cookie.
So therefore, this was empty. Make sure you're clear on that. So that worked, let's try setting it again. That's one way to do it. I'll just uncomment that line, save it, reload the page. Now I don't see it here. But it did set it. And you can check if you want, or you can just reload it again to make sure that we're comfortable with that fact. And then let's try the next one. We're going to take it down here. But this time, instead of the value, we're going to set it equal to null. We'll save it. Reload the page. Again we have to reload it a second time and you'll see that it's gone.
And then comment it out. We'll set the cookie back again and reload the page twice. There it is, hello. And now we're going to get rid of it again using this other method. This time though we're going to say the same thing. We'll put the value again for this first one and expire we're going to change to be time minus and it can be any time in the past. 3600 will be an hour ago or you can go ahead and set it to something more like a week ago or two weeks ago. Let's save it. Let's go back and reload the page twice. And sure enough it disappears that way too.
And like I said, the way that I prefer is to do it this way. Let's set it first before we do this. But the one we're going to do is both together. We'll put Kevin's recommendation for unsetting. There we go. We'll set it one more time, there it is and then comment that out. Let's try unsetting it and there it is. Let's go back and just assure ourselves. If we go to preferences, we look at cookies. Show cookies. And look for the cookie that is for a local host. I don't have any so you'll see that it's gone.
It's not there. But you can search for it to find it or scan down the list. So that's it, that's all there is to unsetting a cookie. Just make sure that you don't try and unset the values that are here because at that point, you're too late to unset it. You're not making any tangible effect on the user's browser, you're just changing what's happening in PHP only. and that's not going to actually accomplish it for you.
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.