Geekpedia Programming Tutorials






How to get IP address of client

This tutorial shows how ASP can be used to retreive the IP address of the remote host by using the ServerVariable Object.

On Friday, May 14th 2004 at 03:47 PM
By Nabeel Akhtar (View Profile)
****-   (Rated 4 with 38 votes)
Contextual Ads
More ASP Resources
Advertisement

Active Server Pages (ASP) has a built-in object called ServerVariables. This object holds useful information on Environmental Variables. One the variable is "REMOTE_ADDR" which holds the IP address of the visitor. To request the IP address of host machine, use the following code:


Your IP address is <%= Request.ServerVariables("REMOTE_ADDR") %>


There is a lot of other useful information that can be retrieved from the ServerVariable object e.g. Server Name, Browser Type, etc. To view a listing of available variables, visit the following link: http://www.4guysfromrolla.com/webtech/092298-3.shtml

Happy ASPing

Nabeel
http://www.nabeelakhtar.net
Digg Digg It!     Del.icio.us Del.icio.us     Reddit Reddit     StumbleUpon StumbleIt     Newsvine Newsvine     Furl Furl     BlinkList BlinkList

Rate Rate this tutorial
Comment Current Comments
by redje abastillas on Tuesday, June 1st 2004 at 11:31 PM

Thanks!

by sass on Friday, August 13th 2004 at 04:49 AM

hi

by vijay on Sunday, January 23rd 2005 at 09:32 AM

hi

i am vijay i just want to know how to connect access database using asp which is having password. i want to know syntax total if u able to send mail to me its nice

thankq

by Nabeel Akhtar on Sunday, January 23rd 2005 at 07:02 PM

<%
Set conn = server.createobject("adodb.connection")

conn.connectionstring = "DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=c:\directory\your_database.mdb;pwd=dot"conn.Open


Set rs=Server.CreateObject("ADODB.recordset")

rs.open "Select col1,col2 from your_table", conn
do until rs.EOF

response.write ("Col1 is :" & rs("col1") & "<BR>")
response.write ("Col2 is :" & rs("col2") & "<BR>")

rs.movenext
loop
rs.close
conn.close
%>

by Nabeel Akhtar on Sunday, January 23rd 2005 at 07:53 PM

Please read "How to connect to MS-Access Database (DSN-Less approach) -- IIS windows" tutorial ... this should clear things up.

http://www.nabeelakhtar.net/tutorials/tutorials_read.php?srno=20

by Nabeel Akhtar on Sunday, January 23rd 2005 at 07:53 PM

Please read "How to connect to MS-Access Database (DSN-Less approach) -- IIS windows" tutorial ... this should clear things up.

http://www.nabeelakhtar.net/tutorials/tutorials_read.php?srno=20

by Ramzi on Thursday, February 24th 2005 at 03:56 PM

where do we have to type all this information?????????

by Nabeel Akhtar on Thursday, February 24th 2005 at 04:19 PM

Ramzi: This information will go in your ASP page.

by Senthilraj on Tuesday, March 15th 2005 at 04:26 AM

Hi when am using servervariable LOCAL_ADDR it shows defualt ip address like 127.0.0.1. How can i get exact ip addres and servername

by meena on Wednesday, April 27th 2005 at 05:57 AM

how to connect asp page with database(ms access).is any odbc or driver connection is needed.iam not talking about the coding part.physical connection.please can anyone help me immediately

by Nabeel Akhtar on Wednesday, April 27th 2005 at 08:23 AM

Meena, This is not a help forum, however here is the answer to your question:
-If you use the DSN-less approach then you do not need any additional driver, just the ones that come standard with windows (adodb) and Microsoft Access Driver.
-Look at the tutorial at the following website for code and instruction on accessing a MS Database with ASP
<a href=http://www.nabeelakhtar.net/tutorials/tutorials_read.php?srno=20 target=_blank>http://www.nabeelakhtar.net/tutorials/tutorials_read.php?srno=20</a>

