Solved

Finding IP Address of site visitor

Posted on 2013-06-13
3
277 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
  • 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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

Citrix XenApp, Internet Explorer 11 set to Enterprise Mode and using central hosted sites.xml file.
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…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
This Micro Tutorial will demonstrate how nuggets on the Web are formatted by using Chrome Developer Tools. These tools would not only view the site's CSS but it can also modify it and save the CSS to use on your own site.

832 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