Number of visits counter - real solution


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

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

//take the GET parameter:
$klic = $_GET['k'];
// if there are no parameter, take "1":
if ($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)

//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,"",time()+3333 ); $inc_cnt=1; } ;

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

//if needed, increasing the counter:

//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());';

//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);