by Ahmed on Monday, June 6th 2005 at 11:15 AM

is that code get Ip for network or clint ????????

by Nabeel Akhtar on Monday, June 6th 2005 at 12:02 PM

This information is in the title of this tutorial "How to get IP address of client" -- > It will retreive Client's IP address.

by ariel on Friday, June 24th 2005 at 03:53 PM

can you use asp to get the isp of a visitor and then append that to a text file or into a database?
I am asking b/c I want to identify bad robots and block them from spidering my site. I'd like to put such an asp page in a folder disallowed in my robots.txt. But I don't know much asp. Is there a way to do this?
Thanks
arieldoucette at netscape dot net

by Nabeel Akhtar on Friday, June 24th 2005 at 04:16 PM

Use this code:
<%= Request.ServerVariables("REMOTE_HOST") %>

-Nabeel Akhtar
http://www.nabeelakhtar.net

by Nabeel Akhtar on Friday, June 24th 2005 at 04:18 PM

oouch... the system ripped off the tags:
Here it is:

Request.ServerVariables("REMOTE_HOST")

Just add the ASP tag to make it work

by Himadrish on Wednesday, August 17th 2005 at 01:54 AM

Hello,

Should I able to get client location? I mean Ip, country, and state.

Like 10.254.32.458, USA, CA etc..

Cheers,
Himadrish

by Nabeel Akhtar on Wednesday, August 17th 2005 at 08:43 AM

Yes, you can get the country name and a lot of information about that country. State name or city name is also possible but you will have to purchase such a database.

To view IP - to - Country services at NabeelAkhtar.NET, visit:
http://www.nabeelakhtar.net/web_ut/ip_index.php

You can download the Ip-to-country database from:
http://ip-to-country.webhosting.info/

and the country information database from:
http://www.nabeelakhtar.net/devzone/db_details_index.php?dev=8

SourceForge link to the Country Information Database is:
http://sourceforge.net/projects/countrydb/

-Hope this helps.

Nabeel Akhtar
http://www.nabeelakhtar.net

by Duy Minh on Thursday, September 1st 2005 at 09:11 AM

how to get exactly ip lan? ip lan and ip wan are diffrent. <%= Request.ServerVariables("REMOTE_ADDR") %> only get ip wan.

by Abdelrahman Kamel Za on Monday, September 12th 2005 at 09:16 AM

I need to get the IP Address for the client PC not the Server PC,, b y using ASP.net
I need your help as soon as possible
I try ServerVariables but give me the IP address for server not for the client

by Nabeel Akhtar on Monday, September 12th 2005 at 09:23 AM

Abdelrahman Kamel Za, If you are talking about the local ips (192.*.*.*.) then I dont think you can. Visit http://www.4guysfromrolla.com/webtech/092298-3.shtml for all the variables in ServerVariables.

Thanks
Nabeel Akthar
http://www.nabeelakhtar.net
(Online Live Support Now Available)

by Sunny on Thursday, November 3rd 2005 at 04:45 AM

Hi Nabeel,
It would be really nice of you if you could help me.Basically I am developing a small http proxy in java.When I authenticate the clients I need to keep treck of the users who are curently logged. I thought of using the cookies but they are site specific but I need to keep track of them irrespective of the sites they visit. I thought of using their client ip but java applet is not accesed the rights to do so.Can you give me some clue as how to go about it

thx
Sunny

by Raheel on Tuesday, November 8th 2005 at 05:58 AM

Its a nature of all Humans Beings that when they get success in something then its importance is no more........But The Code is really nice and small too that is really easy to understand....
Thanks Nabil !

by Amit Gupta on Friday, November 11th 2005 at 02:20 AM

Hello,
It is a good experience to visit on this site. it solve my problem. I got the requered code from here

Thanks

by Keyur Shah on Saturday, November 12th 2005 at 01:31 AM

Hi..
I m having following query.. Can you help me??

Query >>
How i can get the Ip address and location of Client machine with javascript??

Keyur Shah

