I am creating a website where user will come and do some task. Now what I want is, if user 'X' comes, a cookie should be saved with name 'X' in the browser. And if user 'Y' comes then cookie for 'Y' user should also be saved. But cookie of 'X' should not be deleted or overwrite. Now suppose next time again 'X' visits the website the data in 'X' cookie should be updated, same should be done for 'Y'. If is is possible then tell me exact way to do it. Thanks.
-
It **sounds** like you have a serious misunderstanding of how cookies work. They are stored in the browser and are usually used to identify which user is logged in. Most people expect a *roughly* 1-to-1 correspondence between user and browser. Are you really planning to store user-specific data for multiple users in the same browser? What is your plan to distinguish between users? – Quentin Mar 20 '19 at 10:50
-
actually I have created a game. Suppose 'X' played the game and won 10 time, this info should be saved as cookie, Now suppose 'Y' came and won 5 times. Cookie for 'y' should also be saved. Now next time when 'X' come to play he should be able to see his last victories. – raj Mar 20 '19 at 10:55
-
That doesn't address any of the points I made in my previous comment. – Quentin Mar 20 '19 at 10:56
-
Do you have solution or not? – raj Mar 20 '19 at 10:57
-
No. I don't have a solution. You can tell this because **I haven't posted an answer**. This is because your real problem is unclear. I could give you a literal answer (like dearsina already has, which you don't seem to understand either) but I suspect it won't solve the real problem. That is why I made a comment asking you to clarify your problem. – Quentin Mar 20 '19 at 11:07
1 Answers
You can create whatever cookies you want. Think of cookies as $_SESSION
values that are stored on the user's computer instead of your server. Cookies can be complex arrays, so you could store an array for each user.
Keep in mind that any user of the computer can see what's inside any cookie, so don't ever save anything secret, like passwords or API keys.
Arrays as cookies
You can store arrays as cookies using json_encode()
, like so:
setcookie('your_cookie_name', json_encode($array), time()+3600);
Further reading
Have a look at setcookie()
for further documentation on how to use it in PHP.
Am I doing the right thing?
The most common use for cookies is to identify one user, and to save them having to identify themselves. It doesn't make much sense to store lots of information about or for lots of users on their own machine (instead of your own server), because cookies can be cleared out easily.
You're much better off storing everything on your own server, and only use cookies to identify the current user.

- 4,774
- 2
- 28
- 34
-
Now I want to save only one info but for many user. How to do it with cookies? – raj Mar 20 '19 at 10:58
-
@raj, just change the cookie name to be some sort of user identifier. Then you can store as much data you want for each user. It's a bad idea though. – dearsina Mar 20 '19 at 11:19