Put simply, you can't. Since HTTP is non persistent (for the most part anyway) when a user visits a page on your site they are connecting for the while to download everything on the page then they disconnect. This is the nature of HTTP.
What you could do as a trick is that when a user comes to your site, every page updates their profile with the date/time using a function like NOW() in MySQL.
Then in your section where you are showing people who are online, you simply pull out those who've been on in the last X amount of time.
e.g. pageheader function in global include
$userid = $_COOKIE['mycookie'];
$query = "UPDATE users SET lastvisit = NOW() where userid='$userid'";
$result = mysql_query($query);
e.g. activity function someone on your page
$query = "SELECT username FROM users WHERE lastvisit >= DATE_SUB(NOW(),INTERVAL '5' MINUTE) ORDER BY lastvisit DESC LIMIT 30";
$result = mysql_query($query);