1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

PHP members Online

Discussion in 'PHP' started by shivampaw, Nov 5, 2013.

  1. #1
    I have a member bases site where the user logs in and then session is set.

    This code is in my top.php page:
    session_start();
    $user = $_SESSION['user'];
    $userid = $_SESSION['id'];
    $dbuser = $_SESSION['user'];
    $dbid = $_SESSION['id'];
    PHP:
    I was wondering how I can run a mysql query whenever the user logs out, closes the tab or window. It should be a code that can do all these, but I am not sure how. I know the query to run when user logs out but not sure when user leaves the site by closing the tab or window. I will then echo out online users in my online users page but I am not sure about running a query when browser window or tab is closed.

    It has to be a code that works for browser window and tab.

    Any other suggestions for running the query when user fully logs out or leaves the site?
    SEMrush
    Thanks
     
    Solved! View solution.
    shivampaw, Nov 5, 2013 IP
    SEMrush
  2. bartolay13

    bartolay13 Active Member

    Messages:
    735
    Likes Received:
    14
    Best Answers:
    1
    Trophy Points:
    98
    #2
    on logout - you can do logging of data on your logout script
    when closing the tab or window- you can use javascript onUnload

    this is more of a client events so focus more on javascript. though prepare your apis that would do the logging for js.
     
    bartolay13, Nov 6, 2013 IP
  3. #3
    onunload fires not only when the window is closed, but also when the user navigates away from the page. By clicking on a link, or by submitting a form. Even if the link or form points to your own page. Plus, there's no guarantee there'll be enough time to send a new request to the server before the window closes or the user leaves. In short, it's not reliable.

    Instead, you should update a database row, or a cached entry every time the user loads a page. Update the data with the current time, so you know when they were online for the last time. Then, in your "users online" part, you query all users that made some kind of movement in the last, say, 5 or 10 minutes. This won't be 100% accurate, but still better than the onunload solution.

    Additionally, if your users stay on the same page for a longer time, you can also make an AJAX request to the server every couple of minutes, and update the last time. This way, the user will still be considered "online" even if they're on another tab or window at the moment.

    
    // Update "last seen" time
    if (!empty($_SESSION['id']))
    {
        $db = new PDO(/* ... */);
        $stmt = $db->prepare("UPDATE `users` SET `lastseen` = NOW() WHERE `id` = ?");
        $stmt->execute([$_SESSION['id']]);
    }
    
    PHP:
    
    // Users online
    $db = new PDO(/* ... */);
    $stmt = $db->query("
        SELECT COUNT(*) AS `total`
        FROM `users`
        WHERE `lastseen` > DATE_SUB(NOW(), INTERVAL 10 MINUTE)
    ");
    $count = $stmt->fetch(PDO::FETCH_ASSOC);
    
    echo "{$count['total']} users online";
    
    PHP:
     
    nico_swd, Nov 6, 2013 IP
  4. shivampaw

    shivampaw Greenhorn

    Messages:
    42
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    16
    #4

    Looks great but on the update would I put it in my top.php and how do I run it every 2 minutes with javasvript. And I am guessing changing
    INTERVAL 10 MINUTE
    Code (markup):
    to
    INTERVAL 2 MINUTE
    Code (markup):
    Will make it show users online in last two minutes?

    What interval time do you recommend for a good and accurate but not overloading with updates?

    Thanks
     
    shivampaw, Nov 6, 2013 IP
  5. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #5
    I don't know what your top.php is or does, but the name of the file suggests that it might be a good place to put the code in.

    Some people will hate me for this, but if you're using jQuery, use something like the following. (If not, Google for an AJAX tutorial not based on jQuery):
    
    <?php
    if (!empty($_SESSION['id']))
    {
        // 120000 == 2 minutes
        echo
            '<script type="text/javascript">', 
                 "window.setInterval(function() { $.get('updateTime.php'); }, 120000);",
            '</script>';
    }
    
    PHP:
    And in updateTime.php, you make sure the session is started, the user ID exists in $_SESSION, and update the user's row.

    That's right.
    This really depends on how many users and page views you have. But I would probably update every 5 minutes or more, not less.

    If you're afraid of overloading the database, you can also set the last online time of the user in the session. Then before updating, you compare that time to the current time, and only update if the last move has been made 5 minutes ago or longer. This will add some extra inaccuracy on top of it, though. But you could also check if the last move has been made just a minute ago or longer.
     
    nico_swd, Nov 6, 2013 IP
  6. shivampaw

    shivampaw Greenhorn

    Messages:
    42
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    16
    #6
    Thanks :)
    Thanks Ill try out all that code and get back.
     
    shivampaw, Nov 6, 2013 IP
  7. shivampaw

    shivampaw Greenhorn

    Messages:
    42
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    16
    #7
    One more thing, I use mysql query for querying as I am unfamilliar with PDO what would the updateTime.php query be in mysql.

    If it has to be PDO my table is users and the column is lastseen and I would like it to have an interval time of 5 minutes.

    Thanks
     
    shivampaw, Nov 6, 2013 IP
  8. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #8
    Just replace the question mark with $_SESSION['id']. You can take the rest of the queries as they are.
     
    nico_swd, Nov 6, 2013 IP
  9. shivampaw

    shivampaw Greenhorn

    Messages:
    42
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    16
    #9
    Thanks :)
     
    shivampaw, Nov 6, 2013 IP