Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP Shopping cart without login - cookies vs sessions vs both?

It's a php based web store without user logins because all of the payments are handled via paypal. My question is what would you guys suggest for the shopping cart - cookies, sessions, or both? I'm not too concerned with the longevity of the shopping cart's contents be I'd like for the user to be able to click around and do a few things before they commit the order. I'm leaning towards sessions because some people may still disable cookies on their machines.

like image 962
jreed121 Avatar asked Sep 15 '11 17:09

jreed121


People also ask

Which is better session or cookie in PHP?

Both of them accomplish much the same thing. The main difference between cookies and sessions is that information stored in a cookie is stored on the visitor's browser, and information stored in a session is not—it is stored at the web server. This difference determines what each is best suited for.

Should I use cookie or session for login?

session login is always preferred, if you specifically do not need any cookie variables to set for your webpage. Sessions use either a cookie to pass the session id between pages or add it in the querystring.

What is the difference between PHP session and cookie?

Cookies are client-side files on a local computer that hold user information. Sessions are server-side files that contain user data. Cookies end on the lifetime set by the user. When the user quits the browser or logs out of the programmed, the session is over.


2 Answers

PHP sessions use a cookie with the session id to track the user. I would go with sessions since it will handle all of the identification for you and make things easier and more transparent.

It is also possible to use sessions with no cookies and it will pass the session id around in the URL. That in some cases can be a security risk, but perhaps not so much in your situation.

like image 84
drew010 Avatar answered Sep 23 '22 04:09

drew010


By default, PHP sets a cookie on the visitor's browser to know which session id to use anyway, so the only real difference between the three options in the end would be how much data gets sent up to your server during the request.

That being said, you can also use sessions without cookies by making sure to add ?session_id={session_id();} to all of your internal links and the following to the beginning of every page:

 if (isset($_GET ['session_id'])
   session_id($_GET ['session_id'])

 session_start();

So, recommend using sessions.

like image 20
tdk001 Avatar answered Sep 19 '22 04:09

tdk001