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 wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

help really advanced application level variable structure stuff (you g 1

Status
Not open for further replies.

awesomebeats

Technical User
Nov 29, 2001
25
US
Alright, ya'll are going to love this one. I want to have an array that tells what users are logged in and out of my application at any given moment, so that I can eventually prevent multiple logins (yes I’m still on this same problem). I think from what I’ve been told I need to use an application level array (application.array), I’m guessing. Easy enough! When a user logs in I simple add a row to the array with their info and when the log out delete that row. I know how to do that much. Here though are my questions. Firstly, is there a way to have it where when a user closes their browser it automatically clears their row from the array? I have some code (posted below) I use to clear session variables when a user closes their browser. Could I link their row in the array with a session variables that gets cleared when the browser closes, so that their row clears? I don’t have to do it that way just a suggestion I don’t know how to follow up on. Anyway to get the user’s row of the array to clear when the exit their browser is needed. Also this I know is possible. If I have an array how can I query it? Like can I dynamically select the row where user x’s info is placed. That’s about it thank you for your help. I really need it!

awesomebeats

<cfif IsDefined(&quot;Cookie.CFID&quot;) AND IsDefined(&quot;Cookie.CFTOKEN&quot;)>
<cfset localcfid = Cookie.CFID>
<cfset localcftoken = Cookie.CFTOKEN>
<cfcookie name=&quot;CFID&quot; value=&quot;#localcfid#&quot;>
<cfcookie name=&quot;CFTOKEN&quot; value=&quot;#localcftoken#&quot;>
</cfif>
 
to retreive a list of all currently set session variables, you must loop through the session structure; the following code will give you an entire list of all session variables, including the built-in session variables:

<cflock timeout=&quot;30&quot; name=&quot;#Session.SessionID#&quot; type=&quot;readonly&quot;>
<cfloop collection=&quot;#Session#&quot; item=&quot;i&quot;>
<cfoutput>Key = #i# (Value = #Session#)<br></cfoutput>
</cfloop>
</cflock>

now a few words about session variables (from Mastering ColdFusion): because a session is tied to the CFIF and CFTOKEN cookies that are normally set on the client side, if these cookies no longer existed, then the user would have to begin a new session. If you want to ensure that these cookies expire when user closes their browser, then all you have to do is reset the cookies to their current values but with no expiration. If no EXPIRE attribute is set for a cookie, it automatically expires upon the close of the browser and and doesn't get written to the client side. By re-creating the CFID and CFTOKEN cookies, you are effectively overwriting the existing ones that are set to expire sometime in the future.

The following code can be used to reset the CFID and CFTOKEN cookies:

<cflock timeout=&quot;30&quot; name=&quot;#Session.SessionID#&quot; type=&quot;exclusive&quot;>
<cfcookie name=&quot;CFID&quot; value=&quot;#session.CFID#&quot;>
<cfcookie name=&quot;CFTOKEN&quot; value=&quot;#session.CFTOKEN#&quot;>
</cflock>

with this, the session variable is terminated and when you check the list of available session variables, you should get only users that are currently logged in AND have open browser. Sylvano
dsylvano@hotmail.com
 
Here's another thought. Try experimenting with the window.onunload() Javascript event handler. This handler is invoked when the user leaves a page. Thus, it is also invoked when the window is closed. My idea is to see if you can make the event handler invoke a ColdFusion template to modify your array.
 
As to how you can query an array. You can't do it directly. What you can do is loop through the array and check each element's data against your criterion. You might that it's appropriate to use a structure (associative array) instead of an array. With a structure you can &quot;query&quot; by specifiying the key. E.g., <cfif structfind(structure_name, key)> (to see if a specific key exists); or <cfif structure_name.key = &quot;string&quot;> (to see if a specific key has a specific value).
 
I agree with a440guy that a structure is a better fit to you problem than an array.

Is it essential that you &quot;log out&quot; a user when he closes his browser? Or could you do something with timeout instead? We use a technique where after a certain amount of time, the user has to log back in. It's usually 30 minutes but can be controlled by the specific page. A person doesn't log out so much as time out. In this example, we would store the log in time in the structure every time a new page in the application was visited. As each page is visited, the application would check the log in time, and if it exceeded the time allowed, the user would be required to log back in.

It's a different way of approaching the same problem. If it's vital for you to know whether a user was in or not, then add an exit page to the application, and use the exit page to remove the information from the login structure. While that won't catch everyone, it will catch most.

And if you need it to catch everyone, then don't let people log back in until they've been through the exit page ...

Sometimes it helps to look at the problem from a completely different point of view. I hope this is one of those cases.

Mike
 
ok thanx a lot guys so i should use structures rather than arrays alright i'v been reading on in in the cfdocs thanx a lot. and i'm play'n around with this window.onunload() the only thing is then i need frames but that's ok i can use an iframe or something.

awesomebeats
 
well now that i've read this structure stuff i could use some help. so if i want to make a structre called online. i do this.
<CFSET application.online=StructNew()>
well this is where i'm lost. i want to add to each row the name of the user and when they got online.
<CFSET value=StructInsert(appliaction.online, &quot;#username#&quot;, &quot;#time#&quot;)>
is that right is that how easy it is? then too query something like:
<CFLOOP COLLECTION=application.online ITEM=&quot;what goes here?&quot;></CFLOOP>
that's where i get lost what goes for item and then how do i pull the time based on the username. thank you.

awesombeats
 
ok i messed up in my last one i should name the key &quot;user&quot; right and then put the values at their id or is it so that there can be only one value per key. is a key like a column of a db with lots of values in it or is there only one value per key.

awesomebeats
 
You can have only one value per key, but that value can be any data structure, such as a list or another structure. So, for example, if you need the userid, the time, and the browser, you could do:
Code:
StructInsert(online, &quot;#username#&quot;, &quot;#time#,#browser#&quot;);
Of course, when you access the data, you have to remember that the data in the online structure is a list and handle accordingly:
Code:
usertime = ListGetAt(StructFind(online, #username#), 1);
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top