{"id":75764,"date":"2020-01-20T12:02:51","date_gmt":"2020-01-20T06:32:51","guid":{"rendered":"https:\/\/www.vskills.in\/certification\/tutorial\/?p=75764"},"modified":"2024-04-12T14:17:16","modified_gmt":"2024-04-12T08:47:16","slug":"cookies","status":"publish","type":"page","link":"https:\/\/www.vskills.in\/certification\/tutorial\/cookies\/","title":{"rendered":"Cookies"},"content":{"rendered":"<p>Browser developers long ago recognized that HTTP\u2019s statelessness poses a huge problem for Web developers, and thus cookies were born. A cookie is a small piece of information that browsers store on behalf of Web servers. Every time a browser requests a page from a certain server, it gives back the cookie that it initially received. Let\u2019s take a look how this might work. When you open your browser and type in google.com, your browser sends an HTTP request to Google that starts something like this:<\/p>\n<p>GET \/ HTTP\/1.1<br>\nHost: google.com<br>\n&#8230;<br>\nWhen Google replies, the HTTP response looks something like the following:<\/p>\n<p>HTTP\/1.1 200 OK<br>\nContent-Type: text\/html<br>\nSet-Cookie: PREF=ID=5b14f22bdaf1e81c:TM=1167000671:LM=1167000671;<br>\nexpires=Sun, 17-Jan-2038 19:14:07 GMT;<br>\npath=\/; domain=.google.com<br>\nServer: GWS\/2.1<br>\n&#8230;<\/p>\n<p>Notice the Set-Cookie header. Your browser will store that cookie value (PREF=ID=5b14f22bdaf1e81c:TM=1167000671:LM=1167000671) and serve it back to Google every time you access the site. So the next time you access Google, your browser is going to send a request like this:<\/p>\n<p>GET \/ HTTP\/1.1<br>\nHost: google.com<br>\nCookie: PREF=ID=5b14f22bdaf1e81c:TM=1167000671:LM=1167000671<br>\n&#8230;<\/p>\n<p>Google then can use that Cookie value to know that you\u2019re the same person who accessed the site earlier. This value might, for example, be a key into a database that stores user information. Google could (and does) use it to display your name on the page.<\/p>\n<p>Getting and Setting Cookies &#8211; When dealing with persistence in Django, most of the time you\u2019ll want to use the higher-level session and\/or user frameworks discussed a little later in this chapter. However, we\u2019ll pause and look at how to read and write cookies at a low level. This should help you understand how the rest of the tools discussed in the chapter actually work, and it will come in handy if you ever need to play with cookies directly. Reading cookies that are already set is incredibly simple. Every request object has a COOKIES object that acts like a dictionary; you can use it to read any cookies that the browser has sent to the view:<\/p>\n<p>def show_color(request):<br>\nif &#8220;favorite_color&#8221; in request.COOKIES:<br>\nreturn HttpResponse(&#8220;Your favorite color is %s&#8221; % \\<br>\nrequest.COOKIES[&#8220;favorite_color&#8221;])<br>\nelse:<br>\nreturn HttpResponse(&#8220;You don&#8217;t have a favorite color.&#8221;)<\/p>\n<p>Writing cookies is slightly more complicated. You need to use the set_cookie() method on an HttpResponse object. Here\u2019s an example that sets the favorite_color cookie based on a GET parameter:<\/p>\n<p>def set_color(request):<br>\nif &#8220;favorite_color&#8221; in request.GET:<br>\n# Create an HttpResponse object&#8230;<br>\nresponse = HttpResponse(&#8220;Your favorite color is now %s&#8221; % \\<br>\nrequest.GET[&#8220;favorite_color&#8221;])<br>\n# &#8230; and set a cookie on the response<br>\nresponse.set_cookie(&#8220;favorite_color&#8221;,<br>\nrequest.GET[&#8220;favorite_color&#8221;])<br>\nreturn response<br>\nelse:<br>\nreturn HttpResponse(&#8220;You didn&#8217;t give a favorite color.&#8221;)<\/p>\n<p>You can also pass a number of optional arguments to response.set_cookie() that control aspects of the cookie, as shown in Table 12-1.<\/p>\n<table>\n<thead>\n<tr>\n<td><strong>Parameter<\/strong><\/td>\n<td><strong>Default<\/strong><\/td>\n<td><strong>Description<\/strong><\/td>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>max_age<\/td>\n<td>None<\/td>\n<td>Age (in seconds) that the cookie should last. If this parameter is None, the cookie will last only until the browser is closed.<\/td>\n<\/tr>\n<tr>\n<td>expires<\/td>\n<td>None<\/td>\n<td>The actual date\/time when the cookie should expire. It needs to be in the format &#8220;Wdy, DD-Mth-YY HH:MM:SS GMT&#8221;. If given, this parameter overrides the max_age parameter.<\/td>\n<\/tr>\n<tr>\n<td>path<\/td>\n<td>&#8220;\/&#8221;<\/td>\n<td>The path prefix that this cookie is valid for. Browsers will only pass the cookie back to pages below this path prefix, so you can use this to prevent cookies from being sent to other sections of your site.<p><\/p>\n<p>This is especially useful when you don\u2019t control the top level of your site\u2019s domain.<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>domain<\/td>\n<td>None<\/td>\n<td>The domain that this cookie is valid for. You can use this parameter to set a cross-domain cookie. For example, domain=&#8221;.example.com&#8221; will set a cookie that is readable by the domains www.example.com, www2.example.com, and an.other.sub.domain.example.com.<p><\/p>\n<p>If this parameter is set to None, a cookie will only be readable by the domain that set it.<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>secure<\/td>\n<td>False<\/td>\n<td>If set to True, this parameter instructs the browser to only return this cookie to pages accessed over HTTPS.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><strong>The Mixed Blessing of Cookies<\/strong> &#8211; You might notice a number of potential problems with the way cookies work. Let\u2019s look at some of the more important ones:<\/p>\n<ul>\n<li>Storage of cookies is essentially voluntary; browsers don\u2019t guarantee anything. In fact, all browsers enable users to control the policy for accepting cookies. If you want to see just how vital cookies are to the Web, try turning on your browser\u2019s \u201cprompt to accept every cookie\u201d option.<br>\nDespite their nearly universal use, cookies are still the definition of unreliability. This means that developers should check that a user actually accepts cookies before relying on them.<br>\nMore important, you should never store important data in cookies. The Web is filled with horror stories of developers who\u2019ve stored unrecoverable information in browser cookies only to have that data purged by the browser for one reason or another.<\/li>\n<li>Cookies (especially those not sent over HTTPS) are not secure. Because HTTP data is sent in cleartext, cookies are extremely vulnerable to snooping attacks. That is, an attacker snooping on the wire can intercept a cookie and read it. This means you should never store sensitive information in a cookie.<br>\nThere\u2019s an even more insidious attack, known as a man-in-the-middle attack, wherein an attacker intercepts a cookie and uses it to pose as another user.<\/li>\n<li>Cookies aren\u2019t even secure from their intended recipients. Most browsers provide easy ways to edit the content of individual cookies, and resourceful users can always use tools like mechanize (http:\/\/wwwsearch.sourceforge.net\/mechanize\/) to construct HTTP requests by hand.<br>\nSo you can\u2019t store data in cookies that might be sensitive to tampering. The canonical mistake in this scenario is storing something like IsLoggedIn=1 in a cookie when a user logs in. You\u2019d be amazed at the number of sites that make mistakes of this nature; it takes only a second to fool these sites\u2019 \u201csecurity\u201d systems.<\/li>\n<\/ul>\n\n\n<p>Go to- <a href=\"https:\/\/www.vskills.in\/certification\/tutorial\/php-developer\/\" target=\"_blank\" rel=\"noreferrer noopener\"><strong>Certified PHP Developer Tutorial<\/strong><\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Browser developers long ago recognized that HTTP\u2019s statelessness poses a huge problem for Web developers, and thus cookies were born. A cookie is a small piece of information that browsers store on behalf of Web servers. Every time a browser requests a page from a certain server, it gives back the cookie that it initially&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"categories":[8655],"tags":[1509],"class_list":["post-75764","page","type-page","status-publish","hentry","category-django-web-development","tag-cookies"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v24.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Cookies - Tutorial<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.vskills.in\/certification\/tutorial\/cookies\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Cookies - Tutorial\" \/>\n<meta property=\"og:description\" content=\"Browser developers long ago recognized that HTTP\u2019s statelessness poses a huge problem for Web developers, and thus cookies were born. A cookie is a small piece of information that browsers store on behalf of Web servers. Every time a browser requests a page from a certain server, it gives back the cookie that it initially...\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.vskills.in\/certification\/tutorial\/cookies\/\" \/>\n<meta property=\"og:site_name\" content=\"Tutorial\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/vskills.in\/\" \/>\n<meta property=\"article:modified_time\" content=\"2024-04-12T08:47:16+00:00\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"5 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/cookies\/\",\"url\":\"https:\/\/www.vskills.in\/certification\/tutorial\/cookies\/\",\"name\":\"Cookies - Tutorial\",\"isPartOf\":{\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/#website\"},\"datePublished\":\"2020-01-20T06:32:51+00:00\",\"dateModified\":\"2024-04-12T08:47:16+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/cookies\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.vskills.in\/certification\/tutorial\/cookies\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/cookies\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.vskills.in\/certification\/tutorial\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Cookies\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/#website\",\"url\":\"https:\/\/www.vskills.in\/certification\/tutorial\/\",\"name\":\"Tutorial\",\"description\":\"Vskills - A initiative in elearning and certification\",\"publisher\":{\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.vskills.in\/certification\/tutorial\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/#organization\",\"name\":\"Vskills\",\"url\":\"https:\/\/www.vskills.in\/certification\/tutorial\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/www.vskills.in\/certification\/tutorial\/wp-content\/uploads\/2017\/07\/vskills-min-logo.jpg\",\"contentUrl\":\"https:\/\/www.vskills.in\/certification\/tutorial\/wp-content\/uploads\/2017\/07\/vskills-min-logo.jpg\",\"width\":73,\"height\":55,\"caption\":\"Vskills\"},\"image\":{\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/vskills.in\/\",\"https:\/\/x.com\/vskills_in\",\"https:\/\/www.linkedin.com\/company-beta\/1371554\/\",\"https:\/\/www.youtube.com\/channel\/UCMWnscxPwRF_PqXo9B7q_Tw\"]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Cookies - Tutorial","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.vskills.in\/certification\/tutorial\/cookies\/","og_locale":"en_US","og_type":"article","og_title":"Cookies - Tutorial","og_description":"Browser developers long ago recognized that HTTP\u2019s statelessness poses a huge problem for Web developers, and thus cookies were born. A cookie is a small piece of information that browsers store on behalf of Web servers. Every time a browser requests a page from a certain server, it gives back the cookie that it initially...","og_url":"https:\/\/www.vskills.in\/certification\/tutorial\/cookies\/","og_site_name":"Tutorial","article_publisher":"https:\/\/www.facebook.com\/vskills.in\/","article_modified_time":"2024-04-12T08:47:16+00:00","twitter_misc":{"Est. reading time":"5 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.vskills.in\/certification\/tutorial\/cookies\/","url":"https:\/\/www.vskills.in\/certification\/tutorial\/cookies\/","name":"Cookies - Tutorial","isPartOf":{"@id":"https:\/\/www.vskills.in\/certification\/tutorial\/#website"},"datePublished":"2020-01-20T06:32:51+00:00","dateModified":"2024-04-12T08:47:16+00:00","breadcrumb":{"@id":"https:\/\/www.vskills.in\/certification\/tutorial\/cookies\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.vskills.in\/certification\/tutorial\/cookies\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.vskills.in\/certification\/tutorial\/cookies\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.vskills.in\/certification\/tutorial\/"},{"@type":"ListItem","position":2,"name":"Cookies"}]},{"@type":"WebSite","@id":"https:\/\/www.vskills.in\/certification\/tutorial\/#website","url":"https:\/\/www.vskills.in\/certification\/tutorial\/","name":"Tutorial","description":"Vskills - A initiative in elearning and certification","publisher":{"@id":"https:\/\/www.vskills.in\/certification\/tutorial\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.vskills.in\/certification\/tutorial\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.vskills.in\/certification\/tutorial\/#organization","name":"Vskills","url":"https:\/\/www.vskills.in\/certification\/tutorial\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.vskills.in\/certification\/tutorial\/#\/schema\/logo\/image\/","url":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-content\/uploads\/2017\/07\/vskills-min-logo.jpg","contentUrl":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-content\/uploads\/2017\/07\/vskills-min-logo.jpg","width":73,"height":55,"caption":"Vskills"},"image":{"@id":"https:\/\/www.vskills.in\/certification\/tutorial\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/vskills.in\/","https:\/\/x.com\/vskills_in","https:\/\/www.linkedin.com\/company-beta\/1371554\/","https:\/\/www.youtube.com\/channel\/UCMWnscxPwRF_PqXo9B7q_Tw"]}]}},"_links":{"self":[{"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/pages\/75764","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/comments?post=75764"}],"version-history":[{"count":5,"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/pages\/75764\/revisions"}],"predecessor-version":[{"id":85199,"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/pages\/75764\/revisions\/85199"}],"wp:attachment":[{"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/media?parent=75764"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/categories?post=75764"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/tags?post=75764"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}