Questions tagged [cookies]

An HTTP cookie is a piece of data stored by the user's web browser. Unless otherwise specified, cookies can be created, read, modified and deleted both by JavaScript and from server-side through HTTP headers.

An HTTP cookie is a piece of data stored by the user's web browser. Unless otherwise specified, cookies can be created, read, modified and deleted both by JavaScript and by server-side through HTTP headers.

Cookies can be used to remember the state of the session, such as authentication, state of GUI controls and personalization through user preferences. They can also be inappropriately used to track the browsing history and to transfer malware.

A server sets a cookie using Set-Cookie HTTP header:

 Set-Cookie: someName=someValue; Expires=Fri, 18-Jan-2013 10:13:13 GMT
 Set-Cookie: someOther=someOtherValue

It is possible to set cookies also on image and similar content that makes them a powerful tracking tool. When cookie is set, it is reported back by the browser:

 Cookie: someName=someValue; someOtherName=someOtherValue

For more security, cookies can be restricted to some domain and path:

 Set-Cookie: name=value; domain=www.foo.com; Path=/hereonly

They can also have additional secure (HTTPS only but accessible through JavaScript) and httponly (HTTP or HTTPS but not accessible through JavaScript) attributes:

 Set-Cookie: goldlocation=somewhere; Domain=.morgan.com; secure; httponly

In this example the cookie is accessible for all subdomains of morgan.com but only through HTTPS and not accessible from JavaScript.

Cookie access control is based on domain, (optionally) path and (optionally) URL scheme (http: vs. https:). The rules governing cookies are not the same as the access control rules of the DOM in JavaScript which are based on the same domain policy, but because cookie access is mostly based on domain name, they are sometimes confused with the usual HTTP same domain policy.

The behaviour of HTTP cookies in real life browsers is not described in any RFC (thus quoting a RFC to describe cookies is almost always wrong). The various RFC are of historical interest.

Browsers are recommended to allow at least 20 cookies per domain and 4KB per cookie. If you are looking for an alternative to cookies that aren't sent in HTTP headers and can store more data, consider

Implementation Hint

For fans, there is a simple plugin make it easy to deal with cookies (write, read and delete) could be found here.


Questions:

34843 questions
198
votes
17 answers

Get and Set a Single Cookie with Node.js HTTP Server

I want to be able to set a single cookie, and read that single cookie with each request made to the nodejs server instance. Can it be done in a few lines of code, without the need to pull in a third party lib? var http =…
Corey Hart
  • 10,316
  • 9
  • 41
  • 47
195
votes
15 answers

Is there a setting on Google Analytics to suppress use of cookies for users who have not yet given consent

According to EU Article 5(3) of the E-Privacy Directive (a.k.a 'The Cookie Laws'), web sites that target EU users have to gain opt-in consent from users before they set a cookie. See ICO Guidance I am trying to square this with Google Analytics on…
JW.
  • 4,821
  • 5
  • 43
  • 60
192
votes
5 answers

What is the difference between server side cookie and client side cookie?

What is the difference between creating cookies on the server and on the client? Are these called server side cookies and client side cookies? Is there a way to create cookies that can only be read on the server or on the client?
Rahul
  • 2,511
  • 3
  • 20
  • 20
186
votes
3 answers

How to send cookies in a post request with the Python Requests library?

I'm trying to use the Requests library to send cookies with a post request, but I'm not sure how to actually set up the cookies based on its documentation. The script is for use on Wikipedia, and the cookie(s) that need to be sent are of this…
Ricardo Altamirano
  • 14,650
  • 21
  • 72
  • 105
179
votes
11 answers

Javascript Cookie with no expiration date

I would like to set up a cookie that never expires. Would that even be possible? document.cookie = "name=value; expires=date; path=path;domain=domain; secure"; I don't want to make the date really large, I am just wondering if there was a value…
Jose Vega
  • 10,128
  • 7
  • 40
  • 57
169
votes
25 answers

Can I set the cookies to be used by a WKWebView?

I'm trying to switch an existing app from UIWebView to WKWebView. The current app manages the users login / session outside of the webview and sets the cookies required for authentication into the the NSHTTPCookieStore. Unfortunately new WKWebView…
Col
  • 2,300
  • 3
  • 16
  • 16
158
votes
5 answers

Using CookieContainer with WebClient class

I've previously used a CookieContainer with HttpWebRequest and HttpWebResponse sessions, but now, I want to use it with a WebClient. As far as I understand, there is no built-in method like there is for HttpWebRequests (request.CookieContainer). How…
Maxim Zaslavsky
  • 17,787
  • 30
  • 107
  • 173
157
votes
9 answers

Differences between cookies and sessions?

I am training in web developement and am learning about JSP & Servlets. I have some knowledge of HttpSession - I have used it in some of my sample projects. In browsers I have seen the option to "delete cookies". If I delete the cookies it deletes…
blacktiger
  • 18,957
  • 5
  • 20
  • 11
155
votes
4 answers

Should JWT be stored in localStorage or cookie?

For the purpose of securing REST API using JWT, according to some materials (like this guide and this question), the JWT can be stored in either localStorage or Cookies. Based on my understanding: localStorage is subjected to XSS and generally it's…
pkid169
  • 2,240
  • 2
  • 15
  • 22
155
votes
6 answers

How to set and get cookies in Django?

I have a web site which shows different content based on a location the visitor chooses. e.g: User enters in 55812 as the zip. I know what city and area lat/long. that is and give them their content pertinent to that area. My question is how can I…
Jeffrey
  • 1,621
  • 2
  • 12
  • 7
153
votes
3 answers

How to read a HttpOnly cookie using JavaScript

EDIT What one means by "a secure cookie" is ambiguous. To clarify: Secure as in sent over the https:// protocol — ie. cookie is not sent in plaintext. Known as the "secure flag" Secure as in the cookie cannot be read by Javascript running in…
tzam
  • 1,802
  • 3
  • 14
  • 15
152
votes
4 answers

What are "signed" cookies in connect/expressjs?

I am trying to figure out what "signed cookies" actually are. There isn't much on the net, and if I try this: app.use(express.cookieParser('A secret')); But still... Cookies are still 100% normal on the browser, and I don't really know what…
Merc
  • 16,277
  • 18
  • 79
  • 122
147
votes
8 answers

How do you remove a Cookie in a Java Servlet

How do you remove a cookie in a Java servlet? I tried this: http://www.jguru.com/faq/view.jsp?EID=42225 EDIT: The following now works successfully it appears to be the combination…
Dougnukem
  • 14,709
  • 24
  • 89
  • 130
147
votes
2 answers

How to use Python to login to a webpage and retrieve cookies for later usage?

I want to download and parse webpage using python, but to access it I need a couple of cookies set. Therefore I need to login over https to the webpage first. The login moment involves sending two POST params (username, password) to /login.php.…
mandom
  • 1,481
  • 3
  • 10
  • 5
147
votes
4 answers

CURL to access a page that requires a login from a different page

I have 2 pages: xyz.example/a and xyz.example/b. I can only access xyz.example/b if and only if I login to xyz.example/a first. If accessing xyz.example/b without going through the other, I simply get access denied (no redirect to login) via the…
ms1013
  • 1,725
  • 2
  • 14
  • 16