0

at one of my project, i store a temporary data in database with a sessionId, and i've to delete them if its sessionId is not in use, is there a simple way to check if session id is in use?

hakre
  • 193,403
  • 52
  • 435
  • 836
  • 1
    Why aren't you storing the data in the session (where it will be cleaned up automatically when the session dies)? – Quentin Jul 18 '11 at 09:29
  • or if you really want to use database - use temporary tables for that – k102 Jul 18 '11 at 09:35
  • possible duplicate of [Check if PHP session_id is in use](http://stackoverflow.com/questions/6206929/check-if-php-session-id-is-in-use) – hakre Jul 18 '11 at 09:39
  • Related: http://stackoverflow.com/questions/520237/how-do-i-expire-a-php-session-after-30-minutes – hakre Jul 18 '11 at 09:44

3 Answers3

1

Store a last accessed timestamp, and delete those rows whose time is X minutes earlier than the current time.

Dan Grossman
  • 51,866
  • 10
  • 112
  • 101
1

You can name your session with session_name ([ string $name ] ),

example: session_name('myname'),

and then just check if there is a session id for that name with session_id ([ string $id ] ),

example:

if(session_id('myname') != "")
       echo session_id('myname');
         else
      //your code here
Aleksandar
  • 46
  • 2
0

is there a simple way to check if session id is in use?

If you are using the default session handler then checking if the session file is present and has been modified since time()-session_cache_expire().

However a much better solution would be to use a database-bound session handler. This is much more flexible when trying to look at session data from outside the session, you could just do a simple join to find the matching data, but the right way to solve the problem is to implement the session management code within the session handler (i.e. delete the data when the session is deleted)!

symcbean
  • 47,736
  • 6
  • 59
  • 94