well, if you have a way of detecting when the user closes the browser window/leaves the page, you can forcfully destroy the session with session_destroy.

if you just want to know how to safely overwrite php's session functions, check session_set_save_handler.
There also some ready-to-use tools out there which hook onto php sessions functions and save the sessions data in databases.

the good think with session_set_save_handler is, that you can just go on and use the normal session functions and the $_SESSION array.