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
93
votes
8 answers

Can I limit the length of an array in JavaScript?

I want to display the product browsing history, so I am storing the product ids in a browser cookie. Because the list of history is limited to 5 items, I convert the cookie value to an array, then check the length of it and cut the redundant. The…
Charles Yeung
  • 38,347
  • 30
  • 90
  • 130
92
votes
9 answers

Accessing $_COOKIE immediately after setcookie()

I'm trying to access a cookie's value (using $_COOKIE) immediately after calling the setcookie() function in PHP. When I do so, $_COOKIE['uname'] isn't set. Why? Note, however, that $_COOKIE['uname'] is set as expected upon the next execution of…
heapzero
  • 1,413
  • 2
  • 13
  • 18
91
votes
6 answers

Can't access cookies from document.cookie in JS, but browser shows cookies exist

I can't access any cookie from JavaScript. I need to read some value and send them via JSON for my custom checks. I've tried to access cookies from JS, like it was described at: http://www.w3schools.com/js/js_cookies.asp Get cookie by name As you…
user2402179
90
votes
7 answers

jQuery $.cookie is not a function

I am trying to set a cookie using jQuery: $.cookie("testCookie", "hello"); alert($.cookie("testCookie")); But when I load my page, I receive the error "$.cookie is not a function". Here is what I know: I have downloaded the jQuery cookie plugin…
ElliotSchmelliot
  • 7,322
  • 4
  • 41
  • 64
90
votes
7 answers

Setting cookies with net/http from the server

I'm trying to set cookies with Go's net/http package. I have: package main import "io" import "net/http" import "time" func indexHandler(w http.ResponseWriter, req *http.Request) { expire := time.Now().AddDate(0, 0, 1) cookie :=…
Tech163
  • 4,176
  • 8
  • 33
  • 36
89
votes
3 answers

Set start date and expiration date for Rails cookies

How do I set a Rails cookie to start and/or expire at a certain date?
nirmal
86
votes
4 answers

Do I have to store tokens in cookies or localstorage or session?

I am using React SPA, Express, Express-session, Passport, and JWT. I'm confused about some of the different client-side storage options to store tokens: Cookies, Session, and JWT / Passport. Do tokens have to be stored in cookies, even if I can…
Faris Dewantoro
  • 1,597
  • 4
  • 17
  • 31
86
votes
5 answers

Rails sessions current practices

Anyone have any "best practices" tips for Rails and sessions? The default session type for Rails 3 is still CookieStore, right? I used SqlSessionStore for a while and it worked well, but I may move away from that in favor of CookieStore. Is it…
Lukas
  • 3,175
  • 2
  • 25
  • 34
85
votes
9 answers

Why does Chrome ignore local jQuery cookies?

I am using the jQuery Cookie plugin (download and demo and source code with comments) to set and read a cookie. I'm developing the page on my local machine. The following code will successfully set a cookie in FireFox 3, IE 7, and Safari (PC). But…
Nathan Long
  • 122,748
  • 97
  • 336
  • 451
84
votes
8 answers

Get cookie with react

I need to know if my user is connected or not. For that I want to read the cookies that I set in the server side with express-session : app.use(session({ secret: 'crypted key', resave: false, saveUninitialized: true, cookie: {…
Jackal
  • 1,041
  • 1
  • 10
  • 13
84
votes
1 answer

How does Facebook set cross-domain cookies for iFrames on canvas pages?

I was browsing Facebook's documentation reading about canvas applications and I came across an example application: http://developers.facebook.com/docs/samples/canvas. As I read through their example, however, I got very confused about their use of…
Aaron Gibralter
  • 4,773
  • 3
  • 35
  • 50
83
votes
3 answers

what is ASPXAUTH cookie?

While working with ASP.Net Forms Authentication I came across the .ASPXAUTH cookie. I have a couple questions: What is the purpose of this cookie? What is the location of this cookie?
balaweblog
  • 14,982
  • 28
  • 73
  • 95
83
votes
20 answers

Detecting if a browser is using Private Browsing mode

I'm building an extranet for a company paranoid about security. They want to make sure that (among other things) their users are browsing the site with the Private Browsing mode switched on in their web browser so that no cookies or history is…
Steve
  • 3,601
  • 4
  • 34
  • 41
81
votes
8 answers

What is the difference between Sessions and Cookies in PHP?

What is the distinction between Sessions and Cookies in PHP?
Harsh
  • 2,078
  • 6
  • 22
  • 37
81
votes
4 answers

How to manage log in session through headless chrome?

I want to create a scraper that: opens a headless browser, goes to a url, logs in (there is steam oauth), fills some inputs, and clicks 2 buttons. My problem is that every new instance of headless browser clears my login session, and then I need…
Anton Kurtin
  • 813
  • 1
  • 7
  • 7