1

I'm building a small website project and I am curious if there would be any reason not to do session_destroy() when a user wants to log off? What about just before logging in a new user? The site request a user to be logged in before interacting with the site in any way.

JDelage
  • 13,036
  • 23
  • 78
  • 112

2 Answers2

1

Yes it is. It's actually the common way to do so. If you want an example see the docs for session_destroy() there's a complete example with everything you need to do.

TheGreenkey
  • 143
  • 2
  • 8
  • What would be the uncommon way ? – Samosa Jan 23 '15 at 22:35
  • You can also loop through all session variables and manually unset every variable... There is also a function called [session_unset()](http://php.net/manual/en/function.session-unset.php) – TheGreenkey Jan 23 '15 at 22:40
1

If you are using PHP's built in session management, then it is what you should do at each logout. This way you can make sure that a new user at the same computer can't reuse any saved data that has been stored for the previous user before.

An other way is session_unset, but that, unlike session_destroy does not delete all session data such as data in the session storage. More about the difference: What is the difference between session_unset() and session_destroy() in PHP?

Community
  • 1
  • 1
nxu
  • 2,202
  • 1
  • 22
  • 34