Cookies

5 minutes
Share the link to this page
Copied
  Completed
An HTTP cookie (also called web cookie, Internet cookie, browser cookie, or simply cookie) is a small piece of data sent from a website and stored on the user's computer by the user's web browser while the user is browsing.

Transcript

Hello, in this python programming video, we are going to cover cookies. So we're going to cover setting cookies, retrieving cookies and deleting cookies. So to set a cookie input, so input cookie module, like so. And that's all pretty cool. But but you might be wondering, you know, what is, you know, an actual cookie? Because, trust me, it's not the things that you eat.

No, no, no. But it is, you know, named after them. Because you know, we need a break on a trail of what you be doing. So cookies essentially allow you to set data and they're stored with your browser's session. So that's great when you I'm sure you've been back to a website where you've logged in and you're already logged in. He was like, Yes, I'd have to log in again.

Oh, you know, it's got some extra information that you've already set up. So we're cool, and he uses cookies. So yeah, obviously the finesse, the best way to get your head around it is just some values that you add that keep track of you or you know your user session on some other basic information, right, you probably as you use row ID, maybe a couple of other pieces of information. And therefore, you can get that information from a database without me using a database or anything like that. But once you're comfortable with this, feel free to check out the database videos in this series and combine them and you over develop. Okay, so we've imported the cookie module, we need to create a cookie sort of variable object and call it cook equals cookie, simple cookie.

And to set a cookie this to cook no quotation marks in square brackets and then put the name of your cookies and put user ID Equals on. And I'm going to set a expiry date. Very my expiry dates are set in seconds. So this is the way you do it by equals, and 60 times 60 times 20%, only six seconds. So 60 minute time, or 60 is an hour to 24 hours a day. So it was time to resupply without a cookie listed for a year.

So that's fine. We're only going to be using one cookie in this video. But to set another cookie, just duplicate this line, change the cookie name and change the data is all if you need to change the data they fit into the cookie. And you can change the expiry date as well by doing something very similar. Next, what we need to do is do a quick and we need to create our cookie or a cookie object. So we actually set the cookie.

But the moment this is creating Python, but we need to set it in HTML form and Now if we save that, reload the page as suddenly the localhost, the cookies, there's nothing there, or reload, and go back to cookies. It's been setting that is fantastic. So we got our cookie set. So that's all well and good. How do we actually retrieve it to read previous really simple, I'm going to retrieve it and print a line, you just do cook. square brackets quotation marks, the name of the user ID and then dot value.

That simple. Go back refresh and see if interferon which is the user ID in this cookie. And again, you probably won't just do it in raw form like this, you will probably get it in as in Python, and then maybe link to a database and get the information for this particular user ID something along those lines. But once you're all good with this, you're good to go. The final thing I want to cover is how to actually set You know how to delete a cookie. And it's actually you know, kind of weird I think I need to delete a plea.

To delete cookies, you have to set this binary date which is before the current date. So date in the past Cookie, cookie this obviously, this just sets it into the future from you know, currently so we don't want that what we want. So we could do something like th U which is a 301 Gen 1970. This is like epoch time 00 GMT and save that now. And if we go back, refresh, I think you'll see there is no cookie in here for this because I didn't reset it. We are expiring.

Obviously, I'll comment this line that you'll be there for you can act source code. We don't want that there to constantly be deleting our cooking. So that's it for the cookies video. Now what I think I'm actually gonna go and have some chocolate chip cookies. Thanks for watching. I'll see you in the next Python demo.

Sign Up

Share

Share with friends, get 20% off
Invite your friends to LearnDesk learning marketplace. For each purchase they make, you get 20% off (upto $10) on your next purchase.