Jump to content

Showing user status is online or offline


mlukac89

Recommended Posts

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");
}
?>
 
Link to comment
Share on other sites

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 by Psycho
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.