Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
In this movie we're going to learn how to set Cookie Values using PHP, and we'll do that by using PHPs setcookie function. The set cookie function is going to take three arguments, name, value and an expiration. The name is going to be the name that we want to set for this particular cookie. The value is the value that we want to associate with that name. Together, they work like a key value pair. And in fact, when we get our cookie back from the user, and it's stored in our cookie superglobal. We're going to refer to that cookie by its name. That'll be the key in the associative array and it will return the value to us. The third argument, the expiration is us telling the browser how long it ought to keep this cookie around.
You should consider this cookie valid until a certain date. Cookies don't necessarily last forever, we can tell the browser, keep this cookie around for 24 hours. Keep it around for a week. Keep it around for 10 years. It's up to us to tell the browser how long we want this cookie to remain valid. Let's try setting one. To begin with let's open up basic.html. And let's do Save As, we'll call this cookies.php, Cookies, and I'll come down here. And I'll make a php block. And we will type our functions setcookie, no underscores, all run together. And then name, value, and expire.
Now when you actually use this in the real world you probably won't use variables for all of this. You'll probably just type the value in here a lot of time, and the name might be just test. I'm going to go ahead and leave these variables in here so that they're a little bit descriptive to you about what each of these things is. And instead I'll just put name up here equal to test and value equal to 45. Now for the expiration, we need to send a Unix time stamp. You may be thinking, well, what in the world? How do I deal with a Unix time stamp? That's something I've never dealt with. Well, luckily, you don't have to worry too much about it, because PHP will give you the current time as a Unix times tamp by using the time function.
So time will return that time to you, and then we can just use math to add time to it to say how far in the future from right now should this cookie stay valid. Because the unit's timestamp is precise to the second, we need to add seconds to that time. So the easiest way to do that is to say well, there's 60 seconds in a minute and there are 60 minutes in an hour and there are 24 hours in a day. And there are seven days in a week. So this would then be adding one week on to the current time. If you wanted to have two weeks, well you'd add a 2 at the end, four weeks would be a 4, and so on. This is the easiest way to do it.
You'll see this all the time in people's code, so add seconds. Now, t his will set a cookie called test to the value 45, and will make it last for one week. So these cookie instructions are going to be sent with the response header. Now, think about that for a second. Hopefully in chapter 10, I impressed upon you why that matters so much. If not, go back and review headers and output buffering. Headers can only be set before any HTML output unless buffering is on. So, that includes setting cookies, too. They go in the header. You can't set a cookie in the header if the header has already left PHP and is waiting the webserver, getting ready to go back to the browser.
So therefore, all of this needs to go up here above our HTML. Now, in truth, I believed I turned output buffering on. So this probably would work just fine. But I'm going to go ahead and put it up here at the top anyway. And I'm going to Paste a little note here to remind myself. Remember that setting cookies must be done before any HTMLs output, unless output buffering is turned on, okay. So now we're ready to try it. Now we're ready to set the cookie and then it will render the HTML after that. So I'll go back to my browser. Then before I even go to a URL, let's make sure that we have cookies turned on because this is a configurable thing. People can turn off their cookies under preferences, under privacy in Firefox. In different browsers it might be in different places, it's usually Privacy or Security.
You can accept whether to choose cookies from different sites. Now if I choose private browsing mode, it won't accept cookies from the sites. If I choose, Never remember history, it won't remember, okay? But, if I have it set to accept cookies from sites, then, cookies will come to me and be stored. So, make sure that's set for you first. Usually it is, most people have it on. And let's do then local host, for me that's going to be located at kevinskoglund/sandbox/cookies.php. Okay, so now, it loaded the page. And you can see it says cookies up here, so we have the page loaded. Did it set the cookie? We could use php to retrieve the value but we can also go back there to our preferences and there's an potion for show cookies.
This is, most browsers should have something like this somewhere, you may have to dig around a little bit until you find it. But you can see that Show Cookies is an option and here I have one for local host, that's the server that I'm on. You can see that, when I was on Google, Google set a cookie and it's google.com. So it would usually be the domain. In our case our domain is local host, and here it is. So all the cookies would be listed here under my domain. The cookie name is test and the content for that cookie is 45. Notice that we can see the value of our cookie easily. It may not be any big deal, but as developers we need to stay mindful of the fact that this information is exposed to our end users.
They can see the values that we set for cookies. And you should assume that anything you set as a cookie could be used by hackers to try and trick your site or to gain useful information about how your site works. So don't store anything critical here without first encrypting it. Notice also that our exportation date is down here. You can play around and try setting different expiration dates, see what those looks like. Notice also that the cookie can be removed, so cookies are not permanent. The user has the ability to remove cookies just by doing that. Now the cookie is gone. It's not set anymore. So the user always has that option, but if they don't do that, then we can remember who they are.
And typically, the kinds of things we want to store with cookies are not super application sensitive. They are things that are nice for the user, so they probably want to keep them around because they help us to give a better user experience to them. Okay, so now that we know how to set cookies, in the next movie let's see how we read cookies back using PHP.
Get unlimited access to all courses for just $25/month.Become a member
82 Video lessons · 97097 Viewers
61 Video lessons · 84405 Viewers
71 Video lessons · 68612 Viewers
56 Video lessons · 101102 Viewers
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.
Your file was successfully uploaded.