Jump to content

Archived

This topic is now archived and is closed to further replies.

ScottRiley

Users online

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

Share this post


Link to post
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

Share this post


Link to post
Share on other sites

×

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.