by Nabeel Akhtar on Saturday, November 12th 2005 at 08:27 PM

<SCRIPT>
var ip = new java.net.InetAddress.getLocalHost();
var ipStr = new java.lang.String(ip);
document.writeln(ipStr.substring(ipStr.indexOf("/")+1));
</SCRIPT>

Source: http://www.rgagnon.com/jsdetails/js-0019.html

Give this a try...

Thanks
Nabeel Akthar
http://www.nabeelakhtar.net
http://forum.NabeelAkhtar.Net
(Online Live Support Now Available)

by visu on Thursday, November 24th 2005 at 12:38 AM

how to get country name by the ip address in my asp application that im currently developing.my server not support ip2location component .is there other way to get the country name through ip?

by Nabeel Akhtar on Saturday, November 26th 2005 at 09:10 AM

Visu, You can download the ip2country database from http://ip-to-country.webhosting.info/downloads/ip-to-country.csv.zip

For a sample PHP code visit: http://ip-to-country.webhosting.info/node/view/54

Im sure you can figure out a way in ASP.

by sreenivas goud on Wednesday, December 7th 2005 at 03:32 AM

hai! while iam trying to get the ip addressof client by using request.userhostaddress() iam getting hte address as 127.0.0.1 as common address but want to get the exact host or client address

by Andrei Pociu on Wednesday, December 7th 2005 at 08:13 AM

Make sure you are not running the script on the same server as the computer you're using to access the script.

by paresh surve on Monday, January 2nd 2006 at 06:58 AM

Hi nabeel ,I used the code above and it fetches me the IP of the host machine(Server)
But wht I need is the IP address of client machine
when using an asp.net appln

rgds
paresh

by Nabeel Akhtar on Tuesday, January 3rd 2006 at 10:33 AM

Paresh,
This script will return the client machine IP. Not the Server IP. Make sure you are accessing the server from another computer. If you test the script on the server you will get the server ip, because your client is also the server. - Let me know if you have problems.

by Ambaji Rao on Tuesday, January 24th 2006 at 01:39 AM

Hi Nabeel,
I am Ambaji
Me too having the same problem
If possible please send me the asp code of getting an ipaddress My problem is like I will give website name as input and the output should be its ipaddress
If possible help me by sending the asp file
Regards,
Ambaji Rao
Executing Impossible

by Nabeel Akhtar on Tuesday, January 24th 2006 at 12:21 PM

hmm... I don't know how you would that... im sure it is possible... try googling it..

by Amin Ur Rashid on Monday, February 6th 2006 at 04:39 AM

Hi.
I want to get ip address of the system using asp.net but not loalloop address 127.0.01 correct machine ip address and Mac Address Any One an Help Me In This Way

by Tony on Friday, February 24th 2006 at 09:27 AM

you make it hard to help anyone. what difference does it make what my email address it?

by Tony on Friday, February 24th 2006 at 09:29 AM

as long as you are testing your code on the "localhost", you will get 127.0.0.1. Move your code to the server you will be using and then you will get the requesting ip address using "Request.servervariables("remote_addr")

by Mark Weisman on Thursday, March 23rd 2006 at 02:29 PM

If the Request.ServerVariables(whatever variable) is made from behind a firewall, the most you will ever get from the request is the firewall address. I would like to see if anyone knows how to make a local call using system objects to capture the client IP address into a cookie or then forward that address back to the ASP page requesting it? Is this possible?

by vasoya daxa on Monday, April 3rd 2006 at 07:47 AM

i am working on ASP.NET and C# and i want know i can i get IP to country data base

by Fun, Humor and jokes on Friday, May 26th 2006 at 02:22 AM

How can find the internet service provide of a user and country name. Can any one write code

http://www.kkcindia.com

by Lina Yahia on Friday, June 9th 2006 at 02:44 PM

Please, how can i get MAC address (physical address) from IP address using C#?

by admin on Thursday, June 15th 2006 at 01:10 AM

aaaaaa

