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
Solved

Finding IP Address of site visitor

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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

SASS allows you to treat your CSS code in a more OOP way. Let's have a look on how you can structure your code in order for it to be easily maintained and reused.
If you are a web developer, you would be aware of the <iframe> tag in HTML. The <iframe> stands for inline frame and is used to embed another document within the current HTML document. The embedded document could be even another website.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
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…

808 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