2

I am creating an application where users cannot attempt multiple logins. If a particular user logs in, my database flag value becomes set to 1 that means no other member who has the same username and password can login.

If a user signs out my database flag value is set to 0. That's all working fine but now I have one different problem that is when a user suddenly closes the browser without logout page or session is timeout then automatically the database flag value should be set to 0. Now I don't know how to do it. Please give me any solution.

Darren
  • 68,902
  • 24
  • 138
  • 144
Saurabh Palatkar
  • 3,242
  • 9
  • 48
  • 107
  • 2
    It's almost always easier to implement "if they start a new session, all previous sessions are terminated" - it still achieves only one logged in session per user, but it's far easier to implement and users can automatically fix issues if their PC suddenly caught fire and lost network connectivity. – Damien_The_Unbeliever Sep 03 '13 at 12:10

2 Answers2

8

In your Global.asax file you have Session_OnEnd()

public void Session_OnEnd()
{
    // Set flag to 0
}

This will be okay if your sessionState is set to INPROC (Which is the default setting), if you have manually adjsuted it to StateServer or SQL Server then this event will be ignored.

When the browser closes the window you can do use onbeforeunload in JavaScript

window.onbeforeunload = function(e) {
  // call an Ajax function to reset the state.
};
Darren
  • 68,902
  • 24
  • 138
  • 144
  • Probably it's worth mentioning that `Session_OnEnd` will never be fired if you are using an out-of-proc session state (which you probably are if you are running in a WebFarm). – Darin Dimitrov Sep 03 '13 at 11:57
  • sir have store the UserID in session and Session_OnEnd() fired on session expired but to reset the flag i need the data which is in session so how should i get the those data to reset the flag – Rhushikesh Sep 24 '13 at 06:04
  • hello sir ur answer is very helpful to me but i dont know how to call the ajax function to reset the flag will u plz guide me – Rhushikesh Sep 24 '13 at 11:40
  • @Rhushikesh - hi, I believe this is Saurabn's question. If you need help you can post a new question (And link this question if you think it's related to your problem). Provide as much information as possible and what you've tried and someone will be able to help. – Darren Sep 24 '13 at 11:44
  • http://stackoverflow.com/questions/18980862/call-ajax-function-to-reset-the-flag-in-database – Rhushikesh Sep 24 '13 at 11:52
0

Simply create a column isloggedin in database

eg:

USERNAME | PASSWORD | isloggedin | logintimestamp

    user | pass     | 0          | 3/9/2013 23:59:59

and set it to 1 when user logs in and while theuser is logged in use ajax to update current time in db...

if this fails to do make timer functions to check the loggedin value depending on timestamp http://dev.mysql.com/doc/refman/5.1/en/events.html

here is the tutorial: http://www.sitepoint.com/how-to-create-mysql-events/

Bhushan Firake
  • 9,338
  • 5
  • 44
  • 79
  • And when would you set it back to 0? – Darin Dimitrov Sep 03 '13 at 12:01
  • And what happens if he closes the browser and never logs out? Did you read the original question? It's exactly what the OP is asking here? For example the user kills the browser in his task manager. What happens in your database with this value? It stays 1 for the eternity preventing this user from ever logging again? – Darin Dimitrov Sep 03 '13 at 12:18
  • OMG... really sorry apologies... Ajax can help... the page sends a probe with the username and the timestamp is recorded in extra column... wait i edit my answer –  Sep 03 '13 at 12:22
  • How would AJAX help if the user kills the browser in his task manager? AFAIK there's no such event that you could subscribe to. – Darin Dimitrov Sep 03 '13 at 12:24
  • ajax would only help while the user is logged in but after he kills the page, MySql Event Scheduler is going to help –  Sep 03 '13 at 12:28