Trapping IP address

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.
 
Z

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"];
}
 
A

Arne Vajhøj

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
 

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. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,983
Messages
2,570,187
Members
46,747
Latest member
jojoBizaroo

Latest Threads

Top