How to delete a session value from table when we close a browser
I am a beginner in PHP. I got a problem related to session, I created a o开发者_如何学Gone table with username, password fields and one temp_session table with sessionID. When I login with given username a session is generated and session will store in temp_session table and when i logout by pressing logout button session value is deleted from tem_session table.
Now, I have question that what would I do when I close browser, in this case session get expired but session value is still in temp_session table I want to remove that value after 2 minute from temp_session after browser close. what logic I will use for this.
you can't clear database value on event of closing browser but you need to implement logical solution.
you need to add time stamp in session table as last activity time, set last activity = current time if user's last activity time and current time difference is not more then 2 minute else remove record from database.
Let's discuss cases.
when you login you will add session in DB. if user is idle for more than 2 min than we will delete session from DB. if user id not idle more then 2 min than last activity will set as current time.
Here is a suggestion instead of invalidating the sessionId after two minutes why dont you reset the value when the user logs back into your application. I am not a PHP expert but I am guessing you should be able to use session_start handler for the same.
If you still wanna go ahead and do this then you need to setup a timestamp in the users table that will be updated everytime the server receives a request and then you probably would want to have a schedule running that updates your table based on the timestamp.. Do you see the unnecessary overhead you are introducing.
In java we can handle it through HTTPSessionListner and adding an entry in web.xml file . This will be called when the session is not active and session.invalidate() method we can call post after operations we want to do.
So, You can try implementing same in PHP or findout something related to that :)
精彩评论