Jump to content

[SOLVED] Showing online users?


JP128

Recommended Posts

First thing to do is add a column to your users table to be used to record whether a user is online or not. There a couple of variations this can be used, either to record purely either 'online' or 'offline' or to record a timestamp when a user logs in or is active.  The problem with using 'online'/'offline' it is going to be difficult to update when a user is not online so I would recommend using a timestamp column.#

ALTER TABLE tablename ADD COLUMN columnname DATETIME;

Thats the easy bit! The train of thought here is that whenever a user logs in or access another page, this column should be updated with a current timestamp.  Then when wanting to see if a user is online, you can query this column and check to see if the timestamp is within a set amount of time from the current time.  If within the preset time limit, we assume the user is online.  You will have to decide on the time limit you are going to check within, but obviously this is not going to be 100% accurate because a user may log out at anytime within the time limit.

Am I making sense this far?
Link to comment
Share on other sites

You need to add a bit of script on the beginning of each page in your site that will update your new column.  I suggest you do this in an include file.  You will need to check if there is a user logged in first, and then run a query to update the timestamp.

How do you record users are logged in? Do you use sessions? What variables do you use in the session?
Link to comment
Share on other sites

Sessions, and right now, I have a way to do it.. when the log on, set useronline to 1, and if its 1, say they are online, and when they click logout.. set to 0.

$_SESSION['username']...

but I need to go to sleep.
add me to AIM: Stropfo, or yahoo messenger: johnny_ensign@yahoo.com
Link to comment
Share on other sites

[quote author=emehrkay link=topic=119225.msg488299#msg488299 date=1166547623]
the easiest way would be to add a field to your user table called last_online

then create a lil function that runs at the load of every page that updates that field

then run a query to see who has been online in the last x mins
[/quote]

Thats what I said to start with?
Link to comment
Share on other sites

OK, couple of assumptions then: you have table 'users' to which you have added the column 'online'  (DATETIME) and when a user is logged in, you have a session variable called 'username' which is only present when somebody is logged in.  The table 'users' also has column 'username'.

Create the following file and save as online.php

[code]
<?php

if (isset($_SESSION['username'])) { // checking to see if there is a user logged in
// user logged in so update current time in 'online' column of 'user' table
$query = "UPDATE users SET online = now() WHERE username = \"".$_SESSION['username']."\"";
$result = mysql_query($query);
}

?>
[/code]

Now include the above script at the beginning of every page, ensuring that you position it after you start the session and connect to the database

[code]
<?php

// start session
session_start();

// connect to database

// include new script
require('online.php');

?>
[/code]

Now all you have to do is run a query to see if the timestamp stored in the online column is within a tolerable recent time limit to see if the user is online.  We are going to use 5 minutes as our tolerable time limit.

[code]
<?php

// create query that will return the difference between current time and 'online' time
// this will be returned in seconds but for some reason in this calculation the figure is based
// on 100 seconds in a minute!?
$query = "SELECT now() - online AS diff FROM users WHERE username = \"".$_SESSION['username']."\"";
$result = mysql_query($query);
$row = mysql_fetch_array($result);

// check returned value is within tolerance
if($row['diff'] < 500) {
// user has been online within the last 5 minutes
echo "User is online";
} else {
// user has NOT been online within the last 5 minutes
echo "User is offline";
}

?>
[/code]

I have not checked this last bit but give it all a go and see if it works!
Link to comment
Share on other sites

I modded your script some Chip... I made the include this:
[code]
<?php
$query1 = "SELECT username,online FROM users WHERE now() - online<=500";
$result1 = mysql_query($query1);
while ($row1 = mysql_fetch_assoc($result1)){
if($row1['diff'] < 500) {
// user has been online within the last 5 minutes
echo "<a href='http://jp128.mooo.com/userprofile.php?username=".$row1['username']."'>".$row1['username']."</a> ";
}
}
?>
[/code]
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.