PHP getting active sessions

后端 未结 3 1672
余生分开走
余生分开走 2021-01-25 15:24

I am trying to echo each active session that has been active in the past 15 minutes, I check using the following code to log their last seen time.

$_SESSION[\'la         


        
相关标签:
3条回答
  • 2021-01-25 16:00

    I think you don't understand $_SESSION global array. It contains data only for current session of user (not for all users of your site). You should save this data in memcached or database.

    0 讨论(0)
  • 2021-01-25 16:25

    Long story short:

    1) First of you need to recall session_start() every time (if you haven't).

    2) You can't read from other sessions with PHP's native session handler. You would need to implement your own session handler.

    I guess you just started using PHP. So here are some basic explanations you need to know about the web, PHP, sessions and cookies:

    Cookies: Cookies are small information and sent to the client's browser when the page is loading. If the browser accepts the cookie (which is NOT always the case), he's sending that cookie information on every request.

    so... doing something like

    <?php
    
    setcookie('my_cookie', 'Hello World!', time() + 60 * 60);
    

    If the client is requesting that page and the code above gets executed, the clients browser is asked to store a cookie from your_domain.com with the name 'my_cookie', containing 'Hello World!'

    Assuming the browser has accepted that cookie, he will be sending it on every page load against your_domain.com - as long as the cookie hasn't expired. This allows you to store small, textual information on the clients side and you will be able to retrieve and read on later requests with $_COOKIE['my_cookie'].

    So what has this to do with sessions? Without cookies we won't be able to track a client. PHP application loses session information between requests. If the client stumbles on your page, invoking the first time session_start(), PHP will create a Session Cookie with a unique ID and allocates somewhere on the filesystem space for data, stored in $_SESSION. This is called a "session storage". So you could start writing data to $_SESSION like $_SESSION['last_seen'] = date("Y-m-d H:i:s");. The browser will send that cookie every time to your server on the next several requests. Recalling session_start(), PHP is now aware of that a session cookie has already been set and reads that ID. If the ID matches against the session storage, previously generated, the $_SESSION var will be populated and you could read the value of $_SESSION['last_seen'] You thought with $_SESSION you would access all the session information from any user, but that's wrong. The session store will just give you that client's session id specific data. As I mentioned before you can't access any other session data with PHP's native session handler, however you could implement your own session handler.

    Most people who start writing their own session handler tend to use a MySQL database as their session storage. I prefer using Redis.

    This may help you: Basic PHP session tutorial: http://www.phpro.org/tutorials/Introduction-To-PHP-Sessions.html

    Writing your own session handler and using MySQL: http://culttt.com/2013/02/04/how-to-save-php-sessions-to-a-database/

    0 讨论(0)
  • 2021-01-25 16:27

    I suggest you actually try to echo "<pre>"; print_r($_SESSION); echo "</pre>" for troubleshooting purposes - this will tell you the structure of the session array.

    PS: I trust you have started the session with a command session_start()?

    0 讨论(0)
提交回复
热议问题