Number of visits counter - real solution

<?php

header('Content-Type: image/jpeg');

mysql_connect('147.32.164.26','alik',(there should be the password))
   or die('chyba:'.mysql_error() );
mysql_select_db('alik');

//take the GET parameter:
$klic = $_GET['k'];
// if there are no parameter, take "1":
if ($klic<1) 
{
$klic=1;
}
;

// make a cookie to mark, that we were here
// (important, if user press reload - it should not count again)
$cname = 'arii_fs_cvut_pocitadlo_'.$klic;

//initialization of working variable (program cannot write any message, not even varning - because a picture)
$inc_cnt=1;

//list through cookies and try to find
// (the test "if ($_COOKIE[$cname]) ..." would display warning, so cannot be used at all )
foreach($_COOKIE as $n1 => $v1) 
  {   
  if ($n1 == $cname )
        {$inc_cnt=0; }; };

// no active cookie - set a new one (holding time 3333 s is less then hour)
if ($inc_cnt==1)
  { setcookie($cname,"www.fs.cvut.cz/prt/web/cv/cv9e.htm",time()+3333 ); $inc_cnt=1; } ;

//reading data from the database:
$result=mysql_query('select max(cislo) as minuly from pocitadlo3 where klic='.$klic.';');
$cislo=intval(mysql_result($result,0,"minuly"));
                         

//if needed, increasing the counter:
$cislo+=$inc_cnt;

//write it back - even, if not counted; we keep record of use (by the ip address saving)
$q= 'insert into pocitadlo3 values ('.$cislo.','.$klic.',"'. $_SERVER["REMOTE_ADDR"].'",now());';
$result=mysql_query($q);
mysql_close();     

//still not solved:
//two coincidental access
//if user reload after other access the page, it will show the new value;
//the solution would be to save this value to the cookie

$src = @imagecreatefromjpeg('0123456789.jpg');
$dst = imagecreatetruecolor(25*4, 39);

for ($i=3; $i>=0; $i--) {
  $c = $cislo % 10;
  $cislo = ($cislo - $c) / 10;
  imagecopy($dst, $src, $i*25, 0, $c*25, 0, 25, 39);
}

imagejpeg($dst);

imagedestroy($dst);

imagedestroy($src);

?>