Trapping IP address

Discussion in 'ASP .Net' started by Zach, Jun 1, 2012.

  1. Zach

    Zach Guest

    I was looking for code to trap the visitor's IP address and found the
    following:
    Partial Class IP_Address
    Inherits System.Web.UI.Page
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
    Handles Me.Load
    Dim nowip As String
    nowip = Request.ServerVariables("HTTP_X_FORWARDED_FOR")
    If nowip = "" Then
    nowip = Request.ServerVariables("REMOTE_ADDR")
    End If
    If txtIPAddress.Text = "" Then
    txtIPAddress.Text = nowip
    End If
    End Sub
    End Class
    ......
    Translating into C#, I ran into problems;
    for a start the using isn't recognized:

    I tried:

    using System.Web.UI.Page;
    // blabla
    string nowip = Request.ServerVariables("HTTP_X_FORWARDED_FOR");
    if(nowip == stringEmpty) nowip = Request.ServerVariables("REMOTE_ADDR");
    string ip_address = " the visitor's IP address is: " + nowip;

    This corde produces errors;
    could you please help to get it right?
    Zach.
     
    Zach, Jun 1, 2012
    #1
    1. Advertisements

  2. Zach

    Zach Guest

    If anyone is interested, this is what the code should be like.

    Zach.

    string ipAddress = Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
    if (ipAddress == null || ipAddress == "")
    {
    ipAddress = Request.ServerVariables["REMOTE_ADDR"];
    }
     
    Zach, Jun 1, 2012
    #2
    1. Advertisements

  3. Zach

    Arne Vajhøj Guest

    Yes. C# uses [] for indexers.

    Arne
     
    Arne Vajhøj, Jun 2, 2012
    #3
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.