there are different approaches, one being:
in your main page script, e.g. index.php, have a record written into a database table "visitors" which represents the user.
table visitors (e.g.):
id = primary key
ip
time
in the table, store the current time as well as the visitor's ip address; $ip = getenv("REMOTE_ADDR");
before inserting a new visitor, check by the ip whether he is already stored in the table. if so, don't insert.
for displaying the number of current visitors, execute two steps:
delete all visitors whose entries are older than, say, half an hour (they will have left already)
do a:
select count(*) from visitors
which will give you the number of different visitors who came to your page during the last 30 minutes.
I repeat, there are other ways, but this one's really simple.