Detect duplicate user sessions via AJAX with jQuery + PHP
Beware this content is over 6 years old and considered stale. It may no longer be accurate and/or reflect the understanding of the author but remains here for reference only. Please keep this in mind as you use this content.
A new project (that @willvrny was working on) which incorporates a user logging process and a custom user area brought with it a few new challenges.
One particular one was the scenario that if a client which buys a certain number of logins to the service then begins to share that single login around it’s offices thereby allowing a whole office to use the service through a single login, instead of purchasing a unique login for each user.
Therefore, we needed a way to manage a users login session by keep track of the number of concurrent connections or logins, which would be one session per a login.
While the traditional/easy way is to store a users session in the database at login and then each time the page loads to match up the user stored session with the current users session and if they do not match redirect the user to the login page with an appropriate error message.
The problem with the service that we were providing is that it would be possible for multiple users to consume the service while remaining on the same page.
So one user could login, navigate to a resource and then allow another user to login and navigate to another resource they needed, so that both users would be consuming different resources on different sessions using the same login.
That’s where the AJAX magic comes in. The theory is that you should be able to poll the database for the current session at regular intervals, asynchronously, and detect whether or not the stored session and the current session match up. If they do, do nothing, if they don’t eject that user and kill their session.
The following was what we were able to come up with and I would welcome feedback or alternative solutions.
I assume you already have a working knowledge of PHP sessions, particularly how to start them, set session variables and retrieve/get session variables. You’ll need to know how to do this in order to store the automatically generated
session_id in the database after a successful authentication/login and to retrieve it for your scripts.
Things to note is that while I simply unset the PHP session variable (for demonstrative simplicity) using:
unset($_SESSION). You will probably want to utilise your own function to kill the session altogether.
I also have left a function
get_session_reference($user_id) in there that retrieves the stored
session_id from the database taking the users
id as an argument. There is also a custom function
get_user_id() that retrieves the current users
id which will be the current authenticated user.
While it becomes more obvious why the script is returning/setting HTTP status codes using the
The first few attempts used a straight
header() location redirect e.g.
header('Location: https://www.example.org/logged-out/') But it was discovered that the way the jQuery AJAX function works is by including the server-side output into the main HTML document from which it is getting called. This prevents a proper redirect because for the redirect to work it needs to occur before any output is sent to the screen.
This solution utilises the callbacks for the jQuery AJAX function that allow it to handle response codes, based on success or failure.
While I appreciate it may not confirm to strictly correct usage for the HTTP status codes, it allows us to send different error status codes depending on the outcome of the script. One outcome for those that send no session reference to the script and another for those that send an invalid session reference.
On the AJAX
error callback the XMLHttpRequest (XHR) object is used to retrieve the status code that we sent from the PHP script which compares the sent/current session with the one that is stored in the database for that user.
That way a different redirect is used depending on the status code.
The whole AJAX function is wrapped up as a function so that it can be repeatedly called at regular intervals using the
That’s pretty much it. Please shout if you need more clarification or if you have a better, more elegant solution to this problem.