Mega Code Archive

 
Categories / Php / MySQL Database
 

A couple of functions that convert an IP address into its color code

and not-color-code. Useful when viewing an apache log with a mysql result grouped by IP <? /*this function converts $ipaddr into a color string*/ function IP2Color($ipaddr) { $pieces=explode(".",$ipaddr); $color=""; for($i=0;$i<3;$i++) { if(($pieces[$i]>=0) && ($pieces[$i]<=255)) { $color.=dechex($pieces[$i]); } } $color=substr($color."000000",0,6); return("#".strtoupper($color)); } /*this function converts $ipaddr to anti color string*/ function IP2NotColor($ipaddr) { $pieces=explode(".",$ipaddr); $color=""; for($i=0;$i<3;$i++) { if(($pieces[$i]>=0) && ($pieces[$i]<=255)) { $color.=dechex((128+$pieces[$i]) % 255); } } $color=substr($color."000000",0,6); return("#".strtoupper($color)); } Example: $IP=getenv("REMOTE_ADDR"); print "<table><tr>\n"; print " <td bgcolor='".IP2Color($IP)."'><font face='Arial,Courier' color='".IP2NotColor($IP)."'>$IP</font></td>\n"; print "</tr></table>\n"; ?>