How to print all session variables currently set?

后端 未结 7 2059
眼角桃花
眼角桃花 2020-12-12 18:56

Without having to call each session variable by name, is there a way to display the content of all the session variables currently set?

7条回答
  •  情书的邮戳
    2020-12-12 19:24

    Not a simple way, no.

    Let's say that by "active" you mean "hasn't passed the maximum lifetime" and hasn't been explicitly destroyed and that you're using the default session handler.

    • First, the maximum lifetime is defined as a php.ini config and is defined in terms of the last activity on the session. So the "expiry" mechanism would have to read the content of the sessions to determine the application-defined expiry.
    • Second, you'd have to manually read the sessions directory and read the files, whose format I don't even know they're in.

    If you really need this, you must implement some sort of custom session handler. See session_set_save_handler.

    Take also in consideration that you'll have no feedback if the user just closes the browser or moves away from your site without explciitly logging out. Depending on much inactivity you consider the threshold to deem a session "inactive", the number of false positives you'll get may be very high.

提交回复
热议问题