Mehmet Hazar Artuner
Mehmet Hazar Artuner

Reputation: 77

Check if a PHP SessionID is in use?

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?

Upvotes: 0

Views: 1076

Answers (3)

symcbean
symcbean

Reputation: 48367

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)!

Upvotes: 0

Aleksandar
Aleksandar

Reputation: 46

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

Upvotes: 1

Dan Grossman
Dan Grossman

Reputation: 52372

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

Upvotes: 1

Related Questions