364,088 Members | 5483 Browsing Online
Community for Developers & IT Professionals
Bytes IT Community

A Simple Code To Count How Many Guests Are Online..

Nadeem
P: 1
I need a simple code to count how many guests are currently online for my site www.morpgr.com. any type of code will do.. :D
Apr 16 '06 #1
Share this Question
Share on Google+
6 Replies


nathj
Expert 100+
P: 850
Hi Nadeem,

Welcome to Bytes.

I assume you have users information stored in a table. I assume that when
they log in you updated a field to show they are logged in. You can then run a simple select statement to get all those that are logged in. This could be all those who are logged in and then return a count of the rows using mysql_num_rows or it could be select statement that uses a count(*) and Group By Clause.

I've made a lot of assumptions because you haven't given much actual information.

nathj
Nov 26 '08 #2

MrMancunian
Expert 100+
P: 449
Congratulations, you just answered a post from 2,5 years ago...
Nov 28 '08 #3

nathj
Expert 100+
P: 850
@MrMancunian
I know, it was posted before I joined. But hey perhaps it's still helpful?
Nov 28 '08 #4

Markus
Expert Mod 2.5K+
P: 4,422
@MrMancunian
It matters not; another user may find this helpful.
Nov 28 '08 #5

NeoPa
Expert Mod 15k+
P: 20,527
@nathj
Absolutely!

The fact that anyone would question that reflects only on their understanding, not yours.

Answering old questions is really quite a useful exercise, beneficial in many ways. Newer ones are probably more important of course, but the more threads we have that have associated answers, the more helpful the site will be as a general resource for those of us (we all fit into that category somewhere) that are looking for help. Please do not be dissuaded from continuing to answer any thread, of whatever age, that you feel you can help with.
Dec 11 '08 #6

ciranjeebxtreme
P: 4
### Guys Try This Out ###

1.Write The onlineguest.php Script

<?php
//Now Grab The Users Info You Want To Store In Your Database
$user_ip=$_SERVER['REMOTE_ADDR'];
$user_agent=$_SERVER['USER_AGENT'];
$entry_time=time();
$exit_time=time()+(60*60*24); //Remove User Info From Database In 24 Hours From //Now
if(!empty($user_id) || !empty($user_agent)) {

$dbc=mysqli_connect('localhost','username','passwo rd','databasename');
//Query Now To Check If Guest Is New Or Already Entered
$que="SELECT user_ip FROM guests WHERE user_ip='$user_ip'";
$re=mysqli_query($dbc,$que);
if(mysqli_num_rows($re) == 0)
{
//Guest Is New So Enter Guest Info In Database
$que="INSERT INTO guests (user_id,user_ip,entry_time,exit_time,user_agent)
VALUES (0,'$user_ip','$entry_time','$exit_time','$user_ag ent')";
mysqli_query($dbc,$que);
}
//Create Function To echo Out Total Guests From Database
function total_guests($dbc)
{
$que="SELECT * FROM guests";
$re=mysqli_query($dbc,$que);
$total = mysqli_num_rows($re);
$online='Online Guests('.$total.')';
return $online;
}
echo total_guests($dbc);

//Run Query That AutoMatically Deletes A Guest Row During Their Exit_time Every Time //The Script Has Been Called

$que="DELETE user_id,user_ip,entry_time,exit_time,user_agent FROM guests
WHERE exit_time='$entry_time'";
mysqli_query($dbc,$que);
mysqli_close($dbc);
}
//end Of Script
?>


2.Create Database With Table Named guests

CREATE TABLE 'guests' (
user_id INT AUTO_INCREMENT NOT NULL,
user_ip VARCHAR(16) NOT NULL,
entry_time VARCHAR(32) NOT NULL,
exit_time VARCHAR(32) NOT NULL,
user_agent VARCHAR(16) NOT NULL
);

3.Create The Database
4.Modify The Script Depending On Your Database Name
5.Add The onlineguests.php Script Into Your Index Page Using require_once() -
example :
<div id="guest"><?php require_once('onlineguests.php'); ?></div>

6.This Script Is Just a Improvised One By Me ....
Hope It Will Help You Guys.........
If You've Any Problems With The Script Let Me Know...
ciranjeebxtreme@gmail.com
Sep 24 '10 #7

Post your reply

Help answer this question



Didn't find the answer to your PHP question?

You can also browse similar questions: PHP