We help IT Professionals succeed at work.

Check out our new AWS podcast with Certified Expert, Phil Phillips! Listen to "How to Execute a Seamless AWS Migration" on EE or on your favorite podcast platform. Listen Now

x

Get User IP address

lulu50
lulu50 asked
on
Medium Priority
376 Views
Last Modified: 2012-06-27
Hi,

I want to get the user IP address when they visit my site.  even if they have a firewall software application installed on their machine.

the code that I have now it display a number different then the IP address that I see when I run the cmd ipconfig /all
when I refresh my page the IP address Is: 12.176.249.232
but when I do a run cmd ipconfig then I see this IP: 10.25.120.194

So, I am not sure which one is correct??? and why I am getting two different one and
what's the code to get the correct IP address from the users machine???

here's my code:
      <CFIF #CGI.HTTP_X_Forwarded_For# EQ "">
            <CFSET ipaddress="#CGI.Remote_Addr#">
      <CFELSE>
            <CFSET ipaddress="#CGI.HTTP_X_Forwarded_For#">
      </CFIF>

<cfoutput>#ipaddress#</cfoutput>
 


Comment
Watch Question

I believe that you are trying to get the machines IP address?
When your client is behind a firewall, the only IP address that will be shown is that of the firewall's outside address.
You would probably need some local java or vb on the clients machine to transmit the machine's internal IP address.

You can <cfdump  var="#cgi#"> to see all the information that is available to you.

I always use #cgi.remote_host# but that is my personal preference.
CERTIFIED EXPERT

Commented:

when I refresh my page the IP address Is: 12.176.249.232
but when I do a run cmd ipconfig then I see this IP: 10.25.120.194


The 10.25... address is your Internal IP address.   When inside a local area network (LAN) every computer is given an internal IP address.    The 12.176... number is the public or external address (Wide Area Network = WAN).   This is the one available over the internet.  You cannot get a user's internal IP address,  but if you could, there is nothing useful about it.  Internal addresses are not unique at all...   You will have MANY users with the same internal IP address.  

Internal addresses exist so that a company or local area of computers can manage a network without conflicting with the internet.  Imagine if every computer inside a company had to have a public IP address, the Public IPs would have to be three times longer !  

So, you should share what it is you're trying to accomplish and maybe we can help you with the best approach.  For example, you could use a unique session id, etc..

lulu50Web application

Author

Commented:
gdemaria:

Thank you for making things clear to me this is great.  Let me tell you what I am trying to accomplish.
I have the main page called index.cfm
1. On index.cfm I have a list of states
exp:
New York
Connecticut
Michigan
Ohio

if the user visit my page for the first time that means this user is not a guest memeber of any state and a list
of all states will be displayed on index.cfm for the user to pick from.

if the user is not first time visitor then I should store something in my database from the user to know that they are
a guest member of a specific state.

if the user picked a specific state a detail information on that state will be displayed on a different page.

the whole idea is I don't want the user to every time visit my site to pick a state I want to store something in
my database to know that who ever used this machine is interested in this state so they will go directly to
a page that display information regarding the state the user selected.

Please, let me know if I confused you. (I have a broken english here)
CERTIFIED EXPERT

Commented:
I understand you very well, good job.

Your best bet is to use a cookie.   A cookie can indicate the user's preferred state and as long as the user returns to that computer, you will always know the state.

There are two ways to do this.   First, you can just save the state...

 <cfcookie name="preferState" value="CA">

The other option is to store a unique identifier for the user and have a matching one in the database for his/her state.

<cfcookie name="mySiteID"  value="329089874598743897435">

Associate that big number with the user's database record (preferably he will have a login or something associated with that so that if he/she goes to another computer, and he logs in, it will reset the cookie)


As far as their first visit, you can use locator code with the IP address to estimate their state and set it as the default.     That way when the user first visits your site, you could have their current state already selected, with the option of changing states, of course.    This code may cost money and may have a large database associated with it, so you may not want to do it, just an idea...

lulu50Web application

Author

Commented:
gdemaria:

I have a question what do you think craigslist.org is using
if you visit the site for the first time then you will see a list of state and cities to select from
and based on the selection the site will take you to a page that display information about the selected city.

I tried to remove the cookies from my web browser than went back to craigslist.org site. The site
directed me to the state I already picked that means they are not using cookies.  because I removed mine and it still know what state I work with??

I want to use the same idea on my site.  HMMMM how did they do that???????
lulu50Web application

Author

Commented:
I don't want to use cookies because the user can delete the cookies
CERTIFIED EXPERT

Commented:

They are probably using the IP location software I mentioned above.   You can do this too.  You take the IP address and call this software and get back their state.   There are lots of different things you can choose from.  Do some google searches on "IP address Location"  

But I would not be afraid to use cookies as well, really, how often does the typical user delete cookies?   And if they do, all they need to do is select the state again, not a big problem.

lulu50Web application

Author

Commented:
gdemaria:

Please explain again and do you code example of IP location software
I like this idea and would like to use it on my site. any example code that
I can take a look at please.
CERTIFIED EXPERT
Commented:
Unlock this solution with a free trial preview.
(No credit card required)
Get Preview
CERTIFIED EXPERT
Most Valuable Expert 2015
Commented:
Unlock this solution with a free trial preview.
(No credit card required)
Get Preview
CERTIFIED EXPERT

Commented:
Good Points agx...  


I suggest..

  On the user's first visit, set the location to the cookie, allow it to be modified by the user.

  Only check geo location if the cookie does not exist.
lulu50Web application

Author

Commented:
Thank you
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a free trial preview!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.