Web Analytics Made Easy -
StatCounter Need a little help - CodingForum

Announcement

Collapse
No announcement yet.

Need a little help

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Need a little help

    Hi All,
    On my website I have always showed how many user were on my site. What I want to do now is to show the names of the users that are logged in. So everyone on the site could see each other. Kinda like a fourm where it show the list of users that are at the fourm. At first I thought that I could make a temp, table in my database so when someone logs in I could read the list from that table. But if they don't log out then it will show them as being logged in all the time. Then I thought that maybe I could use a application variable this way it could be seen by everyone across all the pages. So I have setup some test pages to see if I can get this to work.

    Here is how I setup my global.asa
    Code:
    <SCRIPT LANGUAGE="VBScript" RUNAT="Server">
    
    Sub Application_OnStart
    	' Set our user count to 0 when we start the server
    	Application("users") = 0
    	Application("namedUsers") = 0
    	Application("userNames") = ""
    End Sub
    
    Sub Session_OnStart
    	Session.Timeout = 5 ' minutes
    	Session("name") = ""
    	Application.Lock
    	Application("users") = Application("users") + 1
    	Application.Unlock
    End Sub
    
    Sub Session_OnEnd
    	' Decrease the active visitors count when the session ends.
    	Application.Lock
    	Application("users") = Application("users") - 1
    	If Len(Session("name")) > 0 Then
    		Application("namedUsers") = Application("namedUsers") - 1
    		Application.Contents.Remove("userNames")
    	End If
    	If Application("namedUsers") <= 0 Then
    		Application("namedUsers") = 0
    	End If
    	Application.UnLock
    End Sub
    </SCRIPT>
    Here is my login page
    Code:
    <%
    ' User wishes to log in
    If Request.QueryString("login").Count > 0 Then
    %>
    <html>
    <head>
    <title>Log in</title>
    </head>
    <body>
    <form action="<%=Request.ServerVariables("Script_Name")%>"
    	method="post">
    <p>Enter your name: 
    <input type="text" name="name">
    <input type="Submit" value="OK"></p>
    </form>
    </body>
    </html>
    <%
    Response.End
    End If
    ' User is logging in
    If Request.Form("name").Count > 0 Then
    	Session("name") = Request.Form("name")
    	Application.Lock
    	Application("namedUsers") = Application("namedUsers") + 1
    	Application("userNames") = Application("userNames") + "<br>" & Session("name")
    	Application.Unlock
    	Response.Redirect(Request.ServerVariables("Script_Name"))
    	Response.End
    End If
    %>
    Here is the display page
    Code:
    <%
    If Len(Session("name")) > 0 Then
    	Response.Write("Logged in as: " + Session("name"))
    Else
    	Response.Write("Not logged in. <a href=""")
    	Response.Write(Request.ServerVariables("Script_Name"))
    	Response.Write("?login=1"">Log in</a>")
    End If
    ' Display tally of users
    Response.Write("<br>" & Application("users") & " users online")
    Response.Write(" (" & Application("namedUsers") & " logged in)")
    Response.Write("<br><br>User names" & Application("userNames"))
    %>
    This is how it prints out:

    Logged in as: Frank
    4 users online (3 logged in)

    User names
    Tim
    Frank
    Jim


    Now this works but as the sessions ends I want it to remove only the name of the one that the session ended on. But with the Application.Contents.Remove("userNames") it remove all the names.
    So now I don't know if I should try to make an application for each user, and if I did this how would I know what user session ended to in order to remove the contents? Or should I be looking into using this with a text file?
    Any idea?
    I was wondering why frisbees got bigger as they got closer, then it hit me.

  • #2
    Your Application("usernames") contains one big string. Why dont you make it an array (or a dictionary), then you can iterate through it, deleting the username you want
    I am the luckiest man in the world

    Comment


    • #3
      Thanks Roelf,
      I did some google searchs on using applications for storing user names but didn't find very much.

      I will try to work it using an array in the application. As far as using a dictionary, I have never worked with that yet. But it maybe fun!

      Again Thanks for pointing me in the right area.
      I was wondering why frisbees got bigger as they got closer, then it hit me.

      Comment


      • #4
        Hi Roelf,
        I have been doing some research on adding and removing items in an array. And even with the redim and preserve I don't think it will work right. I think that it maybe simpler to write the name to a text file then on session end remove that users name from the text file. But it would have been nice to use an array in an application vairable, but only if there was a straight forward way of adding and removing values in an array. If you know of a link that has more infor on adding and removing values in an array please let me know.

        After I finish this, I will post the code, hoping you guys will look it over to make sure I not taking a longer route in doing it then need be.
        I was wondering why frisbees got bigger as they got closer, then it hit me.

        Comment


        • #5
          That is why the Dictionary is such an handy object

          Add items using the Add method (check if exists before with Exists method) delete using the Remove method.

          All you need is the keys, you can store empty items, using the usernames as the itemkeys
          I am the luckiest man in the world

          Comment

          Working...
          X