Hashemian Blog

Web Tools, Financial Markets, Technology

Sunday, August 19, 2007

IP address and Host name Scripts 

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.

    ,,,,,,,,,,

    Labels: , , , , , , , ,

    <IP address and Host name Scripts>

    0 comments |

    0 Comments:

    Post a Comment

    This page is powered by Blogger. Isn't yours?

    Links
  • Hashemian Blog Feeds
  • Add to Google
  • Read Hashemian.com/blog/ with Bloglines
  • Subscribe to Hashemian.com/blog/ with My Yahoo!
  • Technorati Profile
  • TMCnet.com
  • ARCHIVES
  • 09/01/2003 - 10/01/2003
  • 03/01/2004 - 04/01/2004
  • 04/01/2004 - 05/01/2004
  • 05/01/2004 - 06/01/2004
  • 06/01/2004 - 07/01/2004
  • 07/01/2004 - 08/01/2004
  • 08/01/2004 - 09/01/2004
  • 09/01/2004 - 10/01/2004
  • 10/01/2004 - 11/01/2004
  • 11/01/2004 - 12/01/2004
  • 12/01/2004 - 01/01/2005
  • 01/01/2005 - 02/01/2005
  • 02/01/2005 - 03/01/2005
  • 03/01/2005 - 04/01/2005
  • 04/01/2005 - 05/01/2005
  • 05/01/2005 - 06/01/2005
  • 06/01/2005 - 07/01/2005
  • 07/01/2005 - 08/01/2005
  • 08/01/2005 - 09/01/2005
  • 09/01/2005 - 10/01/2005
  • 10/01/2005 - 11/01/2005
  • 11/01/2005 - 12/01/2005
  • 12/01/2005 - 01/01/2006
  • 01/01/2006 - 02/01/2006
  • 02/01/2006 - 03/01/2006
  • 03/01/2006 - 04/01/2006
  • 04/01/2006 - 05/01/2006
  • 05/01/2006 - 06/01/2006
  • 06/01/2006 - 07/01/2006
  • 07/01/2006 - 08/01/2006
  • 08/01/2006 - 09/01/2006
  • 09/01/2006 - 10/01/2006
  • 10/01/2006 - 11/01/2006
  • 11/01/2006 - 12/01/2006
  • 12/01/2006 - 01/01/2007
  • 01/01/2007 - 02/01/2007
  • 02/01/2007 - 03/01/2007
  • 03/01/2007 - 04/01/2007
  • 04/01/2007 - 05/01/2007
  • 05/01/2007 - 06/01/2007
  • 06/01/2007 - 07/01/2007
  • 07/01/2007 - 08/01/2007
  • 08/01/2007 - 09/01/2007
  • 09/01/2007 - 10/01/2007
  • 10/01/2007 - 11/01/2007
  • 11/01/2007 - 12/01/2007
  • 12/01/2007 - 01/01/2008
  • 01/01/2008 - 02/01/2008
  • 02/01/2008 - 03/01/2008
  • 03/01/2008 - 04/01/2008
  • 04/01/2008 - 05/01/2008
  • 05/01/2008 - 06/01/2008

  • Read Financial Markets  |   Home  |   Blog  |   Web Tools  |   News  |   Articles  |   FAQ  |   About  |   Contact

    © 2001-2008 Robert Vahid Hashemian
    Support the effort
    Liked this page?
    Please consider creating a link to it
    from your Web site.

    hashemian.com
    هاشمیان.com

     Home

     Blog

     Web Tools Add Free Web Tools custom Google Toolbar button (Requires Toolbar >V4)
    Usage

     News

     Articles

     FAQ

     About

     Contact

     Financial Markets Book
    Read Complete Book



    BOOK
    Great Speeches by African Americans: Frederick Douglass, Sojourner Truth, Dr. Martin Luther King, Jr., Barack Obama, and Others (Thrift Edition)
    James Daley
    $3.50


    BOOK
    Big Book of Apple Hacks: Tips & Tools for unlocking the power of your Apple devices (Hacks)
    Chris Seibold
    $34.99


    BOOK
    Mac OS X Leopard Pocket Guide (Pocket Reference)
    Chuck Toporek
    $14.99


    BOOK
    Lives of a Cell: Notes of a Biology Watcher
    Lewis Thomas
    $13.00


    BOOK
    The Wisdom of the Native Americans
    $17.95

    |ip-address-and-host-name-scripts|

    more…



    aStore - Hashemian.com on Amazon

    Visits: Powered by hashemian.com

     

     

     

     

     

    Search Hashemian.com





    MICKEY MOUSE SALAD FORK AND SPOON NEW IN BOX RARE
    $8.09
    Ends: Sun Jul 13, 2008 01:29:00 EST


    How To Host A Murder -The Last Train From Paris
    $4.99
    Ends: Sun Jul 13, 2008 01:29:07 EST


    Baby girl clothing, 3-6 month, purple sweater and skirt
    $1.99
    Ends: Sun Jul 13, 2008 01:29:08 EST


    4 Body Pierceing DVD's Must Have For Pros And Consumers
    $0.99
    Ends: Sun Jul 13, 2008 01:29:14 EST


    2200 PHP Scripts & Applications Webmaster Mega Kit Plus
    $3.99
    Ends: Sun Jul 13, 2008 01:29:15 EST

    more…