?
Solved

Finding IP Address of site visitor

Posted on 2013-06-13
3
Medium Priority
?
293 Views
Last Modified: 2013-06-22
I'm hopeing someone has encountered this before.  I've used this code to get the IP address of a site visitor before with no problems... now I'm adding it to a site that has a table called "Requests" and I am getting an error "Model.Request does not contain a definition for ServerVariables"  My assumption is that the issue is due to there being a table with the same name.  Is there a way around this or a different method of finding the IP address of a site visitor?

Thanks,
Greg


string ip =  Request.ServerVariables["remote_addr"].ToString();

Open in new window

0
Comment
Question by:Greg_L_WER
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 15

Expert Comment

by:angus_young_acdc
ID: 39247083
Hi Greg

The following link has some very good information:
http://stackoverflow.com/questions/735350/how-to-get-a-users-client-ip-address-in-asp-net
0
 

Accepted Solution

by:
Greg_L_WER earned 0 total points
ID: 39253896
Hi Angus,

Thanks for the info.. unfortunately none of the options seem to work in my system due to the fact that I have a table called Requests which conflicts with the code.  There was one part of the page that did work though it returns the IP address of the internal PC, not the external IP that I'm needing.  I did uncover a method that does appear to work...


    private string ExternalIPFinder() {
        try {
            WebClient client = new WebClient();
            // Add a user agent header in case the requested URI contains a query.

            client.Headers.Add("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR1.0.3705;)");
            string baseurl = "http://checkip.dyndns.org/";
            Stream data = client.OpenRead(baseurl);
            StreamReader reader = new StreamReader(data);
            string IPAddress = reader.ReadToEnd();
            data.Close();
            reader.Close();
            IPAddress = IPAddress
                .Replace("<html><head><title>Current IP Check</title></head><body>", "")
                .Replace("</body></html>", "")
                .Replace("Current IP Address:", "")
                .Replace("\r\n", "").ToString().Trim();

            return IPAddress;
        }
        catch {
            return null;
        }
    }

Open in new window

0
 

Author Closing Comment

by:Greg_L_WER
ID: 39267777
Other solutions provided didn't solve my problem but I came across this information elsewhere and wanted to submit it so it's available for others...
0

Featured Post

New benefit for Premium Members - Upgrade now!

Ready to get started with anonymous questions today? It's easy! Learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Preface This is the third article about the EE Collaborative Login Project. A Better Website Login System (http://www.experts-exchange.com/A_2902.html) introduces the Login System and shows how to implement a login page. The EE Collaborative Logi…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Shows how to create a shortcut to site-search Experts Exchange using Google in the Chrome browser. This eliminates the need to type out site:experts-exchange.com whenever you want to search the site. Launch the Search Engine Menu: In chrome, via you…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

719 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question