BlueBerry BlueBerry - 1 month ago 6
PHP Question

Which to use session variable or cookie?

I have made shopping site which includes a product-display page and a cart.
I have followed various tutorials of how to make a cart-mechanism and the main method of retrieving the posted variables was to store them in a session variable.

After that, I've read documentation and a few other posts saying that storing a values in a cookie is easier to manipulate as it is stored on the local machine while storing in session variable is rather tough.

So, my question is why use session variables in the first place?

Answer Source

Both cookies and session variables are used consequently. cookies aren't as reliable as session variables. In your case, cookies can be deleted from the browser leaving your code with a loop-hole. Session variables are more practical as they are stored on the server and impregnable from local-side interventions. Using session variables to store your data is the best option.