Locked lesson.
About this lesson
Exercise files
Download this lesson’s related exercise files.
Cookies.docx59.2 KB Cookies - Solution.docx
59.5 KB
Quick reference
Cookies
Cookies are small files that are placed on a website visitor's computer.
When to use
Cookies are useful tools to track website visitors.
Instructions
To work with Cookies, use the $_COOKIE superglobal.
To set a cookie:
setcookie(name, value, expire, path, domain, secure, httponly);
Place that code above the very top <html> tag of a web page.
To set the time for the expire section (in seconds):
time() + (86400 * 30)
To retrive a cookie:
echo $_COOKIE[$cookie_name];
To check to see if a Cookie has been set:
isset($_COOKIE[$cookie_name]);
Hints & tips
- setcookie(name, value, expire, path, domain, secure, httponly);
- Cookie expiration is measured in seconds
- to retrieve a cookie: echo $_COOKIE[$cookie_name];
- To check a cookie: isset($_COOKIE[$cookie_name]);
- time() + (86400 * 30)
- 00:04 In this video, I wanna talk about cookies, and cookies
- 00:07 are those annoying things that get placed on your computer when you visit a website.
- 00:11 And they're used to usually track you or
- 00:14 keep track of certain information about you or whatever.
- 00:17 Some people think they're annoying.
- 00:18 If you own a website, they're very useful.
- 00:20 And PHP makes it pretty easy to set cookies and
- 00:23 retrieve cookies on a website visitor's computer.
- 00:26 So this is what we use.
- 00:28 We use the setcookie function.
- 00:30 And the setcookie function has a number of parameters, the name,
- 00:34 the value, when the cookie expires.
- 00:36 Because by definition, a cookie can't be really forever, or
- 00:39 it shouldn't sit on somebody's computer forever.
- 00:42 It should be for a specific amount of time, so expire.
- 00:45 The path exactly where you wanna put it on their computer if you don't wanna just use
- 00:49 the default, which you probably will.
- 00:51 Domain, secure and HTTP only.
- 00:53 You only have to use this name value.
- 00:56 The rest of these are all optional.
- 00:58 In fact, I generally just will use these first three.
- 01:01 So that's what we're gonna do.
- 01:02 So let's create a cookie here.
- 01:03 Now to do this, you need to put the setcookie function above the very top
- 01:08 HTML tag of an HTML document, right?
- 01:12 That's just how it goes.
- 01:13 So we can do php opening and closing tags, and then setcookie.
- 01:20 In fact, let's just leave it like this for now.
- 01:23 Let's create a couple of variables first.
- 01:25 Let's call it cookie_name and set that equal to Our Cookie.
- 01:32 And then let's say cookie_user, and let's set this equal to John Elder.
- 01:39 Now, programmatically, you may wanna do something different here, but
- 01:43 I'm just gonna put the value of John Elder and a cookie on somebody's computer.
- 01:47 And this will go on everybody's computer.
- 01:48 So obviously, you won't wanna put John Elder on there, but whatever you wanna
- 01:51 save on a person's computer, we're gonna put it in this variable.
- 01:54 So now let's create our cookie.
- 01:56 We go to setcookie and the name.
- 01:58 Let's add our cookie_name, that's our name.
- 02:02 We'll just use that variable.
- 02:03 And the value, let's say cookie_user.
- 02:09 And when will it expire?
- 02:10 So to do this, I'm gonna use the PHP time function.
- 02:13 This will give the current time.
- 02:15 And then we wanna add, let's make this 30 days.
- 02:17 And this 8,640, those are seconds.
- 02:20 And those are how many seconds in one day, 60 seconds in a minute,
- 02:24 60 minutes in an hour, 24 hours in a day.
- 02:26 If you do that multiplication, it comes to 86,400 times 30, 30 days,
- 02:31 and that'll be that, very common.
- 02:34 And let's see, for the path, let's just put it, let's do like that forward slash.
- 02:39 So go ahead and delete this, and this will set a cookie on a person's computer.
- 02:45 Piece of cake, anytime somebody comes to our website,
- 02:47 it'll set this cookie on their computer.
- 02:49 That cookie will sit there for 30 days.
- 02:51 Now how do we retrieve that cookie?
- 02:53 Well, first of all, well, remember in the last video, we talked about superglobals,
- 02:57 and there was a COOKIE superglobals, so we can just echo that out.
- 03:01 And like all other superglobals, we pass it a parameter in here, and
- 03:04 we can pass then the cookie_name.
- 03:06 So let's just go ahead and do that.
- 03:07 Uh-oh, it got an error because we need to take out that space.
- 03:13 So let's do that.
- 03:14 So hit Reload and we get John Elder, which is our cookie user.
- 03:18 And I should say it printed out our actual cookie_user variable and
- 03:22 not the cookie_name.
- 03:23 Whenever you call the name of the cookie,
- 03:25 it just prints out whatever is in the cookie.
- 03:27 So that's interesting.
- 03:28 There's one more thing to talk about.
- 03:29 We can check to see if a cookie has been set with the isset function, and
- 03:33 it's just I-S-S-E-T, and
- 03:35 then inside the parameters you pass at the COOKIE superglobal with its name.
- 03:40 Now we can run a check to see if there's a cookie by using a simple if statement.
- 03:44 So we'll go if, and I'm gonna use this exclamation point, which means not,
- 03:49 if there's not a cookie.
- 03:50 Then echo No Cookie,
- 03:55 else echo Yes cookie!
- 04:00 So if we save this, come back here and hit Reload,
- 04:02 we see Yes cookie because we have already placed a cookie.
- 04:06 So those are cookies, pretty simple.
- 04:07 To delete a cookie, you just set a cookie again, but you give it a time in the past,
- 04:11 and that will delete the cookie.
- 04:13 Very useful, very cool, very easy to do.
- 04:16 And in the next video, we're gonna talk about sessions.
Lesson notes are only available for subscribers.