When should I use session variables instead of cookies?

前端 未结 12 763
囚心锁ツ
囚心锁ツ 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:53

    Use sessions only if the data is too big for cookies or if the data is so big that it would decrease the performance if you used cookies.

    For example, if you are saving smaller data then the size of a session ID in your cookie, like two login tokens or something similar... Then I don't see why you would use sessions over cookies.

    Also note that PHP session files are saved to disk by default, compared to cookies, which are saved only on the client side.

提交回复
热议问题