-4

I apologize if this belongs outside of stackoverflow but I am at wits-end trying to understand internet cookies.

I have read and completed the tutorials at the following sites:Whatsmyipadress, w3schools, tutorialspoint, and how stuff works.

From what I understand, cookies work like:

  • client visits site,
  • site requests cookie,
  • if cookie does not exist, site creates cookie,

I found this example and tried to use but everyone has the same name and value:

When I ran tests with different users, everyone returned the same combo name:bob

Am I supposed to randomly assign a value? The first link says that users would need to complete a registration-type page and then server would use the info to create an id. This is similar to the tutorial on schools (except their example uses a popup). Is this what I need to do as well?

How are cookies made unique ? From my experience, I wouldn’t be able to personalize any experience because everyone would have the same name/value pair.

halfer
  • 19,824
  • 17
  • 99
  • 186
OctoCatKnows
  • 399
  • 3
  • 17
  • cookies are exchanged in request/response [http headers](https://en.wikipedia.org/wiki/List_of_HTTP_header_fields). Also see [http cookie](https://en.wikipedia.org/wiki/HTTP_cookie). – Mulan Mar 20 '19 at 19:44
  • Im quite interested in understanding this part (from your references): “server sends cookie to client that contains a unique identifier”. So how is the identifier achieved? In R I could use session$token to get the session and assign it I imagine..is there a way to include this in a JS. The name could be something like – OctoCatKnows Mar 20 '19 at 20:34

1 Answers1

0

Cookies are specific to a particular domain, and can also be made specific to subdomains and/or subdirectories within a domain. So there's no conflict between cookies with the same name from different web sites, and you don't have to worry about making your cookies unique (except among all the cookies used by your own site). Each domain can only see and modify its own cookies.

Cookies are also specific to a particular client browser. When you send or get a cookie from one user, it has no effect on cookies from a different user.

Barmar
  • 741,623
  • 53
  • 500
  • 612
  • On the point about uniqueness-this would be on my own site to identify individual users (such as a greeting message that says ‘Hello X’). If my name/value pair are the same, wouldnt everyone be greeted with ‘Hello Bob’? – OctoCatKnows Mar 20 '19 at 20:31
  • Every user has their own cookies. – Barmar Mar 20 '19 at 20:39
  • Understood, but I am hoping to understand how-when I set cookies for users, how such a cookie is unique? For example, with my sample set.cookie script, Visitor1 would be assigned a cookie name ‘name’ with a value of ‘bob’. However, Visitor2 would have the same name and value-on the server end, these users are not unique and I cannot provide unique experience for them . – OctoCatKnows Mar 20 '19 at 20:40
  • You can send the same cookie to two different users, with different values. When they connect again, they send back the value you sent them. So user1 will send back `Bob`, while user2 will send back `Jim`. – Barmar Mar 20 '19 at 20:42
  • I think progress is being made! :) so the value needs to be unique:how is this accomplished? Could simple random generation work? – OctoCatKnows Mar 20 '19 at 20:44
  • It doesn't have to be unique. If you have two users named James, they'll both get `Hi James`. But if you're using the cookie as some sort of authenticator, a long random string is a common method. – Barmar Mar 20 '19 at 21:02
  • Great! Thanks for helping me understand it. I am thinking I will assign a random string via session. Youre a great human-being :) – OctoCatKnows Mar 20 '19 at 21:32