<SCRIPT LANGUAGE="VBScript" RUNAT="Server">

Sub Application_OnStart

'*****************************************************************************
' DateBase
'*****************************************************************************
	Application("url") = "127.0.0.1,3442"
	'Application("DB") = "sms_nninc_201303_tmp"
	Application("DB") = "sms_nninc_201303"
	Application("DB_id") = "dbuser_sms"
	Application("DB_pwd") = "sksnaAnswk!@08%"

'*****************************************************************************
' uploader
'*****************************************************************************

	Application("nn_key") = "gt3E7pFzQLsb9+kqYldtGpLHjLZknptQjFgAcUXYCNwCgSlWXtXkPOIbmONi7swy"
'*****************************************************************************





End Sub


Sub Application_OnEnd
	

End Sub



Sub Session_OnStart


	dim theTable
	theTable = "visit_COUNTER"

    Application.Lock
        Application("nowCount") = Application("nowCount") + 1
    Application.UnLock
   
	dim strConnect, dbcon
    
	

   strConnect="Provider=SQLOLEDB.1;Data Source="&Application("url")&";Initial catalog="&Application("DB")&";user ID="&Application("DB_id")&";Password="&Application("DB_pwd")
    Set DbCon=Server.CreateObject("ADODB.Connection")
    DbCon.Open strConnect



	dim temp    
    temp = request.ServerVariables("HTTP_USER_AGENT")
    temp = split(temp,";")

	dim vBrowser, vOS
    if ubound(temp) > 1 then
		vBrowser = temp(1)
		vOS = temp(2)
    vOS = replace(vOS,")","")

		vBrowser = trim(vBrowser)
		vOS = trim(vOS)
		vBrowser = replace(vBrowser,"'","''")
		vOS = replace(vOS,"'","''")
    end if

	dim vURL, vIP, vDomain, vDW
    vURL = request.ServerVariables("HTTP_REFERER")
	'vIP = request.ServerVariables("REMOTE_ADDR")
	vIP = remoteIP()
	vDomain = request.ServerVariables("SCRIPT_NAME") & "?" & request.ServerVariables("QUERY_STRING")
	vDW = weekday(date)

	dim vSeason
	select case month(date)
	case 3,4,5 vSeason = 1
	case 6,7,8 vSeason = 2
	case 9,10,11 vSeason = 3
	case 12,1,2 vSeason = 4
	end select

	dim sql
	sql = "insert into "&theTable&"(vIP,vYY,vMM,vDD,vHH,vMT,vDW,vSeason,vBrowser,vOS,vURL,vDomain) "
	sql = sql & "values('"&vIP&"',"&year(date)&","&month(date)&","&day(date)&","&hour(now)&","&minute(now)&","&vDW&","&vSeason&",'"&vBrowser&"','"&vOS&"','"&vURL&"','"&vDomain&"')"
	dbcon.execute sql
	dbcon.close()
	set db = nothing

End Sub



Sub Session_OnEnd


'  strConnect="Provider=SQLOLEDB.1;Data Source=(local);Initial catalog=nowhome;user ID=sa;Password=aspjwcjd"
'  Set DbCon=Server.CreateObject("ADODB.Connection")
'  DbCon.Open strConnect
'	SQL1 = "Update UserLog Set LogCheck = 0, LogEndDate = getdate() Where L_ID = 'jw'"
'	Dbcon.Execute(SQL1)

  Application.Lock 

	Application("nowCount")=Application("nowCount") - 1

  Application.UnLock

End Sub


Function remoteIP()
    Dim clientIP
    
    ' 1. X-Forwarded-For 헤더 확인 (프록시 환경에서 가장 우선)
    clientIP = Request.ServerVariables("HTTP_X_FORWARDED_FOR")
    
    If clientIP <> "" Then
        ' 여러 프록시를 거친 경우 쉼표로 구분되므로 첫 번째 IP 추출
        If InStr(clientIP, ",") > 0 Then
            clientIP = Trim(Split(clientIP, ",")(0))
        End If
        remoteIP = clientIP
        Exit Function
    End If
    
    ' 2. X-Real-IP 헤더 확인 (Nginx 등에서 사용)
    clientIP = Request.ServerVariables("HTTP_X_REAL_IP")
    If clientIP <> "" Then
        remoteIP = clientIP
        Exit Function
    End If
    
    ' 3. CF-Connecting-IP 헤더 확인 (Cloudflare 사용시)
    clientIP = Request.ServerVariables("HTTP_CF_CONNECTING_IP")
    If clientIP <> "" Then
        remoteIP = clientIP
        Exit Function
    End If
    
    ' 4. HTTP_CLIENT_IP 확인
    clientIP = Request.ServerVariables("HTTP_CLIENT_IP")
    If clientIP <> "" Then
        remoteIP = clientIP
        Exit Function
    End If
    
    ' 5. 기본 REMOTE_ADDR 사용
    clientIP = Request.ServerVariables("REMOTE_ADDR")
    remoteIP = clientIP
End Function

</SCRIPT>