Library code snippets
Get the visitor's IP address
Whenever a browser sends a request for a page, it also sends a number of other headers to the script, containing information such as the browser type. It also includes information such as the visitors IP address. This can be particularly useful when creating an online security scan, or logging their IP address in an online forum.
There are two server variables of interest; REMOTE_ADDR and HTTP_X_FORWARDED_FOR.
As many visitors access the internet via a third party (ie their ISP), REMOTE_ADDR
does not always contain their IP address... it contains their ISP's address.
If this is the case, most browsers then store the users IP address in the HTTP_X_FORWARDED_FOR
variable. So, first, we check HTTP_X_FORWARDED_FOR, and then if
that is empty, we try REMOTE_ADDR instead:
Dim sIPAddress
sIPAddress = Request.ServerVariables("HTTP_X_FORWARDED_FOR")
If sIPAddress="" Then sIPAddress = Request.ServerVariables("REMOTE_ADDR")
One other thing worth bearing in mind. If the user also accesses the internet
via a Proxy server, then HTTP_X_FORWARDED_FOR will contain the
Proxy's IP address. If this is the case, there is no way to get the users 'real'
IP address.
Related articles
Related discussion
-
Calling a function from ASP code
by dunk00 (3 replies)
-
GridView HyperLinkField Problem
by Paul2 (0 replies)
-
looking for help on asp
by cladironbeard (2 replies)
-
simple vb to c#, help please
by lksath (1 replies)
-
Binary Studio | software development outsourcing Ukraine
by Hexfinity (2 replies)
Related podcasts
-
Scott Guthrie
Scott catches up with Scott Guthrie in an interview covering Ajax, Asp 2.0, extender controls, CSS adapters and more.
Events coming up
-
Aug
27
Model-View-Presenter (MVC) in ASP.NET
San Francisco, United States
Model-View-Presenter (MVC) in ASP.NET Presenter Clayton Peddy, Terrace Software, Inc. Details TBD
what if when ever a user enters my site his/her ip address will be saved in a database? for me this is a good protection idea for no good users u save their ip in a database then if they do no good they will be denied the next time they enter the site... is there any code for this. thnx in advance
How do you log a visitors IP address if register globals is off? Most hosts only have register globals off for security reasons.
how can i see the online users` ips? have you got an idea ?
This thread is for discussions of Get the visitor's IP address.