Jump to content

Users online


ScottRiley

Recommended Posts

Hi.  I'm looking for a function/code that will store the number of uses logged in to my site.  I already have teh login form and everything set up, all is going fine, but I was wondering if there was any way to get the number of users loggedn-in.  I've searched google but failed to find anything useful.

Thanks in advance,
Scott
Link to comment
Share on other sites

no one i have come across including my self has really devoloped a code that everyone can use as people lay out there signin forms and everything diffrently but the best way i found was to start from scratch and log sessionID in a mysql table with datetime stamp and wether they show as being logged in or not and upfdate the time every page they go on.. and then the bit that show's users online is just checking for any user in past 5minutes in datetime stamp and count members and guests..


This is personal;y the code i currently use if this is any use
[code]<?php
$page_name=str_replace('/','',$display);
$chk_hits=mysql_query("SELECT * FROM hits WHERE session = '$sess_id' and page = '$page_name'");
$chk_hits=mysql_fetch_array($chk_hits);
if ($chk_hits) {
if ($session_username) {
$hits=mysql_query("UPDATE hits SET `datetime` = NOW(), `member` = '$session_username' WHERE session = '$sess_id' AND page = '$page_name'") or die(mysql_error());
}
if (!$session_username) {
$hits=mysql_query("UPDATE hits SET `datetime` = NOW(), `member` = 'guest' WHERE session = '$sess_id' AND page = '$page_name'") or die(mysql_error());
}
} else {
if ($session_username) {
$hits=mysql_query("INSERT INTO hits (`session`, `page`, `member`, `datetime`) VALUES('$sess_id','$page_name,'$session_username',NOW())");
}
else {
$hits=mysql_query("INSERT INTO hits (`session`, `page`, `member`, `datetime`) VALUES('$sess_id','$page_name','guest',NOW())");
}
}
$guests=mysql_query("SELECT count(*) AS `guest_count` FROM hits WHERE member = 'guest' AND `page` = '$page_name' AND DATE_SUB(NOW(), INTERVAL 5 MINUTE) < datetime") or die (mysql_error());
$guests=mysql_fetch_array($guests);

$members=mysql_query("SELECT count(*) AS `member_count` FROM hits WHERE member != 'guest' AND `page` = '$page_name' AND  DATE_SUB(NOW(), INTERVAL 5 MINUTE) < datetime");
$members=mysql_fetch_array($members);
$members_all_count=0;
$guests_all_count=0;
$guests_all1= mysql_query("SELECT count(*) AS `guest_count`  FROM hits WHERE member  = 'guest' AND DATE_SUB(NOW(), INTERVAL 5 MINUTE) < datetime GROUP BY session") or die (mysql_error());
$members_all1=mysql_query("SELECT count(*) AS `member_count` FROM hits WHERE member != 'guest' AND DATE_SUB(NOW(), INTERVAL 5 MINUTE) < datetime GROUP BY session") or die (mysql_error());

while ($members_all=mysql_fetch_array($members_all1)) {
$members_all_count++;
}
while ($guests_all=mysql_fetch_array($guests_all1)) {
$guests_all_count++;
}


$total=mysql_query("SELECT count(*) AS `hits_count` FROM hits WHERE `page` = '$page_name'");
$total=mysql_fetch_array($total);

$guests_count=$guests['guest_count'];
$members_count=$members['member_count'];

$total_count=$total['hits_count'];

echo("<strong>Page Stats</strong><Br>");
echo("Online Members: $members_count <br>");
echo("Online Guests: $guests_count <br>");
echo("Total Hits: $total_count <br>");
echo("<strong>Site stats</strong><Br>");
echo("Online Members: $members_all_count <br>");
echo("Online Guests: $guests_all_count <br>");

?>[/code]



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