Retrieve visitor's IP address using ASP, PHP, JSP & ColdFusion

Retrieve visitor's IP address using ASP, PHP, JSP & ColdFusion


The IP address is available from the web server variable "REMOTE_ADDR".

ASP without Proxy detection

<%
ipaddress = Request.ServerVariables("REMOTE_ADDR")
%>


ASP with Proxy detection

<%
ipaddress = Request.ServerVariables("HTTP_X_FORWARDED_FOR")
if ipaddress = "" then
ipaddress = Request.ServerVariables("REMOTE_ADDR")
end if
%>


PHP without Proxy detection

<?
$ipaddress = getenv(REMOTE_ADDR);
?>


PHP with Proxy detection

<?
if (getenv(HTTP_X_FORWARDED_FOR)) {
$ipaddress = getenv(HTTP_X_FORWARDED_FOR);
} else {
$ipaddress = getenv(REMOTE_ADDR);
}
?>


JSP without Proxy detection

<%
String ipaddress = request.getRemoteAddr();
%>


JSP with Proxy detection

<%
if (request.getHeader("HTTP_X_FORWARDED_FOR") == null) {
String ipaddress = request.getRemoteAddr();
} else {
String ipaddress = request.getHeader("HTTP_X_FORWARDED_FOR");
}
%>


ColdFusion without Proxy detection

<CFCOMPONENT>
<CFSET ipaddress="#CGI.Remote_Addr#">
</CFCOMPONENT>


ColdFusion with Proxy detection

<CFCOMPONENT>
<CFIF #CGI.HTTP_X_Forwarded_For# EQ "">
<CFSET ipaddress="#CGI.Remote_Addr#">
<CFELSE>
<CFSET ipaddress="#CGI.HTTP_X_Forwarded_For#">
</CFIF>
</CFCOMPONENT>


ASP.NET (C#) without Proxy detection

public string IpAddress()
{
return Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
}


ASP.NET (C#) with Proxy detection

public string IpAddress()
{
string strIp;
strIp = Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
if (strIp == null)
{
strIp = Request.ServerVariables["REMOTE_ADDR"];
}
return strIp;
}


ASP.NET (VB.NET) without Proxy detection

Public Function IpAddress()
IpAddress = Request.ServerVariables("HTTP_X_FORWARDED_FOR")
End Function


ASP.NET (VB.NET) with Proxy detection

Public Function IpAddress()
Dim strIp As String
strIp = Request.ServerVariables("HTTP_X_FORWARDED_FOR")
If strIp = "" Then
strIp = Request.ServerVariables("REMOTE_ADDR")
End If
IpAddress = strIp
End Function


2 comments:

You can also show an image on your website to show visitor IP address and country flag. Just go to the website findmyipaddress.info. copy the code and paste it in your website. save you the time to write the code. ;)
Hi and Thanx Alot for your very useful post