0

Hi guys,
I would like to have an online user counter on my webpage. However, I don't want my visitors to see how many users that are in my website. I am already using good analytics but it is not giving me real time data.

But how can I see total number of users online at a time?
Any HTML or php codes?

Thank you in advance for your help!

6
Contributors
10
Replies
12
Views
6 Years
Discussion Span
Last Post by minitauros
Featured Replies
  • 1
    diafol 3,720   6 Years Ago

    If you have a login you can view data from a db on an admin page. You can ajaxify this so that it refreshes every 30 seconds or so. Read More

1

If you have a login you can view data from a db on an admin page. You can ajaxify this so that it refreshes every 30 seconds or so.

0

Take a variable and add it into the field in your database .When the registered use logins just then set that variable and once he leaves reset that variable.
Count the number of rows in which that field is set...That's your required outup...

0

Take a variable and add it into the field in your database .When the registered use logins just then set that variable and once he leaves reset that variable.
Count the number of rows in which that field is set...That's your required outup...

But what if an un-registered user visits a site?

I think using $_SESSION will be the best solution for this problem.

0

I thought you were interested in only registered users....
If u want that also then $_session sure is a good move to go on....

0

your problem is that users don't tend to log out. have a last_activity column in your users table or a logged table. this should be updated every time the logged in user opens a new page or interacts in any other way. you then call the db every 30s or so in your admin page. reasonably straightforward. you can search for activity within the last 2 minutes or similar.

0

What I real need, it's an online users counter script for my website, which will show the exact count of the users online at a time.

I have searched all around the web, but didn't found what I'm looking for ...
it excists and it can be made into java, php and html

0

What ardav says is right. You can use PHP+MySQL. You need to create a user activity field and check and update it on every page the user views.

For example

CREATE TABLE user_activity (
user_id INT(11) NOT NULL,
last_active TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (user_id)
)

Then, you need to check and compare results on every page.

For example

$q = 'SELECT COUNT(user_id) AS users_online
    FROM user_activity
    WHERE (UNIX_TIMESTAMP(NOW()) - UNIX_TIMESTAMP(last_active)) <= 60';
$rq = mysql_query($q) or die(mysql_error());
$fetch = mysql_fetch_assoc($rq);
$users_active = $fetch['users_online'];

echo '<p>There are '.$users_active.' users online.</p>';

This would count the users that have been active in the last minute. (I'm not sure about the SQL syntax here, might show errors, but this is just something to lead you into the right direction).

Finally, obviously, you will need to update the table. Something like

UPDATE user_activity
SET last_active = "'.date('Y-m-d H:i:s').'"
WHERE user_id = '.$user_id.'

Edited by minitauros: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.