<?php
    //LIVE SETTINGS
    $SERVER_IP 		= 'localhost';
    $DB_DBNAME		= 'lavanaa1_webhits';
    $DB_USERNAME 	= 'lavanaa1_yannick';
    $DB_PASSWD 		= 'password';

    $dbLink = mysql_connect($SERVER_IP, $DB_USERNAME,$DB_PASSWD);

    mysql_select_db($DB_DBNAME);

	function getCountSite($siteid)
	{
		$dataSet = mysql_query("SELECT count(unid) as counthits FROM urlhits where siteid='" . mysql_real_escape_string($siteid) .  "' ");
    		$stringBack = 0;

		$row = mysql_fetch_row($dataSet);

		$stringBack = $row[0];

		return $stringBack;
	}

	function addCount($siteid, $referer, $datetime, $ip)
	{
		mysql_query("INSERT INTO urlhits (siteid,url,datetime,referer,clientip) values (" . mysql_real_escape_string($siteid) . ",'','" . $datetime . "','" . $referer . "','" . $ip . "')");

	}

$count=getCountSite($_REQUEST['siteid'] );
$imageWidth=12;
if ( $count > 9 ){
  $imageWidth=26;
}
if ( $count > 99 ){
  $imageWidth=40;
}
if ( $count > 999 ){
  $imageWidth=60;
}
if ( $count > 9999 ){
  $imageWidth=80;
}
if ( $count > 99999 ){
  $imageWidth=100;
}

$referer=$_SERVER['HTTP_REFERER'];
$visitingIP=$_SERVER['REMOTE_ADDR'];
$currentTime= getDate();

$my_img = imagecreate( $imageWidth, 14 );
$background = imagecolorallocate( $my_img, 255, 255, 255 );
$text_colour = imagecolorallocate( $my_img, 0, 0, 0 );
imagecolortransparent($my_img, $background);
//$line_colour = imagecolorallocate( $my_img, 128, 255, 0 );
imagestring( $my_img, 12, 1, 1, '' . $count,  $text_colour );
//imagestring( $my_img, 14, 10, 12, 'count:'  ,  $text_colour );
imagesetthickness ( $my_img, 5 );
//imageline( $my_img, 30, 45, 165, 45, $line_colour );
addCount($_REQUEST['siteid'], $referer, date('Y-m-d H:i:s'), $visitingIP);
header( "Content-type: image/png" );
imagepng( $my_img );
#imagecolordeallocate( $line_color );
imagecolordeallocate( $text_color );
imagecolordeallocate( $background );
imagedestroy( $my_img );
?>
