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!

Recommended Answers

All 10 Replies

Member Avatar for diafol

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.

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

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...

Hope this helps!Thanks for help!

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.

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

Member Avatar for diafol

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.

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

Hi hellojohnatan,

I actually use scripts for this. I am using this one http://www.twospots.com/free-users-online-counter/ .

--------------------------------
<SPAM>

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.'
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.