2

What's the best way to search if a PHP session has been created on the server?

For example, store the name Joe somewhere in a session, and then after Joe closes his browser, can I then lookup (in a php script elsewhere on my computer) if Joe's session still exists on the server (i.e. not yet expired)?

d-_-b
  • 21,536
  • 40
  • 150
  • 256
  • Thanks, yea I think I'll have to save the info to a database and then check that on each page load :| – d-_-b May 25 '13 at 06:35
  • by the way html5 has the property of storing sessionstorage and localstorage capabilities you should look into that I believe that can solve ur problem if you dont want to save the session in db.. – Dinesh May 25 '13 at 06:41
  • @Dinesh Huh? When you close your browser it might delete the PHPSESSID cookie, but the server has no way of knowing the client closed his browser, and thus wouldn't delete his session data. – mpen May 25 '13 at 07:10
  • @Dinesh I was wondering, there are some sessions are available when we close the browser.. consider if i set a session `$_SESSION['name'] = 'Dinesh'` and i closed and then i checked for same session and it shows the session will exists. I dont know.. how / hwere it will be stored. – Rafee May 25 '13 at 07:14

2 Answers2

4

Session data is usually stored in the server's temporary directory (the session.save_path setting).

While it is theoretically possible to search through that directory, go through every session file, open it, and look for whether it's Joe's session, it's not a clean approach, and there are many ways it can break - for example if the server doesn't give you a list of the files in the temporary directory to start with. Or what if a user has multiple simultaneous active sessions?

If you really need this, you should probably create a custom session handler that stores its data in its own directory or database. For that custom handler, you can then implement an interface that allows you to query whether a certain user is logged in, or a specific session exists.

Pekka
  • 442,112
  • 142
  • 972
  • 1,088
2

In the server the check the path set to store sessions in php using directive session.save_path

and then try decode the files in there using session_decode() method

More information can be found in below links

Location of session files

Reading Session Data

Community
  • 1
  • 1
leela
  • 555
  • 4
  • 11