Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. How to detect the user IP?

How to detect the user IP?

Scheduled Pinned Locked Moved ASP.NET
tutorialquestion
15 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Seraph_summer

    I need to know the user IP to give the right limit to access my website, how to detect the user IP? and moreover, how to know which country is the user from? thanks!

    A Offline
    A Offline
    Abhishek Sur
    wrote on last edited by
    #3

    Client IP could be found from

    ip=Request.ServerVariables("HTTP_X_FORWARDED_FOR");
    if(ip==string.Empty)
    {
    ip=Request.ServerVariables("REMOTE_ADDR");
    }

    Abhishek Sur


    My Latest Articles **Create CLR objects in SQL Server 2005 C# Uncommon Keywords Read/Write Excel using OleDB

    **Don't forget to click "Good Answer" if you like to.

    S 1 Reply Last reply
    0
    • A Abhishek Sur

      Client IP could be found from

      ip=Request.ServerVariables("HTTP_X_FORWARDED_FOR");
      if(ip==string.Empty)
      {
      ip=Request.ServerVariables("REMOTE_ADDR");
      }

      Abhishek Sur


      My Latest Articles **Create CLR objects in SQL Server 2005 C# Uncommon Keywords Read/Write Excel using OleDB

      **Don't forget to click "Good Answer" if you like to.

      S Offline
      S Offline
      Seraph_summer
      wrote on last edited by
      #4

      Hi, thanks! I tried this code on my own PC, it shows the IP: 127.0.0.1 this is obviouly not right. This is not the right IP of my computer. I saw from internet, there are also others have the same problem. then some people suggest using the following code: string strHostName = System.Net.Dns.GetHostName(); string clientIPAddress = System.Net.Dns.GetHostAddresses(strHostName).GetValue(0).ToString(); but it shows the result as follows: fe70::8c3:258f:3f47:fd6b%12 this is IP?? do you have more comments on this? thank you again!!

      A 1 Reply Last reply
      0
      • P Parwej Ahamad

        Go through with this URL http://beta.codeproject.com/KB/aspnet/aspxcode_net.aspx[^] Let me know if you stuck anywhere.

        Parwej Ahamad ahamad.parwej@gmail.com

        A Offline
        A Offline
        amitraj82
        wrote on last edited by
        #5

        This is a good article. :)

        1 Reply Last reply
        0
        • P Parwej Ahamad

          Go through with this URL http://beta.codeproject.com/KB/aspnet/aspxcode_net.aspx[^] Let me know if you stuck anywhere.

          Parwej Ahamad ahamad.parwej@gmail.com

          S Offline
          S Offline
          Seraph_summer
          wrote on last edited by
          #6

          I tried this code on my own PC, it shows the IP: 127.0.0.1 this is obviouly not right. This is not the right IP of my computer. I saw from internet, there are also others have the same problem. then some people suggest using the following code: string strHostName = System.Net.Dns.GetHostName(); string clientIPAddress = System.Net.Dns.GetHostAddresses(strHostName).GetValue(0).ToString(); but it shows the result as follows: fe70::8c3:258f:3f47:fd6b%12 this is IP?? do you have more comments on this? thank you again!!

          P 1 Reply Last reply
          0
          • S Seraph_summer

            I tried this code on my own PC, it shows the IP: 127.0.0.1 this is obviouly not right. This is not the right IP of my computer. I saw from internet, there are also others have the same problem. then some people suggest using the following code: string strHostName = System.Net.Dns.GetHostName(); string clientIPAddress = System.Net.Dns.GetHostAddresses(strHostName).GetValue(0).ToString(); but it shows the result as follows: fe70::8c3:258f:3f47:fd6b%12 this is IP?? do you have more comments on this? thank you again!!

            P Offline
            P Offline
            Parwej Ahamad
            wrote on last edited by
            #7

            I tried this code on my own PC, it shows the IP: 127.0.0.1 this is obviouly not right. This is not the right IP of my computer. Hi you are in right direction and it is correct. It alwasy gives IP: 127.0.0.1 because you are requesting from your local machine. When you will acces you web application via different machine then it will give the correct IP. Anyway your code is correct.

            Parwej Ahamad ahamad.parwej@gmail.com

            S 1 Reply Last reply
            0
            • P Parwej Ahamad

              I tried this code on my own PC, it shows the IP: 127.0.0.1 this is obviouly not right. This is not the right IP of my computer. Hi you are in right direction and it is correct. It alwasy gives IP: 127.0.0.1 because you are requesting from your local machine. When you will acces you web application via different machine then it will give the correct IP. Anyway your code is correct.

              Parwej Ahamad ahamad.parwej@gmail.com

              S Offline
              S Offline
              Seraph_summer
              wrote on last edited by
              #8

              thanks, I see, but do you know string strHostName = System.Net.Dns.GetHostName(); string clientIPAddress = System.Net.Dns.GetHostAddresses(strHostName).GetValue(0).ToString(); gives also IP? or sth else? thank you again!! your article is great!!

              P A 2 Replies Last reply
              0
              • S Seraph_summer

                Hi, thanks! I tried this code on my own PC, it shows the IP: 127.0.0.1 this is obviouly not right. This is not the right IP of my computer. I saw from internet, there are also others have the same problem. then some people suggest using the following code: string strHostName = System.Net.Dns.GetHostName(); string clientIPAddress = System.Net.Dns.GetHostAddresses(strHostName).GetValue(0).ToString(); but it shows the result as follows: fe70::8c3:258f:3f47:fd6b%12 this is IP?? do you have more comments on this? thank you again!!

                A Offline
                A Offline
                Abhishek Sur
                wrote on last edited by
                #9

                Seraph_summer wrote:

                string strHostName = System.Net.Dns.GetHostName(); string clientIPAddress = System.Net.Dns.GetHostAddresses(strHostName).GetValue(0).ToString();

                This is the most funniest code :-D :-D . It means it will try to resolve the dns of the host. Even I saw this. I dont know how people give suggestions to these to resolve IP. .. :laugh: If you are using localhost, the ip will always be 127.0.0.1 as the server and client both the same. Try putting in IIS and get the IP from your local machine. I think you will get the IP address properly. You can also try Request.ServerVariables["REMOTE_HOST"] or Request.UserHostAddress It will give the same output as well. :-D :-D

                Abhishek Sur


                My Latest Articles **Create CLR objects in SQL Server 2005 C# Uncommon Keywords Read/Write Excel using OleDB

                **Don't forget to click "Good Answer" if you like to.

                S 1 Reply Last reply
                0
                • S Seraph_summer

                  thanks, I see, but do you know string strHostName = System.Net.Dns.GetHostName(); string clientIPAddress = System.Net.Dns.GetHostAddresses(strHostName).GetValue(0).ToString(); gives also IP? or sth else? thank you again!! your article is great!!

                  P Offline
                  P Offline
                  Parwej Ahamad
                  wrote on last edited by
                  #10

                  This will give Host IP where your application is deployed. But you are looking for visitor/Client IP.

                  Parwej Ahamad ahamad.parwej@gmail.com

                  S 1 Reply Last reply
                  0
                  • A Abhishek Sur

                    Seraph_summer wrote:

                    string strHostName = System.Net.Dns.GetHostName(); string clientIPAddress = System.Net.Dns.GetHostAddresses(strHostName).GetValue(0).ToString();

                    This is the most funniest code :-D :-D . It means it will try to resolve the dns of the host. Even I saw this. I dont know how people give suggestions to these to resolve IP. .. :laugh: If you are using localhost, the ip will always be 127.0.0.1 as the server and client both the same. Try putting in IIS and get the IP from your local machine. I think you will get the IP address properly. You can also try Request.ServerVariables["REMOTE_HOST"] or Request.UserHostAddress It will give the same output as well. :-D :-D

                    Abhishek Sur


                    My Latest Articles **Create CLR objects in SQL Server 2005 C# Uncommon Keywords Read/Write Excel using OleDB

                    **Don't forget to click "Good Answer" if you like to.

                    S Offline
                    S Offline
                    Seraph_summer
                    wrote on last edited by
                    #11

                    thanks, I see!

                    A 1 Reply Last reply
                    0
                    • P Parwej Ahamad

                      This will give Host IP where your application is deployed. But you are looking for visitor/Client IP.

                      Parwej Ahamad ahamad.parwej@gmail.com

                      S Offline
                      S Offline
                      Seraph_summer
                      wrote on last edited by
                      #12

                      really? if so, I am confused by this code. thanks again!

                      P 1 Reply Last reply
                      0
                      • S Seraph_summer

                        thanks, I see, but do you know string strHostName = System.Net.Dns.GetHostName(); string clientIPAddress = System.Net.Dns.GetHostAddresses(strHostName).GetValue(0).ToString(); gives also IP? or sth else? thank you again!! your article is great!!

                        A Offline
                        A Offline
                        Abhishek Sur
                        wrote on last edited by
                        #13

                        Seraph_summer wrote:

                        your article is great!!

                        At least the Database provided in the article is the most important part of it. Its a great one indeed. :)

                        Abhishek Sur


                        My Latest Articles **Create CLR objects in SQL Server 2005 C# Uncommon Keywords Read/Write Excel using OleDB

                        **Don't forget to click "Good Answer" if you like to.

                        1 Reply Last reply
                        0
                        • S Seraph_summer

                          really? if so, I am confused by this code. thanks again!

                          P Offline
                          P Offline
                          Parwej Ahamad
                          wrote on last edited by
                          #14

                          Yes :) Now your next job is to find the country name.

                          Parwej Ahamad ahamad.parwej@gmail.com

                          1 Reply Last reply
                          0
                          • S Seraph_summer

                            thanks, I see!

                            A Offline
                            A Offline
                            ABitSmart
                            wrote on last edited by
                            #15

                            If the user is behind a proxy then REMOTE_ADDR will be the proxy IP address and HTTP_X_FORWARDED_FOR will be the client IP address. HTTP_X_FORWARDED_FOR can consist of multiple comma separated IP addresses for each proxy the request passes through. Generally, client IP is the first of them. Finally, you can rely on the above methods only for Transparent proxies. There is no way to trace the actual IP in case of Anonymous or Distorting proxies. One more good tool for finding country from an IP is this one[^] on codeplex.

                            1 Reply Last reply
                            0
                            Reply
                            • Reply as topic
                            Log in to reply
                            • Oldest to Newest
                            • Newest to Oldest
                            • Most Votes


                            • Login

                            • Don't have an account? Register

                            • Login or register to search.
                            • First post
                              Last post
                            0
                            • Categories
                            • Recent
                            • Tags
                            • Popular
                            • World
                            • Users
                            • Groups