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
10
votes
3 answers

Unable to log in to ASP.NET website with requests module of Python

I am trying to log in to an ASP.NET website using the requests module in Python. While logging in manually in the website I can see the following headers as well as cookies. Request…
Vikas Pawar
  • 101
  • 1
  • 6
10
votes
3 answers

How to get cookies from urllib.request?

How to get cookie from an urllib.request? import urllib.request import urllib.parse data = urllib.parse.urlencode({ 'user': 'user', 'pass': 'pass' }) data = data.encode('utf-8') request = urllib.request.urlopen('http://example.com',…
Mwerf
  • 117
  • 1
  • 1
  • 5
10
votes
2 answers

Setting cookies for multiple sub-domains

Is it possible to set a cookie for http://www.example.com from a PHP file located at https://secure.example.com? I have some code that was given to me, that appears to try and fails at this. I was wondering if this is possible at all.
nilacqua
  • 163
  • 1
  • 1
  • 7
10
votes
3 answers

How do I stop users circumventing payment?

I have a site that uses paypal to collect payments for electronically displayed data. Variables can't be passed with the URL through paypal (or I can't get them to work) so I have used cookies to pass the item number. However, a crafty user could,…
Nigel
10
votes
2 answers

Does HTML5 web storage (localStorage) offer a security advantage over cookies?

I was looking up alternative to cookies and I've read about HTML5 web storage here, and I've read a simpler explanation here but I still don't get how it works fully. Can someone offer a slightly non-techinical explanation so that I can then…
dozer
  • 861
  • 1
  • 11
  • 22
10
votes
3 answers

What is the correct way to trigger OWIN cookie middleware set to passive authentication mode?

I have been following the OAuth 2.0 Authorization Server sample code http://www.asp.net/aspnet/overview/owin-and-katana/owin-oauth-20-authorization-server As well as looking at the nugget package Microsoft.aspnet.identity.samples…
Steve
  • 596
  • 2
  • 7
  • 20
10
votes
2 answers

Selenium cookie with another domain

I have a code on selenium to test a form. But first i go to another page and then redirect to the my page. When i set cookies to new domain , i got error : Exception in thread "main" org.openqa.selenium.InvalidCookieDomainException: You may only…
Muhammet Arslan
  • 975
  • 1
  • 9
  • 33
10
votes
1 answer

Django: class-view lacking attribute COOKIES

I am currently playing around with REST and backbone.js and ran into this issue: 'LoginView' object has no attribute 'COOKIES' Here comes the following code for might have caused the problem. I have commented out a few things in the javascript,…
Richard Atterfalk
  • 462
  • 1
  • 10
  • 23
10
votes
2 answers

cookiejar in PHP Curl

In PHP Curl case when we need to store/read cookies in term of web scraping, it feels that many resources out there encourage to use a file for handling cookies with these option curl_setopt($ch, CURLOPT_COOKIEJAR,…
bagz_man
  • 555
  • 2
  • 8
  • 20
10
votes
4 answers

Cookies/Sessions login system

When a user logins I get him/her's ID and save it in a session var. What I wonder is, is this the way to go? Or should I use cookies? so it automatically login and so on. session_start(); ifcorrectlogin { $_SESSION['id'] =…
Erkka
  • 101
  • 3
10
votes
5 answers

Get cookies from NSHTTPURLResponse

I've an extremely weird problem, I'm requesting a URL and I want to get the cookies from it, I've used this way to get the cookies: - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response { NSHTTPURLResponse…
Scar
  • 3,460
  • 3
  • 26
  • 51
10
votes
1 answer

HttpWebRequest.Headers.Add("Cookie",value) vs HttpWebRequest.CookieContainer

When I get response from HttpWebRequest with HttpWebRequest.Headers.Add("Cookie",value) vs HttpWebRequest.CookieContainer, and results are difference. So, What is the difference between they are, and when to use them.
Mr.LamYahoo
  • 1,536
  • 13
  • 28
10
votes
1 answer

Multiple 'Cookie' headers in a node.js request

I've seen how to make a request with a single cookie, and I've seen how to write a response with multiple cookies, but does anyone know how to write a request in node.js using http module (if possible) with multiple 'Cookie' headers? So far the…
twinlakes
  • 9,438
  • 6
  • 31
  • 42
10
votes
2 answers

Cause of angular error: "Error: No module: ngCookies"?

In my app I'm doing this: angular.module('myApp.controllers', ['ngCookies']). controller('AppCtrl', function ($scope, socket, $cookies) { console.log("socket:"); console.log(socket); $scope.component = 'main'; …
CommaToast
  • 11,370
  • 7
  • 54
  • 69
10
votes
1 answer

JavaScript Encrypt?

How to hash/encrypt string value in JavaScript? I need a mechanism to do so for hiding some data in localStorage/cookie? It is something related to security concern but I want some protection for my data.
Deepak Biswal
  • 4,280
  • 2
  • 20
  • 37