tags:

views:

731

answers:

4

I want to prevent multiple log in in php application.

Firstly, I create login status(active, notactive) in user table.

when user A login the user status will be set to 'active', and if the user logout the status will set to 'notactive'. when another client try login using the same user acount, I check the user table. if the user still active the error login will be sent to the user.

but the problem occurred, if the user close the browser. the status in user table can bu update because the user didnt click action logout.

Do you have any suggestion about this ?

A: 

What you should do is check on whether they have been active the last several minutes when trying to login. This could be done with a lastonline stamp and should be set on every page request in the user table.

If not done with javascript you could check, when logging on, if the user was active the last 15 minutes. If not you can login as the new user.

You could also do it with javascript. Make an ajax call that fires every minute or so.

<script>
setInterval(function() {
  // do the ajax call
}, 60000);
</script>

Let this call go to a script that will edit the lastonline stamp in the user db. When trying to login you check the user db if the lastonline stamp has exceeded the minute and you have your check if you may login. This will help when you are on the page but you are not active the last 15 minutes and you do not want somebody else to login.

Robert Cabri
why is this not helpfull? I don't get it
Robert Cabri
I wasn't the downvoter, but having client-side JavaScript is by no means a guarantor that visitors will be logged out after 60 seconds. Also consider what happens if the user simply closes the page.
Paul Lammertsma
I will a little bit help full.but, How if the user terminate the browser ???
adisembiring
this method works if you are on the page. if the page closes the ajax call is destroyed. after a minute another user can login somewhere else.
Robert Cabri
+1  A: 

You could change your model so that only the most recent user can be logged in.

If you record the most recent session id seen for each user, when they log in a second time you can trash any currently existing session, effectively logging them out.

For a normal user, things appear to "just work". If you're wanting to prevent "abnormal" users from distributing their login credentials, this should serve as a disincentive.

Paul Dixon
Honestly I didnt catch your answer yet. And I will give you more specific question :D . Person_A try to login using User_A login, the user status will be set to activeand latter, Person_B want to try login to the webiste using User A. person_B can not be log on to the web, because User_A still active.`When an account still active, another people cant login to the website by using the same user`and, the problem occurred. If the user terminate the browser. browser cant be send data to server to perform logout action.So, the status will always active although the user not active anymore.
adisembiring
Well, what I'm saying is that when user_B logs in, user_A is automatically logged out. There may be a good reason why you want to prevent user_B's login, but you haven't made it clear why this is preferable. My suggestion gets around the problem of waiting for a session timeout to allow re-login, while still preventing a user from logging in twice.
Paul Dixon
A: 

Using client side javascript in order to track logged in user is unreliable.

You can get the same result by simply creating a lastlogindate field in the db, and updating it with the last login timestamp of the user.

At every login attempt, if now()-$lastlogindate > predefined_timeout, then you should accept the new login, otherwise refuse it.

Anonymous
+1  A: 

Instead of storing whether the user is active\inactive, it is better to store some attribute which can be checked against the user on a per-action basis; as in, every time the user tries to do something which requires authentication, it will check to see that this attribute matches before it proceeds.

I recommend you do the following;

First, create a hash to uniquely identify the user whenever they log in. I'd imagine that a sha1 of time() would be enough to avoid collisions. Whatever you chose, make sure that it is varied enough so that another user logging in will have a incredibly low chance of receiving the same hash (for example, don't hash the IP address or browser's user-agent, as these are not varied enough).

Second; store this hash in your database and in the user's session at the time of log in. Doing so will effectively 'log out' the previous user, as the hash should be different each time someone logs in.

Since we're using sessions, a cookie should be automatically placed in the user's browser which will contain a unique ID that identifies the user to his or her session data. The contents of the cookie are not really of concern.

Next, create a function called authenticateUser() or similar, which will be called at the start of every script to ensure the user is authenticated. This script should query the database, checking to see whether a user with your user's ID has a hash that matches your user's hash.

For example

function authenticateUser($id, $hash, $databaseLink) {
    # SQL
    $sql = 'SELECT EXISTS(
               SELECT 1
               FROM `tbl_users`
               WHERE `id` = \''.mysql_real_escape_string($id).'\'
               AND `has`h = \''.mysql_real_escape_string($hash).'\'
               LIMIT 1
           );';

    # Run Query
    if ($query = mysql_query($sql, $databaseLink)) {
        # Get the first row of the results
        # Assuming 'id' is your primary key, there
        # should only ever be one row anyway.       
        $result = mysql_fetch_row($query);

        # Casting to boolean isn't strictly necessary here
        # its included to indicate the mysql result should
        # only ever been 1 or 0.
        return (bool)($result[0]);
    } else {
        # Query error :(
        return false;
    }
}

Then we simply pass authenticateUser() the user's ID, hash (per your session data) and a database link (for a database connection you will have to have opened earlier).

If authenticateUser() returns true, the user is authenticated. If false, the user is not OR the database is unavailable or there is an SQL error.

Please note however that this will increase your server load as a database request is sent once per page request. It is probably not all that wise to do this on giant projects where thousands of people are logging in at any given time. I'm sure someone can suggest improvements.

Also, waiting for the cookie to expire is not the best way to force people who have been inactive to log out, as you should never trust cookies. Instead, you can add in an column called last_active which you can update every time the user is authenticated. This will also increase server load, but will allow you to manually override stale log-ins by removing the hash for users who were, say, inactive for 3 hours.

Lachlan McDonald