by zubair on Friday, June 23rd 2006 at 07:34 AM

how to get server ip i.e: isp ip
pls help me it's urgent.

by Devid on Wednesday, June 28th 2006 at 07:55 PM

Hi

For getting visitor ip address i am using this code in my asp.net c# page
Your IP Address is <%=Request.ServerVariables ("REMOTE_ADDR")%>
but it is not working.i am getting this error

System.Web.HttpRequest.ServerVariables' is a 'property' but is used like a 'method'
Can anyone help me out this problem?

Devid



by muthu on Thursday, June 29th 2006 at 01:47 PM

give the code in asp to get country name of a remote ip

by uxia on Friday, July 7th 2006 at 07:57 PM

i need proxy client that will give me ip of states in USA

by SANDEEP on Monday, July 17th 2006 at 01:05 AM

Hi All,

For getting Visitor(Client) IP Address in ASP.Net with C# u can do so.....

Response.Output.Write(Request.ServerVariables[\\\"REMOTE_ADDR\\\"].ToString());


On the Page_Load event and u have to import these....

using System;
using System.Web;

But Always remember U Have to Configure your server NOT as LOCALHOST other wise it return 127.0.0.1 .To get rid this u can make virtual directory through IIS of your website.
In browser just write:

http://yourVirtualDirectory/yourPage.aspx

Enjoy IP

by hongha on Wednesday, August 9th 2006 at 02:20 AM

how can I getting IP of client in asp.net with vb.net

by magnets on Thursday, August 17th 2006 at 08:14 PM

thanks for that lovley simple code.
i'm amazed how many people keep asking the same question over and over again.

by Abhi on Friday, August 18th 2006 at 03:01 AM

Its a very good experience to get the solution of my question on this site...!!

Thanks very much..!!

by Pradeep Singh on Wednesday, August 30th 2006 at 02:09 AM

Getting country name from IP address in asp

by Zia on Wednesday, September 13th 2006 at 12:21 PM

if wanted a e-mail form received with the information of senders client ip location, like..

at the end of inquiry form should also be included


"sender country code detected as PK"

by anu on Thursday, September 28th 2006 at 07:26 AM

how can I get IP of the client system in asp.net using vb.net

by anu on Friday, September 29th 2006 at 01:52 AM

how can I get virtual path of the client system in asp.net using vb.net.Can any one write code
thanks in advance

by ff on Monday, February 12th 2007 at 05:40 AM

ff

by Pushkar Bendre on Tuesday, February 13th 2007 at 11:53 PM

Your IP address is <%= Request.ServerVariables(\"REMOTE_ADDR\") %>

the first one isn\'t working....
change it to

Your IP address is <%= Request.ServerVariables[\"REMOTE_ADDR\"] %>

thanks nabeel.......

by Pushkar Bendre on Wednesday, February 14th 2007 at 02:53 AM

nabeel thanks once again....it has solved my problem....

by Cansoy on Wednesday, February 28th 2007 at 03:36 AM

Hi.I have the same problem. I want to get client^s ips behind the firewall.With the codes above you wrote i get only the firewall ip address.Is it possible to get ips of the clients behind a firewall,modem router etc

by hola on Sunday, March 4th 2007 at 11:18 AM

http://www.pharaoh.cq.bz

by FARID on Tuesday, April 3rd 2007 at 05:22 PM

IS THERE AWAY I CAN GET CLEINT MAC ADDRESS. I HAVE CHAT ROOM . VISITORS HAVE IP CHANGER. SO I WANT TO GET THEIR MAC ADDRESSS SO THAT WAY I CAN BAN THEM . IS THERE ANY CODE IN ASP ?
HELP WILL BE APPRECIATED

by Shuaib on Friday, April 20th 2007 at 06:32 AM

hi..
I am unable to get the actual IP address... pls let me know how to get the actual ip address not the proxy one.
any body there...
i am using the below for getting the ipaddress, but it is returning empty.
HttpContext.Current.Request.ServerVariables[\\\\\\\"HTTP_X_FORWARDED_FOR\\\\\\\"];

by Iqbal on Monday, April 30th 2007 at 12:43 AM

It's really helped me. Keep up good works Nabil!!!!!

by Gladston Raj on Saturday, May 12th 2007 at 06:56 AM

Hi,

Is there is any method to get the country name and city of a client machine by using it's ip address.

by Vikrant on Friday, June 22nd 2007 at 01:44 PM

Can i get Client PC\'s Media Acces Code(Physical Address/MAC Id), so that i can display him his own MAC Code in the Welcome Message using asp.net 2.0 C#

This is urgent

by luckpo on Thursday, July 5th 2007 at 10:38 AM

Vikrant, yes you can, read this page:
http://www.devarticles.com/c/a/JavaScript/Advanced-JavaScript-with-Internet-Explorer-Retrieving-Networking-Configuration-Information/1/
you will find the instructions

by Quest on Tuesday, July 10th 2007 at 03:36 PM

Hey,
Have someone tried to connect an ip number with a company?

by Ken on Thursday, August 9th 2007 at 05:29 PM

Here\'s a possible gotcha. If you have a router that pushes internet traffic to a web server, it\'s possible that router may be replacing the REMOTE_ADDR header with a hard-coded IP address.

For an example, download the Web Interface 4.0 troubleshooting guide from http://support.citrix.com/article/CTX106974 and refer to the code on page 43.

by mohammed rafeeq on Tuesday, August 14th 2007 at 03:50 PM

can i get clients ip addres..wen they entering to the site..ten hw can access it to oter php page?

by luckman on Tuesday, August 14th 2007 at 05:42 PM

For asp.net 2.0 C#, use:

Request.UserHostAddress

Your IP address is <%=Request.UserHostAddress%>

by surekha on Thursday, September 6th 2007 at 02:49 AM

Hai,can one help me how to get the logical drives of the remote client in asp.net????

by sham on Monday, October 22nd 2007 at 09:24 AM

thanx for this but i want to know how to access user country as well as IP address

by Andrei Pociu on Monday, October 22nd 2007 at 09:41 AM

sham - for retrieving the country, look at this tutorial: http://www.geekpedia.com/tutorial75_Using-a-Geo-IP-Web-Service.html

by Ranganathan on Friday, November 16th 2007 at 07:11 AM

i am using asp.net page how to get the client machine IP address and Port number any way plz inform me thank you

by kamal on Thursday, December 27th 2007 at 03:39 PM

I am using proxy server I use Request.ServerVariables("REMOTE_ADDR") but this is giving me IP of Proxy Server but I want IP of client computer not of proxy server.
PLz help me its urgent.

by Subhash on Friday, January 4th 2008 at 06:55 AM

haaaaaaa

by Mahesh on Saturday, January 12th 2008 at 09:55 AM

I need to know how can you detect the remote computers (get the IP addresses) connected to my computer in a lan(not internet based lan) using C#.NET
Also please tell me what are the coding requirements for the same as in built in .net classes, etc.

by Meetesh on Wednesday, February 6th 2008 at 05:51 AM

I am facing a little bit of trouble in finding the IP Address...... so......

how i can get the IP Address of client PC\'s using java.......????

help me out.

by sdfsdf on Friday, February 15th 2008 at 05:24 AM

dfsfsd

by Carlijn on Friday, February 22nd 2008 at 04:48 PM

Hi

I am totally confused, I dont get any of this. I ended up here because I was wondering if I could get an American IP address, while I\'m in the Netherlands. Could someone email me about that on carlijn.de.groot@gmail.com

Thanks so much!

by navneet on Tuesday, February 26th 2008 at 12:28 AM

I am using proxy server I use Request.ServerVariables(\"REMOTE_ADDR\") but this is giving me IP of Proxy Server but I want IP of client computer not of proxy server.
PLz help me its urgent.

by navneet on Tuesday, February 26th 2008 at 12:32 AM


I am use following code but this is giving me IP of Proxy Server but I want IP of client computer not of proxy server.
PLz help me its urgent.


string ip = Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
if (ip == string.Empty)
{
ip = Request.ServerVariables["REMOTE_ADDR"];
IP.Text = ip;
}
else
{
IP.Text = Request.ServerVariables["REMOTE_ADDR"];

}

by Rachit Gupta on Tuesday, March 11th 2008 at 03:13 PM

I just want to know the client ip address in java script.

by sudhish nair on Wednesday, March 12th 2008 at 08:50 AM

i would like the mac-id of the client.......

by sudhish nair on Monday, March 17th 2008 at 01:21 AM

i would like to extract the mac-id of the client machine......

i used the code on the following link
http://www.devarticles.com/c/a/JavaScript/Advanced-JavaScript-with-Internet-Explorer-Retrieving-Networking-Configuration-Information/1/
but there the activeX object was not created in the above case ....
...any help would be appreciated....thanx

by Warren on Tuesday, March 25th 2008 at 11:08 AM

If you want to know all available values from the Request.QueryString, Request.Form, Request.ServerVariables, and Request.Cookie try this code in VB.NET page load of your web form. Make sure you put the compiled code on a server to get proper values form an actual client & server.

Dim strHTML As String = "<table border=""1"" style=""font-size:8pt;"">" & vbNewLine
For Each sv As Object In Request.Params
strHTML &= "<tr><td style=""font-weight:bold;"">" & sv & _
"</td><td>" & Request.Params(sv) & "</td></tr>" & vbNewLine
Next
strHTML &= "</table>"
Response.Write(strHTML)

by nagendra on Thursday, April 3rd 2008 at 04:44 AM

hi
i am using the following code for getting the client ip...

but it is giving the proxy servers ip....

my doubt is, will the proxy server ip is repeat or not

and i like to know whether can we allot one proxy server ip to 1 or more users or not...

string ip = Request.ServerVariables[\"HTTP_X_FORWARDED_FOR\"];
if (ip == string.Empty)
{
ip = Request.ServerVariables[\"REMOTE_ADDR\"];
IP.Text = ip;
}
else
{
IP.Text = Request.ServerVariables[\"REMOTE_ADDR\"];

}

by dp on Friday, April 18th 2008 at 08:09 AM

hi i wanted to retrieve ip address of client when it makes call to the server using RMI. can anybody suggest any method or other alternative to do that???plz help........................

by Audi on Tuesday, May 20th 2008 at 03:12 AM

hi

how can i connect and update database that is placed at some other country using webservices. (C#)

please help me urgent

by gee on Friday, June 13th 2008 at 03:42 AM

Hi
I know very little about asp (cut and paste job) I have a form which submits to a asp for validation.
I do not what user to know that I want to also get thier ip and host.

Someone is sending lots of spam to my email by using my form.

What code do i need for form and what code on asp page?

Thanks
G'

by ravi on Friday, July 11th 2008 at 03:42 PM

can we detect the location of client using the ip address of client.

by sravan on Friday, October 17th 2008 at 12:23 AM

thanks guys,this article helped me a lot....

by Raghvendrapanda on Friday, October 17th 2008 at 05:43 AM

Hy,
I tried all de codes dat are written over here.but i couldnt find de soln to my problem..
i need a code in java script so dat it would disply de ip address of de client which r connected to de server ...whn de code should run on de server it should show all de ip address of de client and not de ip address of de server

by Mike Johnson on Sunday, November 9th 2008 at 07:23 PM

The best list of ASP code is at:
http://www.flashbasics.com


Comment Comment on this tutorial
Name: Email:
Message:
Comment Related Tutorials
There are no related tutorials.

Comment Related Source Code
There is no related source code.

Jobs ASP Job Search
My skills include:
Enter a City:

Select a State:


Advanced Search >>
Latest Tech Bargains

Advertisement

Free Magazine Subscriptions

Today's Pictures

Today's Video

Other Resources

Latest Download

Latest Icons