Are HTTP Cookie 'values' the same for every user?


#1

Hi everyone… :slight_smile:

This is a general question. I’ve read all the documentation about cookies, but I can’t find the answer to this: Does each user get a different value, or not?

Let’s say we set a new cookie in PHP:

$cookie_name = "user";
$cookie_value = '12345';
$cookie_expiry = time() + (86400 * 30); // 86400 = 1 day
$cookie_path = '/'; //cookie will be available on all pages on website

setcookie($cookie_name, $cookie_value, $cookie_expiry, $cookie_path); 

//assign the cookie value to a variable if it exists
$cookie = !isset($_COOKIE[$cookie_name]) ? 'undefined' : $_COOKIE[$cookie_name];

echo $cookie; // returns 12345

It returns 12345 because that’s what I set it to. So how does that differentiate one user’s session to another’s?

If I want to put the current user’s “cookie value” into the database and try to echo out everything associated with that cookie value, wouldn’t it print out Every guest user’s data with the same value?

I’ve read somewhere that the browser ‘encrypts’ each user’s cookie, but how do I get that encrypted value? (and how do I know that ‘12345’ is not encrypted into the same code each time?)

So many questions - I think it will fall into place once I grasp how it works behind the scenes (I read so much docs about this, but cannot figure out how to display, for example, a user’s shopping cart based on his cookie value if all cookie values are the same).

I’m trying to submit the cookie value of each user into the database, so that I can display all his own interactions on the website based on his cookie value.

Maybe the cookie value should not be set to something stagnant, rather, some user input like his name or something?

Agh sorry about this…I must sound soo silly. Been working with PHP sessions for a while and that’s easy now, but cookies are my nightmare!


#2

When you set a cookie value that is probably store in the browser that has performed that request. So if you set a specific value for each user only that user should get the value

But! Why are you using cookies? If you need to set user data then just store it in the session.


#3

Thank you, that explains a bit. I’m trying to use cookies for a shopping cart project, so that users who are not logged in can have a shopping cart available for 30 days. Users who log in will have their shopping cart in the database, forever or until they delete it.