login is track though login screen
logout is track by 3 way
1. session expired and sign off click which is done by user
2. browser close.
So you have maintain a table with fields userid and Status. You have to maintain the Status 'A' for Activate and 'D' for Deactivate.
In Button Event,
1.check the userid status ,
If(Status!='A')
then update the table "Status into 'A'.
Afterwards only redirect into main page.
2.If Logout, Again Change the Status update into 'D'.
Here u will face lot of problems.For Help Visit try below links.
Help 1
Help 2