using session variables is the easiest way for the first part but adding the extra information may require another database table
TABLE tbl_online
user | active | ip | referer

In the login.php page add a line to insert a record containing the userid, a 1,the ip, http_referer
in the logoff page update the table active from 1 to a 0 (inactive)

use select * from tbl_online where active <>0;