PHP does not have any asyncronous ability, so there would be no way to check which users are active on a page. There are a few ways to do it. The easiest would be to add a user's session and time to a database table with each page. You would then have php look at how many users were on the page in the last 30 seconds or so. You could also call this with an Ajax request and auto-update ever few seconds, for a running count.
Basically what jestep said, Sessions + possibly MySQL (upon each request you can sanitize the database by removing users with a last-known-activity older than say 5, 10, 15 minutes etc.) Course if you go the AJAX route, you can have that update the database ever 5 or 10 seconds for a tighter representation of who's on. Using JQuery's $.get() or $.load() would work well in this case if you make the php simply echo back a number.
Store the sessions each time the page is loaded Assume that after a set time, like 5 minutes they are off the page, but once they go to a new page or refresh, tht is readded in Use mysql to store he info, I already coded one, I can sell it to you if you like. Just let me know