Okay for your problm there are 2 cases,
the first is straight forward, if the user is polite enough to click the logoff button, you can call a phpscript to enter log and kill the session,
Under the second case just as the previous user has mentioned call a javascript function onUnLoad which can pop up a small window which runs the script for hasty close, where you will enter the log but not necessary to kill anything as the session automatically ends. You might choose to close this box yourself or give user the option to close this box.