I've got something like this set up on a site I'm building. When a user is logged in, their user id is put into a session variable. Then every time they reload a page, the database table containing their user id is updated:
$datenow = gmdate("U");
mysql_query("UPDATE users SET lastaccess='$datenow' WHERE name='" . $_SESSION['this_user'] . "'");
Also, in the head of each page I have:
<meta http-equiv="refresh" content="900;URL=index.php?logout=auto">
which will automatically log them out if they walk away for 15 minutes.
Then when they attempt to login you just have to compare the current "$datenow" with the "lastaccess" field in their user record to determine whether they should be able to log in or not.
I'm using it for a slightly different purpose. When a user edits some content, their username is automatically entered into a "locked" field in that content record. So if I'm 'Bob' and I'm editing something, and you're 'Rob' and attempt to edit it, it won't let you do it. Once an edit is complete (SQL is submitted), the "locked" field is set to 'null'. But if I forget to complete the edit and walk away (and get logged out automatically), the 'locked' field will still say 'Bob'. But it'll let 'Rob' in as long as his attempted access is more than 15 minutes since 'Bob's last access. Which allows Bob to forget to log out and still lets others access the record.
Probably more than you needed 😉