Experts Exchange Solution brought to you by
"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.
This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.
/// Attempts to retrieve the real client ip (w/ proxy detection).
/// <returns>The IP address of the remote client,
/// or "0.0.0.0" if it cannot be obtained.</returns>
public static string GetClientIpAddress()
const string defaultValue = "0.0.0.0";
if ((context = HttpContext.Current) == null)
// store a local ref to the request object:
request = context.Request;
Debug.WriteLine("Http request context is not available.");
// check proxy forwarder indicators first:
string forwarderForIp = request.ServerVariables["HTTP_X_FORWARDED_FOR"];
int firstCommaIndex = forwarderForIp.IndexOf(',');
if (firstCommaIndex > 0)
// X-Forwarded-For can return multiple ip's, so
// just get the first one.
// ex: X-Forwarded-For: client, proxy1, proxy2
forwarderForIp = forwarderForIp.Substring(0, firstCommaIndex).Trim();
// get the client ip from server from server vars:
string remoteAdd = request.ServerVariables["REMOTE_ADDR"];
return !string.IsNullOrEmpty(remoteAdd) ? remoteAdd : defaultValue;
Open in new window
Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.
From novice to tech pro — start learning today.
Members can enroll in this course at no extra cost.