When should I use session variables instead of cookies?

前端 未结 12 765
囚心锁ツ
囚心锁ツ 2020-12-07 11:12

Session variables and cookies seem very similar to me. I understand the technical differences, but how do you decide when to use one vs. the other?

12条回答
  •  甜味超标
    2020-12-07 11:42

    Your definite Guide

    N.B - A cookie is stored on users' browsers, and a session is stored on your hosting server machine.

    When to Use

    1. Use a cookie when you want your application to remember user's data always, even when they have closed their browsers. E.g whenever you type www.facebook.com it takes you to your account, even when your browser has been closed and re-opened.

      Because any data kept in a session is cleared off once you close your browser.

    2. Use a cookie when the user information to be stored is much larger than normal. ... With a session, if you have a larger user base, like Facebook, think of how it will look storing all user sessions on the hosting machine.

    3. Use a session when the user information to be stored is not larger than normal, and you don't want the public to have access to your user variables...

提交回复
热议问题