Trapping IP address

  • Thread starter Thread starter Zach
  • Start date Start date
Z

Zach

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.
 
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"];
}
 
If anyone is interested, this is what the code should be like.
string ipAddress = Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
if (ipAddress == null || ipAddress == "")
{
ipAddress = Request.ServerVariables["REMOTE_ADDR"];
}

Yes. C# uses [] for indexers.

Arne
 
Back
Top