⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 functions_session_data.asp

📁 简单的asp论坛源码系统,很适用于初学者!界面简洁,功能齐全
💻 ASP
📖 第 1 页 / 共 2 页
字号:
		strSQL = "DELETE FROM " & strDbTable & "Session" & strRowLock & " " & _
		"WHERE " & strDbTable & "Session.Last_active < " 
		'If Access use # around dates, other DB's use ' around dates
		If strDatabaseType = "Access" Then
			strSQL = strSQL & "#" & internationalDateTime(DateAdd("n", -20, Now())) & "#;"
		ElseIf strDatabaseType = "SQLServer" Then
			strDate = internationalDateTime(DateAdd("n", -20, Now()))
			strDate = Replace(strDate, "-", "", 1, -1, 1)
			strSQL = strSQL & "'" & strDate & "';"
		Else
			strSQL = strSQL & "'" & internationalDateTime(DateAdd("n", -20, Now())) & "';"
		End If

		'Set error trapping
		On Error Resume Next

		'Execute SQL
		adoCon.Execute(strSQL)
			
		'If an error has occurred write an error to the page
		If Err.Number <> 0 Then	Call errorMsg("An error has occurred while writing to the database.", "getSessionData()_delete_session_data", "functions_session_data.asp")
		
		'Disable error trapping
		On Error goto 0 
	End If





	'If the user doesn't have a session create one
	If blnFoundSession = false Then

		'ReDimesion the array
		ReDim Preserve sarySessionData(3, UBound(sarySessionData, 2) + 1)

		'Update the new array position with the new session details
		sarySessionData(0, UBound(sarySessionData, 2)) = strNewSessionID
		sarySessionData(1, UBound(sarySessionData, 2)) = strIP
		sarySessionData(2, UBound(sarySessionData, 2)) = internationalDateTime(Now())
		sarySessionData(3, UBound(sarySessionData, 2)) = ";"

		'Initilise the session id variable
		strSessionID = strNewSessionID

		'Create a cookie and querystring with the session ID
		Call setCookie("sID", "SID", strNewSessionID, False)
		
		
		'If using a database for session data then save the new session to the database
		If blnDatabaseHeldSessions Then
		
			'SQL to update the database with the new session
		 	strSQL = "INSERT INTO " & strDbTable & "Session (" &_
			"Session_ID, " & _
			"IP_address, " & _
			"Last_active, " & _
			"Session_data " & _
			") " & _
			"VALUES " & _
			"('" & strNewSessionID & "', " & _
			"'" & strIP & "', " & _
			strDatabaseDateFunction & ", " & _
			"';' " & _
			");"
			
			'Set error trapping
			On Error Resume Next

			'Write to database
			adoCon.Execute(strSQL)
			
			'If an error has occurred write an error to the page
			If Err.Number <> 0 Then	Call errorMsg("An error has occurred while writing to the database.", "getSessionData()_save_new_session_data", "functions_session_data.asp")
		
			'Disable error trapping
			On Error goto 0
		
		End If
	End If




	'If cookies are not detected setup to use querystrings to pass around the session ID
	'For better Search Engine indexing don't use Session Querystring if detected as Search Robot
	If blnCookiesDetected = false Then
		strQsSID = strSessionID			'For form entries etc.
		strQsSID1 = "?SID=" & strSessionID	'For ? querystrings
		strQsSID2 = "&amp;SID=" & strSessionID	'For &amp; querystrings
		strQsSID3 = "&SID=" & strSessionID	'For & querystrings - redirects
	End If





	'Update the session application array (if storing sessions in application level array)
	If blnDatabaseHeldSessions = false AND NOT OSType = "Search Robot" Then
		
		'Lock the application so that no other user can try and update the application level variable at the same time
		Application.Lock
	
		'Update the application level variable
		Application(strAppPrefix & "sarySessionData") = sarySessionData
	
		'Unlock the application
		Application.UnLock
	End If
End Sub







'******************************************
'***  Get application session data	***
'******************************************

'Function to read in application session data for those without cookies
Private Function getSessionItem(ByVal strSessionKey)

	Dim saryUserSessionData
	Dim intSessionArrayPass
	
	'Append '=' to the end of the session key to make full session key (eg. key=)
	strSessionKey = strSessionKey & "="

	'Split the session data up into an array
	saryUserSessionData = Split(strSessionData, ";")

	'Loop through array to get the required data
	For intSessionArrayPass = 0 to UBound(saryUserSessionData)
		If InStr(saryUserSessionData(intSessionArrayPass), strSessionKey) Then
			'Return the data item
			getSessionItem = Replace(saryUserSessionData(intSessionArrayPass), strSessionKey, "", 1, -1, 1)
		End If
	Next

End Function







'******************************************
'***  Save application session data	***
'******************************************

'Sub procedure to save application session data for those without cookies
Private Sub saveSessionItem(ByRef strSessionKey, ByRef strSessionKeyValue)

	Dim saryUserSessionData
	Dim intSessionArrayPass
	Dim strNewSessionData
	Dim intItemArrayPass

	'Don't run code if a search engine spider
	If NOT OSType = "Search Robot" Then
		
		'Read in the application session for the user and update the session data in it
		For intSessionArrayPass = 0 To UBound(sarySessionData, 2)
	
			'If we find the users session data update it
			If sarySessionData(0, intSessionArrayPass) = strSessionID Then
	
				'Split the session data up into an array
				saryUserSessionData = Split(sarySessionData(3, intSessionArrayPass), ";")
	
				'Loop through array and do NOT add the updated key to session data
				For intItemArrayPass = 0 to UBound(saryUserSessionData)
	
					If InStr(saryUserSessionData(intItemArrayPass), strSessionKey) = 0 AND saryUserSessionData(intItemArrayPass) <> "" Then
	
						'Create session data string
						strNewSessionData = strNewSessionData & saryUserSessionData(intItemArrayPass) & ";"
					End If
				Next
	
				'Add the updated or new key to session string
				If strSessionKeyValue <> "" Then strNewSessionData = strNewSessionData & strSessionKey & "=" & strSessionKeyValue
	
				'Update the array
				sarySessionData(3, intSessionArrayPass) = ";" & strNewSessionData
				
				
				'If using a database save the session data to database
				If blnDatabaseHeldSessions AND NOT OSType = "Search Robot" Then
					
					'Make sure session data is SQL safe to prevent SQL injections
					strNewSessionData = ";" & formatSQLInput(strNewSessionData)

					'Initilse sql statement
				 	strSQL = "UPDATE " & strDbTable & "Session" & strRowLock & " " & _
					"SET " & strDbTable & "Session.Last_active = " & strDatabaseDateFunction & ", " & strDbTable & "Session.Session_data = '" & strNewSessionData & "' " & _
					"WHERE " & strDbTable & "Session.Session_ID = '" & strSessionID & "';"
					
					'Set error trapping
					On Error Resume Next
		
					'Write to database
					adoCon.Execute(strSQL)
					
					'If an error has occurred write an error to the page
					If Err.Number <> 0 Then	Call errorMsg("An error has occurred while writing to the database.", "saveSessionItem()_update_session_data", "functions_session_data.asp")
				
					'Disable error trapping
					On Error goto 0
					
				
				'Else save the sesison data to the application array
				Else
	
					'Lock the application so that no other user can try and update the application level variable at the same time
					Application.Lock
		
					'Update the application level session data for the user
					Application(strAppPrefix & "sarySessionData") = sarySessionData
		
					'Unlock the application
					Application.UnLock
				End If
	
				'Exit for loop
				Exit For
			End If
		Next
	End If
End Sub


%>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -