Polling script to determine the continuous time a user has been connected
Structure of your Javascript is tightly coupled with your logic, decoupling will allow change without major changes.

a) Move you Ajax initialization [xmlHttp] to a usable function.

b) UnIntended display of UserId req.send("data=&lt;?php echo $user-&gt;id; ?&gt;"); as plain text inside your javaScript is security risk

c) Refactor your client script and least packit(dean edwards works ok) and compress it.

d) Using a comet based server is Best solution to this rather than polling the server at defined intervals. I would recommend you learn APE Project

another point! you can do it this way, when user navigates away from page ask them a few seconds to send ajax request. You have login time and now you have leaving time , calculate Hit rate yourself.
Also using LocalStorage will be viable option too