Reputation: 1553
I was reading the wiki for cookies and they said it is needed to retain stateful information such as item in cart etc. But why do you need to do it? Why don't just store the state in database table for example?
Upvotes: 2
Views: 141
Reputation: 11732
You can store the state in the db but you need to know who is the owner of that state, so you need to identify the client in between the requests.
Think of the cookies like a caller id for the browser. That is their main use. When you hit my web server the first time with your browser, I say, 'hey, let me set this caller id somewhere on your browser' so next time when you call, I'll just read it and then I know it's you again. Once I know it's you, then I can look in my db for more info, such as your cart items.
Also the caller id I set can't be read or modified by other websites, if they want to identify you they need to set their own.
Upvotes: 3
Reputation: 1759
Cookies are useful to preserve information on the client side.
For instance, in the example you give, with a cookie you could save the cart of someone who isn't logged in or registered yet.
Whereas if you were using a database to retrieve such data, you would need a reliable way to identify the current visitor, and you probably wouldn't have many options apart from asking for a login.
In general, you would want to put in cookies information that would make the life of your visitor easier, but that is not essential to preserve (cookies do expire, they can be emptied, and the visitor could use a different browser etc).
Upvotes: 1