mlukac89 Posted October 27, 2015 Share Posted October 27, 2015 Hi i wonder how i can check if user is online or not, when i login user i put time() in database. This stores ip in other table, but what if user browser crash ? He will still be online. ( BTW i found this tutorial on some page its not mine code. ) So what i need to show me if user is online or not. So if user dont do nothing on any page on website or crash after lets say 5 min show him like offline. Thanks in advance. <?php //server info here $server = "localhost"; $db_user = "root"; $db_pass = ""; $database = "test"; $timeoutseconds = 300; //this is where PHP gets the time $timestamp = time(); $timeout = $timestamp - $timeoutseconds; $mysqli = new mysqli($server, $db_user, $db_pass, $database); //insert the values $insert = "INSERT INTO useronline VALUES (?, ?, ?)"; $stmt = $mysqli->prepare( $insert ); $stmt->bind_param( 'iss', $timestamp, $_SERVER['REMOTE_ADDR'],$_SERVER['PHP_SELF'] ); if(!$stmt->execute()) { print "Useronline Insert Failed > "; } //delete values when they leave $delete = "DELETE FROM useronline WHERE timestamp < ?"; $stmt = $mysqli->prepare( $delete ); $stmt->bind_param( 'i', $timeout ); if(!$stmt->execute()) { print "Useronline Delete Failed > "; } //grab the results $result = "SELECT DISTINCT ip FROM useronline WHERE file = ?"; $stmt = $mysqli->prepare( $result ); $stmt->bind_param( 's', $_SERVER['PHP_SELF'] ); if(!$stmt->execute()) { print "Useronline Select Error > "; } //number of rows = the number of people online $user = $stmt->num_rows; //spit out the results $mysqli->close(); if($user == 1) { print("$user user online\n"); } else { print("$user users online\n"); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/298866-showing-user-status-is-online-or-offline/ Share on other sites More sharing options...
Psycho Posted October 27, 2015 Share Posted October 27, 2015 (edited) There is no definitive way to know that a user is "online". A server doesn't know what the user's state is between one page request and another. All you can do is store a timestamp for each page request and if a user has not requested a page in (some time that you define) assume they are no longer on your site. Now, you *could* implement some JavaScript to make an AJAX call every X minutes to better know when they are not on your site, but the overhead of doing that is not worth it. EDIT: Also, do not DELETE records when you "think" they leave. Just use the last timestamp as the last time the user has made a request. Delete's are big performance hogs and there is no need for it in this context. Edited October 27, 2015 by Psycho Quote Link to comment https://forums.phpfreaks.com/topic/298866-showing-user-status-is-online-or-offline/#findComment-1524492 Share on other sites More sharing options...
mlukac89 Posted October 27, 2015 Author Share Posted October 27, 2015 No i dont want to delete last login time because i use that to see when user was last online, i update record on login only. So lets say it like this, i have a file with main data ( connection to db, session, classes .. etc. ) now if i made here some function and call it, then every user that access any page and for example to see user profile page there will be like "last login from database < 5 min timeout" ( but here if its logged 7 hour ago will be offline so its not good pff ), so how i can know if any user browsing on page ? So if user was not doing anything on any page then will be shown as offline. But problem is that how to make function like that and implement in main file. Quote Link to comment https://forums.phpfreaks.com/topic/298866-showing-user-status-is-online-or-offline/#findComment-1524495 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.