home *** CD-ROM | disk | FTP | other *** search
/ H4CK3R 4 / hacker04 / 04_HACK04.ISO / src / PHP / php.exe / IP2Color.php < prev    next >
Encoding:
Text File  |  2001-07-02  |  2.3 KB  |  71 lines

  1. //**************************************
  2.     //     
  3.     // Name: IP2Color
  4.     // Description:A couple of functions tha
  5.     //     t convert an IP address into its color c
  6.     //     ode and not-color-code. Useful when view
  7.     //     ing an apache log with a mysql result gr
  8.     //     ouped by IP
  9.     // By: Fabien HADDADI
  10.     //
  11.     // Inputs:IP address
  12.     //
  13.     // Returns:formatted HTML with a colored
  14.     //     IP address on a contrasted background
  15.     //
  16.     // Assumes:A color is defined by the thr
  17.     //     ee components Red Green & Blue, to defin
  18.     //     e the color of IP address w.x.y.z, I tak
  19.     //     e w for red, x for green and y for blue,
  20.     //     the 'not-color' is the base color transl
  21.     //     ated by 128
  22.     //
  23.     // Side Effects:Some IPs produce a not s
  24.     //     o contrasted background, because the col
  25.     //     oring function could be improved
  26.     //
  27.     //This code is copyrighted and has    // limited warranties.Please see http://
  28.     //     www.Planet-Source-Code.com/xq/ASP/txtCod
  29.     //     eId.319/lngWId.8/qx/vb/scripts/ShowCode.
  30.     //     htm    //for details.    //**************************************
  31.     //     
  32.     
  33.     <? 
  34.     /***********************************/ 
  35.     /*this function converts $ipaddr into a color string*/ 
  36.     function IP2Color($ipaddr) { 
  37.     $pieces=explode(".",$ipaddr); 
  38.     $color=""; 
  39.     for($i=0;$i<3;$i++) { 
  40.     if(($pieces[$i]>=0) && ($pieces[$i]<=255)) { 
  41.     $color.=dechex($pieces[$i]); 
  42.     } 
  43.     } 
  44.     $color=substr($color."000000",0,6); 
  45.     return("#".strtoupper($color)); 
  46.     } 
  47.     /***********************************/ 
  48.     /*this function converts $ipaddr 
  49.     to anti color string*/ 
  50.     function IP2NotColor($ipaddr) { 
  51.     $pieces=explode(".",$ipaddr); 
  52.     $color=""; 
  53.     for($i=0;$i<3;$i++) { 
  54.     if(($pieces[$i]>=0) && ($pieces[$i]<=255)) { 
  55.     $color.=dechex((128+$pieces[$i]) % 255); 
  56.     } 
  57.     } 
  58.     $color=substr($color."000000",0,6); 
  59.     return("#".strtoupper($color)); 
  60.     } 
  61.     ======== 
  62.     Example: 
  63.     ======== 
  64.     $IP=getenv("REMOTE_ADDR"); 
  65.     print "<table><tr>\n"; 
  66.     print "<td bgcolor='".IP2Color($IP)."'><font face='Arial,Courier' 
  67.     color='".IP2NotColor($IP)."'>$IP</font></td>\n"; 
  68.     print "</tr></table>\n"; 
  69.     ?> 
  70.  
  71.