<?php
/* Sessie starten */
session_start();

/* Variabelen */
$max_clicks = 20; // Per "time_period"
$time_period = 60; // In seconden
$text = array(
  'Maximaal %d kliks per %d seconden! <span id="time_left">Nog %s wachten!</span>',
  'Je hebt al %d van de %d klikken per %d seconden gehad!',
  '<span id="timer">%d</span> seconden',
  ' heel even ',
  'Je hoeft niet meer te wachten!'
);

/* Checken of de clicks-sessie al bestaat, en of het tijd is voor een nieuwe */
if (!isset($_SESSION['clicks'])) {
    $_SESSION['clicks'] = array(time(), 0);
} else {
    if ($_SESSION['clicks'][0] + $time_period < time()) {
        $_SESSION['clicks'] = array($_SESSION['clicks'][0] + $time_period, 0);
    }
}

/* Teller verhogen */
$_SESSION['clicks'][1]++;

/* Controleren */
if ($_SESSION['clicks'][1] >= $max_clicks) {
    $wait_time = $_SESSION['clicks'][0] + $time_period - time();
    $echo = sprintf($text[0], $max_clicks, $time_period, ($wait_time <= 1 ? $text[3] : sprintf($text[2], $wait_time)));
    $show_script = true;
} else {
    $echo = sprintf($text[1], $_SESSION['clicks'][1], $max_clicks, $time_period);
    $show_script = false;
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<title>Klik limiet</title>
<?php if ($show_script) { ?>
<script type="text/javascript">
window.onload = timer;

function timer(go)
{
    if (go) {
        wait = document.getElementById('timer').innerHTML;
        if (wait <= 1) {
            document.getElementById('time_left').innerHTML = '<?php echo $text[4]; ?>';
        } else {
            document.getElementById('timer').innerHTML = wait - 1;
        }
    }
    setTimeout('timer(true)', 1000); 
}
</script>
<?php } ?>
</head>
<body>
<?php echo $echo . "\r\n"; ?>
</body>
</html>