6

What is the best way to destroy all sessions (not just the one of the current user)?

Jean-François Fabre
  • 137,073
  • 23
  • 153
  • 219
n00b
  • 16,088
  • 21
  • 56
  • 72
  • 8
    Blow up your server. – derekerdmann Aug 17 '10 at 14:38
  • 1
    close, but not quite a duplicate... 1226040 asks for user's session, this asks for ALL sessions, not just the current user's session. – Timothy Aug 17 '10 at 14:39
  • 2
    @derekerdmann: That might not delete them all. If the hard drive hasn't been burned to a crisp, the session data may still be on it. :) – cHao Aug 17 '10 at 14:45
  • What would be the purpose of such action? – Gumbo Aug 17 '10 at 14:45
  • 2
    @Gumbo: The only one i can think of, is invalid/stale session data causing problems with changes to the code. But there's probably another i'm missing. – cHao Aug 17 '10 at 14:47
  • Oh, there's another -- if the session data is in the user's directory, zipping up the site could zip up all of those sessions too. Getting rid of the sessions could shrink the zip file's size dramatically. – cHao Aug 17 '10 at 14:50
  • @Gumbo To force, for example, logout on all clients. – zaf Aug 17 '10 at 14:56
  • @cHao: yup, saw that many moons ago (on a site that stored eeeeeverything in the session and didn't check if session data was still fresh vs. the database) – Piskvor left the building Aug 23 '10 at 09:10
  • 1
    @Gordon this question and the one cited as 'duplicate' discuss different matters. Namely the other question is poorly stated but actually referring to all session *data* not all *sessions*. – That Realty Programmer Guy Aug 20 '15 at 14:30

1 Answers1

13

It depends on how your sessions are being stored. If they're in a database, just delete them. If they're on the file system somewhere like in /tmp, just delete them. For information on sessions in PHP, check out the manual. http://www.php.net/manual/en/book.session.php

Use session_save_path() to find where your session files are being saved by default, unless you're sending them to a memcached,mysql or such. From terminal or PHP issue the system command. For instance

Shell: rm -rf /var/lib/php/session
PHP: shell_exec('rm -rf '.session_save_path() );
Timothy
  • 4,630
  • 8
  • 40
  • 68