Hmmm - this is something I am working on at the moment as well.
I have actually come up with 2 possible ways of doing this... each depends on how much control you have over the box your site is on, whether you have MySQL connectivity etc.
Ok - getting on with the point and stopping dribbling ....
Method 1
Simply count the session files currently present in your web servers session directory (session_save_path() function will give you the directory being used by your site). Then just count the number of files beginning "sess" (or whatever your session files are set up to use).
This method has a couple of drawbacks - it means that your PHP must be set to have a set timeout period (usually 20mins) and that your site either has it's own Session directory (which it normally shud because of security issues) or that your site is the only site running on that box (otherwise you could be counting other sites session files as well which would invalidate your count).
Method 2
This involves adding some additional scripting to each of your pages which will register when a user has visited a page.
Each time they visit a page - add an entry to a table with the time the page access occured, and the users session id (you might also want a user id or something if you want to track it back to a username and be really flash saying "fred, Jim and bob are currently online").
When you do a check for how many people are on your site at present, just count all the rows which have been added in the last 20 mins.
Now - I know what ya all shouting - "but my table is gonna end up enourmous if I do that for every page access". So - the resolution would be to peridoically (every time you do the user count if you want) do a deletion on the database deleteing all records which are older that 20 minutes (of whatever session timeout value you have).
The drawbacks to this method are more obvious - more page overheads in several queries to a database when users visit a page, having to have another table in your db etc etc.
Personally - I go form method 2. File operations are notoriously long, but also don't let you track the session back to a user (i.e. you can say 23 people are using the site, but you cannot say "fred, steve etc are logged in") which I find particularly useful.
The drawback with all of this is that you never really know how many people are looking at your site - sessions are set to timeout after 20 minutes (as default) and so, your user count could be upto 20 minutes old ... the wonders of web technology huh 🙂