Hashemian Blog
Web, Finance, Technology

IP address and Host name Scripts

    📂 Computers,Web         🗨 7 Comments

As some of you know, this site contains several utilities in the tools section. One of these tools is JavaScript Visitor IP Address and Host Name. It's a simple JavaScript block that can be placed inside any web page and it displays or prints the visitor's IP address and host name.

As is with the rest of the tools, this one was also born out of necessity and I decided to share it with everyone. But some people are not comfortable putting my scripts on their site. That's cool and I don’t blame them, they don’t know me. So here I am going to explain how to display or print the user's IP address and host name using a number of server-side technologies.

The keyword here is "server-side". That's right, there is no way you can glean that information from client-side JavaScript. Even my JavaScript utility uses server-side calls to obtain the data and then packages it up in JavaScript format and streams it back. If your site supports server-side scripting, then chances are one of the following will do the job for you.

  • Perl:
  • print "IP: $ENV{'REMOTE_ADDR'}<br>Host: $ENV{'REMOTE_HOST'}";

  • SSI:
  • IP: <!--#echo var="REMOTE_ADDR"--><br>Host: <!--#echo var="REMOTE_HOST"-->

  • PHP:
  • <?= "IP: {$_SERVER['REMOTE_ADDR']}<br>Host: {$_SERVER['REMOTE_HOST']}" ?>

  • ASP:
  • <%= "IP: " + Request.ServerVariables("REMOTE_ADDR") + "<br>Host: " + Request.ServerVariables("REMOTE_HOST") %>

  • ASP.NET:
  • <%= "IP: " + Request.UserHostAddress + "<br>Host: " + Request.UserHostName %>

  • Python:
  • print "IP: " + cgi.os.environ["REMOTE_ADDR"] + "<br>Host: " + cgi.os.environ["REMOTE_HOST"]

  • Ruby:
  • print "IP: " + ENV["REMOTE_ADDR"] + "<br>Host: " + ENV["REMOTE_HOST"]

  • JSP:
  • <%= "IP: " + request.getRemoteAddr() + "<br>Host: " + request.getRemoteHost() %>

  • Java Servlet:
  • out.println("IP: " + request.getRemoteAddr() + "<br>Host: " + request.getRemoteHost());

    A common issue with the above calls is that in many cases host names may be returned as IP addresses or nothing at all. In some cases that is because no reverse record for a client's IP address is available. But if this issues occurs all the time, it could mean that reverse resolution is turned off. This is generally done for performance reason, to save on server resources.

    You can ask your hosting company to turn that service on, or you could configure reverse lookup yourself if you have access to the server configuration files. Here’s how reverse look up is switched on for Internet Information Server (IIS) and Apache.

  • IIS (execute at command line):
  • adsutil set w3svc/EnableReverseDNS TRUE

  • Apache (edit httpd.conf file):
  • HostnameLookups On

    By the way, if you ever wanted to run a simple reverse lookup on an IP address, here's a Reverse Whois tool for that job.

    Before I end this post, here's one more piece of information for those who might wonder where parameters like REMOTE_ADDR or REMOTE_HOST come from. Those are part of a collection of parameters known as environment variables that web servers are expected to make available to the scripts. Want the gory details? Read here.

    There you have it. If you can put any of the above scripts to use instead of using my JavaScript utility, I'd appreciate the bandwidth savings. And to those who continue to use my utilities, your trust and confidence are appreciated.



    1. Thanks !!! very useful !!!

      Comment by mohrd — August 27, 2009 @3:33 am

    2. Wow Interesting news about the IP-Address details i Found the Ip-Address details in the site ip-details.com

      Comment by niranjani — August 28, 2009 @12:05 am

    3. Interesting but is there a way to get localhost name?


      Comment by Bertrand — May 18, 2010 @6:30 am

    4. hi,

      thanks for the explanation. i am still not skilled enough to make use of it. i am developing a site that makes use of your script. unfortunately it's going to cost you a lot on the bandwidth if/when my site goes live. for me it's not good even now in testing because my code trips as often i cant get access to your site.

      i am using php script and will be setting up my own server with Apache. any chance you can give me full code ie. js to be embeded in php and Apache changes to be made.i know nothing about Apache server side includes & scripting.

      i know it's a bit of an ask but i want to get this working by myself before i get help in actually setting up the proper lamp.

      if you can give the exact code and instructions i'll sort you out for a meal.



      Comment by ali — July 23, 2010 @1:38 pm

    5. I have seen some of the scripts working, great work

      Comment by — September 26, 2010 @5:12 pm

    6. The Browser button stopped working with Firefox 4



      (bottom of above website page)

      Comment by Mike Burns — March 24, 2011 @7:36 pm

    7. Hello

      I have a website, and a nuisance.
      And he, by the message is annoying me.
      I want the IP address of the sender with all message be sent to me.
      What is your suggestion?
      Please Help Me.

      Thank you
      Amer - From Iran

      Comment by Amer Hashemi — February 26, 2012 @4:27 pm


    * Comments are subject to screening and manual approval.

    Read Financial Markets  |   Home  |   Web Tools  |   Blog  |   News  |   Articles  |   FAQ  |   About  |   Privacy  |   Contact
    Give a few Sats: 1GfrF49zFWfn7qHtgFxgLMihgdnVzhE361
    © 2001-2024 Robert Hashemian   Powered by Hashemian.com