webForumDet fria alternativet

Namnet endast en gång

ASP

6 svar · 230 visningar · startad av ampy

Medlem sedan feb. 20011 498 inlägg
Frågan#1

Har denna kod för att skriva ut inloggade användare:

   	 aSessions = dOnlineUsers.Keys
    	for iUser = 0 to dOnlineUsers.Count - 1
        sKey = aSessions(iUser)
        sUserInfo = dOnlineUsers.Item(sKey)
        aUserInfo = split(sUserInfo, "<|>")
        
        sUserName = aUserInfo(0)
        sLastActionTime = aUserInfo(1)
        sLastPageViewed = aUserInfo(2)
        
        if sUserInfo <> "" AND not sUserName = "Gäst" then
 
	
	

	sql = "select id, idkort, ar, manad, dag, stad, kon from members where username = '" & sUserName & "'"
	set recset = connect.execute(sql)
	userid =  recset(0)
	idkort = recset(1)
	ar = recset(2)
	manad = recset(3)
	dag = recset(4)
	stad = recset(5)
	kon = recset(6)
	set recset = nothing

	username = sUserName

					
	If idkort = "polis" Then
	Response.Write "<img src=""bilder/idpolis.gif"">" 

	ElseIf idkort = "pojke" Then
	Response.Write "<img src=""bilder/idpojke.gif"">"

	Else
	Response.Write "<img src=""bilder/idflicka.gif"">"

	End If
			

			
	Response.Write " <a title='" & username & " bor i " & stad & " och är " 

	dteDOB = ar & "-" & manad & "-" & dag
	intAge = convAge(dteDOB)
	Response.Write intAge & " år"

	Response.Write "' href='krypin.asp?id=" & userid & "' target='_top'>" 					
					

	If Len(username) > 11 then
	strusername = Left(username,11)
	response.write ""&strusername&"."
	else
	response.write username
	End If
					
	Response.Write "</a> </font>" & "<br>"
			

	If kon = "pojke" then
	pojke = pojke + 1
	ElseIf kon = "flicka" then
	flicka = flicka + 1
	end if

	
	

	end if
	next

Men ibland skrivs ett användarnamn ut två gånger, vet inte varför, men då vill jag alltså lägga till en kod i koden här ovanför som gör att ett användarnamn endast skrivs ut en gång, hur kan man fixa det på bästa sätt?

Medlem sedan dec. 200012 464 inlägg
#2

Då är det bättre att se till att du inte lagrar duplikat i dOnlineUsers.

Medlem sedan feb. 20011 498 inlägg
#3

LarsG skrev:

Då är det bättre att se till att du inte lagrar duplikat i dOnlineUsers.

duplikat?

Medlem sedan feb. 200112 078 inlägg
#4

Engelska: duplicate
Svenska: duplikat

duplicat|e a dubbel, tvåfaldig; dubblet, orddubblett, synonym. osv.

Medlem sedan feb. 20011 498 inlägg
#5

Jaha, nu fattar jag vad LarsG menar. Men jag vet inte vad det är som orsakar det.

Medlem sedan feb. 200112 078 inlägg
#6

Eftersom du troligtvis har dubletter i databasen, blir det automatiskt så.

Medlem sedan feb. 20011 498 inlägg
#7

Jag använder inte databasen i detta fall. Utan applications och sessions. Så här ser koden ut som lägger till användare:

<Object Runat="Server" Scope="application" ID="dOnlineUsers" ProgID="Scripting.Dictionary"></Object>

<SCRIPT LANGUAGE=VBScript RUNAT=Server>

	sub Application_OnStart

    		application("ServerStart") = now

	end sub

		function Decrypt(sText)
    		'Replace with your own decrypt ;)
    		Decrypt = sText
		end function    

		function adZero(sText)
    		'Adds zeros to the beginning
    		if isNull(sText) then exit function
    		adZero = string(5 - len(sText), "0") & sText
		end function

	Sub Session_OnStart

    		'Session_OnStart is automatically called by the ASP Interpreter
    		'when a new session starts. (Probably a new visitor)

    		'TimeOut value determines the period in minutes when 
    		'the session is assumed to be abandoned

    		Session.TimeOut = 20

    		'Get the current active users list
    		sActiveUsersList = application("ActiveUsersList")
    
    		'Get the new user name from the cookie
    		sNewUserName = "Gäst"
    
    		'If the visitor doesn't have a registered username, assign Guest(n) label
    		if sNewUserName = "" then sNewUserName = "Guest-" & AdZero(CInt(application("Visitors")))
    
    		'Initial action time
    		sLastActionTime = Time
    
    		'User info consists of user name, last action time, and the page viewed
    		sUserInfo = sNewUserName & "<|>" & sLastActionTime & "<|>" & sLastPageViewed

    		'Add this user to our collection with SessionID being the key
    		'(See the top of this file to see how dOnlineUsers object is initiated)
    		dOnlineUsers.Add Session.SessionID, sUserInfo
    
    		'Lock the application variable and update the contents
    		application.lock
    		'The number of active users
    		application("ActiveUsers") = application("ActiveUsers") + 1
    		'The number of visitors since last application reset
    		application("Visitors") = application("Visitors") + 1
    
    		'If the date is different than the previously stored date,
    		'it means we passed midnight, so reset our "today" counters
    		if application("TodaysDate") <> Date() then
        	application("PageViewsToday") = 0
        	application("VisitorsToday") = 0
    		end if
    		application("VisitorsToday") = application("VisitorsToday") + 1
    		'Store the date
    		application("TodaysDate") = Date()
    		'Unlock and go
    		application.unlock
    
	End Sub

	Sub Session_OnEnd
    	'Session_OnEnd is automatically called by the ASP Interpreter
    	'when the specified TimeOut period has passed after user's last action

    	on error resume next

    	'Remove this session from our collection
    	dOnlineUsers.Remove Session.SessionID
    
    	'And update the application variables
    	application.lock
    	application("ActiveUsers") = application("ActiveUsers") - 1
    	application.unlock

End Sub
</SCRIPT>

Detta är alltså global.asa-filen.

254 ms totalt · 4 externa anrop · v20260731065814-full.86ec41c2
123 ms — deklarationer (db)
0 ms — hämta statistik (cache)
128 ms — hämta tråd, inlägg och bilagor (db)
123 ms — ändringar (db)