Somnath Muluk Somnath Muluk - 11 months ago 86
PHP Question

Are cookies and sessions are depend on each other?

Are cookies and sessions depend on each other in PHP?

Does deleting or clearing either one of them affect the other?

Does by disabling either one of them in the browser affect the other?

P.S. I am newbie.

Edit: I was newbie at time of writing question. This question is faced by many newbies.

Answer Source

They are totally independent...

  • Cookies cannot store unlimited value, sessions can
  • You cannot store data in a cookie if user browser cookie is disabled where in session you can, because session id can append to URL
  • It is better to store data in sessions than to store in cookies because cookies can be tempered
  • If you delete cookies, then only those functionalities in your site will be disabled in which you are retrieving these cookies data but you'll be logged in and if you delete session cookie, you'll be logged out.. (1)
  • Cookies are stored on client machine where session are stored on your server
  • A session is ended if you close you browser while cookies stay there unless they are manually removed by the user or till they are expired

Inshort you've better control over sessions than on cookies

(1) For example if you are setting a cookie name demo and you are using a splash screen unless and until the demo is set you'll show a splash screen

if(!isset($_COOKIE['demo'])) { //Now this will show lightbox always if user has disabled his cookies