1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

TUTORIAL: ServerVariables of ASP

Discussion in 'C#' started by ludwig, Jul 13, 2008.

  1. #1
    • How to find the physical path of virtual site
    • Display Server variable
    • IP Address of the remote user
    • Browser Type of the user
    • Show Message depending on User language
    • Make a delay before or while loading
    • How to keep track of Visitors
    • How to redirect the visitor
    • How to pass variables from Page to Page
    • How to force a User to enter password
    • How to display the number of users currently online

    Physical Path
    The physical path of this virtual web site is <%= Server.MapPath("\")%>
    Outputs : The physical path of this virtual web site is C:\Inetpub\wwwroot

    Server Variables
    <% For Each Item In Request.ServerVariables %>
    <% = Item %> = <% = Request.ServerVariables(Item)%>
    <% Next %> 
    HTML:
    IP Address
    My IP address is <%=Request.ServerVariables("REMOTE_ADDR")%>
    Outputs : My IP address is 173.34.34.6

    Browser type of User
    The Browser type is <%=Request.ServerVariables("HTTP_USER_AGENT")%>
    Outputs : The Browser type is <%=Request.ServerVariables("HTTP_USER_AGENT")%>

    Show Message depending on User language
    <% language = Request.ServerVariables("HTTP_ACCEPT_LANGUAGE")
    If language = "en" Then %>"
    <% Else %>
    <% End If %>
    
    HTML:
    Page Delay
    2 second delay before the rest of the code on the page is executed.
    <%
    ' Define a small function
    Sub TLdelaySec(DelaySeconds)
    SecCount = 0
    Sec2 = 0
    While SecCount 
    Sec1 = Second(Time())
    If Sec1  Sec2 Then
    Sec2 = Second(Time())
    SecCount = SecCount + 1
    End If
    Wend
    End Sub
    %>
    HTML:
    Call the above function as follows to make a 2 second delay
    <% TLdelaySec(2) %>

    How to log visitor Visits
    1. Create an ascii file with the filename as current systemtime
    2. Get the visitors IP Address
    3. Check if the referring page exists

    <%
    ' Lets get the date right so we can name the log file
    LogFileDate = Date
    LogFileName = ""
    	 If Month(LogFileDate) 
    LogFileName = "0"
    End If
    	 LogFileName = LogFileName & Month(LogFileDate)
    	 If Day(LogFileDate) 
    LogFileName = LogFileName & "0"
    End If
    
    
    ' Now place it in the proper directory and deal with it
    LogFileName = "/tracking/" & LogFileName & Day(LogFileDate) & Year(LogFileDate) & ".log"
    Set fs = CreateObject("Scripting.FileSystemObject")
    ' Make sure we can APPEND to the file - thats what the 8 is for
    Set a = fs.OpenTextFile(server.mappath(LogFileName), 8, True, False)
    	' If there is NO REFERER Then Skip this mess
    if Request.ServerVariables("HTTP_REFERER") 
    Request.ServerVariables("HTTP_REFERER") = 0
    Else
    ' If there is a Referer then write the information to the log
    a.Wriste("Referer: " & Request.ServerVariables("HTTP_REFERER"))
    a.Write(" IP Address: " & Request.ServerVariables("REMOTE_ADDR")) & VBcrlf
    End if
    ' Now its recorded adn ready for the next visitor
    %>
    
    HTML:
    How to redirec visitor
    Using the Redirect method to redirect the browser to another URL.
    Firstly of all turn on buffering.
    Note : This statement must appear before the tag.
    <% Response.Buffer = True %>
    Then you can redirect the browser by doing this.
    <% Response.Redirect "somepage.asp" %>

    How to pass variable from page to page
    <% For each item in Request.Form %>
    " Type = "Hidden" VALUE = "<% = Server.HTMLEncode(Request.Form(item)) %>">
    <% next %>
    HTML:
    How to force password request
    <%
    Response.Status="401 Not Authorized"
    Response.End
    %>


    In brief, by placing this code at the Top of the page, you will force the dialog box to appear.
    If the proper information is not entered, they will not be allowed to view the document.
    Capture the User Name that was entered by using the following line of code in your document:
    <%=Request.ServerVariables("Logon_User")>


    How to display the number of users on line
    Code to be placed in global.asa
    <script language=VBScript runat=Server>
    Sub Application_OnStart	
    Application("useronline") = 0
    End Sub
    
    Sub Application_OnEnd	
    	' Do Nothing
    End SubSub
    	Session_OnStart	
    	Session.Timeout = 20	
    	'---Lock the Application variable before updating	
    	Application.Lock
    		Application("useronline") = Application("useronline") + 1	
    	Application.Unlock
    End Sub
    	Sub Session_OnEnd	
    	Application.Lock	
    	Application("useronline") = Application("useronline") - 1	
    	Application.Unlock
    End Sub
    </script>
    HTML:
    Code to be placed in the asp file to diaplay the actual number of users currently online
    Number of User OnLine now : <%=Application("useronline")%>
     
    ludwig, Jul 13, 2008 IP