Telling how many people are on the site using PHP

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
evilmonkey
Forum Regular
Posts: 823
Joined: Sun Oct 06, 2002 1:24 pm
Location: Toronto, Canada

Telling how many people are on the site using PHP

Post by evilmonkey »

I want to tell how many people are browsing my site right now. Can I do this using PHP?

Also I want to put in a hits counter. Can i do this using the onload function in HTML or is there a simpler way? How do I use the onload function? The current hits will be stored in a db, so it should be something like:

Code: Select all

<body "onload">
<?php
//connect to the db
$query="Select value From table";
$result=($query, $connection);
$value=$value+1
//and insert it back...
?>
</body>
Am i way off? :D

Thanks.
Last edited by evilmonkey on Tue Jan 07, 2003 5:24 pm, edited 1 time in total.
User avatar
Elmseeker
Forum Contributor
Posts: 132
Joined: Sun Dec 22, 2002 5:48 am
Location: Worcester, MA

Post by Elmseeker »

Yes, look at the bottom of the front page of these forums and you will see where it says who is logged in as well as how many guests (people not logged in). I am not sure exactly how it is done but most likely it uses a DB table and sessions to keep track of users.


[Edit]

Hmmm...just came across This while browsing through phpclasses.org, think it's exactly what you asked for!
User avatar
evilmonkey
Forum Regular
Posts: 823
Joined: Sun Oct 06, 2002 1:24 pm
Location: Toronto, Canada

Post by evilmonkey »

I can't seem to access the site although I registered. Weird. Can anyone else help?

Thanks
User avatar
hob_goblin
Forum Regular
Posts: 978
Joined: Sun Apr 28, 2002 9:53 pm
Contact:

Post by hob_goblin »

there are tons on http://www.hotscripts.com/ look for "counters" and "real-time counters"
heimei_lovely
Forum Newbie
Posts: 1
Joined: Tue Jan 07, 2003 9:43 pm

Post by heimei_lovely »

you can use session to do this,when you open a page,it will start a session,and when you close the page,it will destroy the session.so it's easily to judge who's online,and how many people is online.Of course you must create a online table in the database.
DaiWelsh
Forum Commoner
Posts: 36
Joined: Wed Jan 08, 2003 9:39 am
Location: Derbyshire, UK

Post by DaiWelsh »

If you create a sessions table something like

CREATE TABLE Sessions (
SessionID int Auto_Increment Primary Key
,SessionHits int
,SessionLastTime timestamp
)
Then on every page of your site check for a session variable (lets say $session_id), if it exists run a query to

UPDATE Sessions SET SessionHits = SessionHits + 1 WHERE SessionID = $session_id

if it doent exist then

INSERT INTO Sessions (SessionHits) VALUES (0)

and

SELECT LAST_INSERT_ID() As NewSession

then finally set the session variable to the New Session Id as just retrieved.

Then anywhere you want to get count of those online do

SELECT count(*) As NumOnline FROM Sessions WHERE SessionLastTime > Whatever time period you determine to be "active".

For a lighter load on the DB (but removes the hit counting) you can store a second session var with the time that you last updated the db for this user and only update the timestamp every 5 mins or so.

HTH,

Dai
User avatar
evilmonkey
Forum Regular
Posts: 823
Joined: Sun Oct 06, 2002 1:24 pm
Location: Toronto, Canada

Post by evilmonkey »

Thanks everyone, I'll take a look at what you have given me. :D

Cheers.
Post Reply