Electronics-HandyMan
Electronics-HandyMan
Google
 
HandyMan on the Way

Display your Visitor's IP Address on your website
I have seen allot of requests for displaying a visitor's IP address on a web page.  Since HTML has no direct facility for that some third party support will be needed.  This page covers a number of methods to accomplish this task.

Method #1:  This is the easiest method, the catch is that your server must have SSI command support, and the file extension (at least on my server) has to be SHTML

SSI command example

<b>Visitor's IP Address = </b>
<!--#echo var="REMOTE_ADDR"-->

Method #2:  This method requires support of Javascript and server side variables.  Also on my server to get access to the server side variables the file must have a SHTML extension.

Javascript example

Visitor's IP Address = 
<script language="javascript">
var vip = "<!--#echo var="REMOTE_ADDR"-->"
document.write(vip)
</script>

Note: Javascript uses essentially the same as method #1, except the IP string is assigned to a variable and then written to the document.

Method #3:  This is the only method I'm aware of that you can display the Visitor's IP address in an htm or html file.  Even then your server needs to support Perl script.
Visitor's IP Address =

This method depends on your server's image processing to have access to server side variables.  If your server supports Perl it will probably run this script correctly.

HTML example

HTML Code:

 <b> Visitor's IP address =</b>
 <img src="cgi-bin/img_ip.pl" border="0" height="16" width="120">

Perl Code:

#!/usr/bin/perl --
   $isInverse = 0;        # 1 = digits are white on black
                          # 0 = black on white
   # bitmap for each digit
   #  Each digit is 8 pixels wide x 10 pixels high 
   #  Bytes are arranged top - to - bottom
   #  @invdigits are white on black, @digits black on white
   @invdigits = ("c3 99 99 99 99 99 99 99 99 c3",  # 0
                 "cf c7 cf cf cf cf cf cf cf c7",  # 1
                 "c3 99 9f 9f cf e7 f3 f9 f9 81",  # 2
                 "c3 99 9f 9f c7 9f 9f 9f 99 c3",  # 3
                 "cf cf c7 c7 cb cb cd 81 cf 87",  # 4
                 "81 f9 f9 f9 c1 9f 9f 9f 99 c3",  # 5
                 "c7 f3 f9 f9 c1 99 99 99 99 c3",  # 6
                 "81 99 9f 9f cf cf e7 e7 f3 f3",  # 7
                 "c3 99 99 99 c3 99 99 99 99 c3",  # 8
                 "c3 99 99 99 99 83 9f 9f cf e3",  # 9
                 "ff ff ff ff ff ff ff ff c7 c7"); # .
  
      @digits = ("3c 66 66 66 66 66 66 66 66 3c",  # 0
                 "30 38 30 30 30 30 30 30 30 30",  # 1
                 "3c 66 60 60 30 18 0c 06 06 7e",  # 2
                 "3c 66 60 60 38 60 60 60 66 3c",  # 3
                 "30 30 38 38 34 34 32 7e 30 78",  # 4
                 "7e 06 06 06 3e 60 60 60 66 3c",  # 5
                 "38 0c 06 06 3e 66 66 66 66 3c",  # 6
                 "7e 66 60 60 30 30 18 18 0c 0c",  # 7
                 "3c 66 66 66 3c 66 66 66 66 3c",  # 8
                 "3c 66 66 66 66 7c 60 60 30 1c",  # 9
                 "00 00 00 00 00 00 00 00 18 18"); # .
$rows = 16;					# Graphic has 16 rows
$col = 15;					# Graphic row is 15 bytes long (120 pixels)
$char = 10;					# Graphic charactor is 10 pixels high
$last = "0";					# Keep track of last "dot" position
@octs = ();					# @octs holds the four octets of the IP address
@dp = ();					# @dp holds the three "dot" position in the sring
@nibbs = ();					# @nibbs holds nibbles
@bytes = ();					# @bytes holds the graphic bytes
$host = $ENV{REMOTE_ADDR};			# Get visitor IP
$len = length($host);				# Determine length of IP string
$pad = (12 - ($len - 3));			# Calculate bytes to pad out
#collect the octets and find the dots
for ($x=0; $x < $len; $x++) {			# Loop through all charactors in $host string
  $digit = substr($host,$x,1);			# Process one digit at a time
  if ($digit eq '.') {				# When you find a "dot"
    $num = substr($host,$last,$x-$last);	# extract charactors from $last to "dot" position
    push(@octs, $num);				# push octet string into array
    push(@dp, $last);				# push "dot" position into array
    $last = $x+1;				# update $last
  }
}
    $num = substr($host,$last,$len-$last);	# no "dot# after last octet
    push(@octs, $num);				# push last octet string into array
# Graphic charactors are 8 x 10 pixels (Graphic total with padding 120 x 16)
# each octet varies from 8 to 24 pixels wide (1 - 3 charactors)
# starting at the upper left corner bytes are written across
# Build Byte Array
   # add three blank rows to each digit on top
   for ($i = 0; $i < ($col * 3); $i++ ) {
      if ($isInverse) {
         push(@bytes,"ff");
      }
      else {
         push(@bytes,"00");
      }
   }
   # build @bytes array, 10 lines ($char) in the middle
   for ($y=0; $y < $char; $y++) {			# 10 lines
      for ($x=0; $x < $len; $x++) {			# go through entire host string
          $digit = substr($host,$x,1);			# one charactor at a time
          if ($digit eq '.') {				# When you find a "dot"
             if ($isInverse) {             		# $inv = 1 for inverted text
                 $byte = substr(@invdigits[10],$y*3,2);	# dotes are on line 10
             } 
             else {
                 $byte = substr(@digits[10],$y*3,2);	# dots are on line 10
             }
          }
          else {
             if ($isInverse) {             		# $inv = 1 for inverted text
                $byte = substr(@invdigits[$digit],$y*3,2);
             } 
             else {
                $byte = substr(@digits[$digit],$y*3,2);
             }
           }
           push(@bytes, $byte);
      }
      for ($i=0; $i < $pad; $i++) {			# pad out remainder of graphic
         if ($isInverse) {             			# $inv = 1 for inverted text
            push(@bytes, "ff");
         } 
         else {
            push(@bytes, "00");
         }
      }
   }
   # add three blank rows to each digit on bottom
   for ($i = 0; $i < ($col * 3); $i++ ) {
      if ($isInverse) {
         push(@bytes,"ff");
      }
      else {
         push(@bytes,"00");
      }
   }
# Output Graphic byte array
   print ("Content-type: image/x-xbitmap\n\n");
   printf ("#define count_width 120\n#define count_height 16\n");
   printf STDOUT "static char count_bits[] = {\n";
   for($i = 0; $i < ($#bytes + 1); $i++) {
      print("0x$bytes[$i]");
      if ($i != $#bytes) {
         print(",");
         if (($i+1) % 15 == 0) {
            print("\n");
         }
      }
   }
   print("};\n");
exit(0);

Note: IP address is converted to graphic image

 

If you need help with a website problem please post your issue on our Electronic Help Forum

Go Home

Hits Counter