Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Determine multiple connections to a site.

Status
Not open for further replies.

EdRev

Programmer
Aug 29, 2000
510
0
0
US
Is there a way I can determine if a user already has a site open?
For example, a user is already logged on to a site and they try to create another connection to the site.
 
you can use a session variabled called (for example) session("blnLoggedIn")...

When the user visits the site, check the session variable if it is true, they are already logged in. If it is false, they are not logged in and you can set it to true.

That will resolve it if they are on the same machine and browser.

If you want to expand it, and go to muliple machines, set a flag + timestamp in a db when they log in and check that everytime. The problem with this method is that the Session_onend event doesn't always work so you'll need to rely on them to actually click a log off button and you can reset the flag.

To get around this (the best I know how)...

Everytime they click a link, re-set the timestamp. Create a background job that re-sets all log in flags to 0 (not logged in) for any timestamps that are more than 20 minutes old (or whatever length you want your session to be active).

So the process will be:

1. User attemps login
2. Script checks to see if that user is already logged in by checking the flag
3. If it flag is set to 1, the user cannot log in again.
4. If the flag is set to 0, the user can log in.
5. If the flag is set to 1, but the other login has just closed their browser, the login should work after a maximum of 20 minutes.

In order to correct it for the current user, use a combination of cookies/db entries and always check to see if the cookie exists.

That may not be the best explanation, hopefully you'll be able to make some sense out of it...perhaps another member of this forum can come up with a different method or explain what I am trying to do in easier to understand terms.

Good luck.



TIP: trying googling the answer before posting, you'll find that more times than not someone else somewhere has had the same request and posted an answer online.
----
I have recently been semi-converted to ensuring all my code (well most of it) works in both javascript and non-javascript enabled browsers
 
vic is on the right track the problem is now tabbed browsers and if the user opens another window in a seperate process.

along the lines he was talking about , you'll need a session value, a cookie, and an application level variable, this gets a little messy to properly track because people just close windows and makes it hard to detect.

the cookie is to syncronise session values, handles the seperate processes, but since seperate processes or individual visits can take minutes to weeks to generate use an application value to store the list of connected users, and double check against it.. basically it's how old school load balancing had to be dealt with before all the server syncronization came into place

[thumbsup2]DreX
aKa - Robert
if all else fails, light it on fire and do the happy dance!
" I always think outside the 'box', because I'm never in the 'loop' " - DreX 2005
 
Another method (you'll need to assume or ensure Javascript is enabled)

You'll need an onLoad and onUnload event in your body tag and a few session vars:

1. Track the user id
2. Track the log in status

Onload = setLogInStatus(1,<%=session("blnLoggedIn")%>,<%=session("intUserId")%>)
Onunload = setLogInStatus(0,<%=session("blnLoggedIn")%>,<%=session("intUserId")%>)


Onload
If the loggedIn session var is true:
Update the record in the db and set the log_in_status to 1 + add a timestamp
If it is false:
Jump out of the script

onUnload
If the loggedIn session var is true:
Update the record in the db and set the log_in_status to 0 (no need for a timestamp)
If it is false:
Jump out of the script

When your user tries to log in, you'll need the following steps in your ASP code:

Check to see if the user is already logged in (if the log_in_status flag is 1):

If the flag is set to 1 and the timestamp is less than 20 minutes old then it means this user is already logged in.
Otherwise
The user user is either not logged in, or has been inactive for more than 20 minutes, and the original session will expire

This (in theory) should resolve some issues:

1. If the user simply closes their browser, the onUnLoad event will set their status to 0 - which means they are logged out
2. If the user clicks a log out button the same function can be called.

It will generate A LOT of calls to the db, twice for every page, but the calls will be quick so it shouldn't (again in theory) cause too much of an overload.

The only time I can see this over lapping is if a second user login is attempted at the exact same time the current user session clicks a link to go to another page.


^ does that make any sense? It does to me, but often I write the way I think and my brain is sometimes working in another realm.





TIP: trying googling the answer before posting, you'll find that more times than not someone else somewhere has had the same request and posted an answer online.
----
I have recently been semi-converted to ensuring all my code (well most of it) works in both javascript and non-javascript enabled browsers
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top