<?php
// config.php
$host = "localhost";
$db   = "tuo_database";
$user = "tuo_utente";
$pass = "tua_password";

try {
    $pdo = new PDO("mysql:host=$host;dbname=$db;charset=utf8mb4", $user, $pass, [
        PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
    ]);

    // Incremento ATOMICO - MySQL lo fa lui, impossibile perderlo
    $pdo->exec("UPDATE counter SET visits = visits + 1 WHERE id = 1");

    // Leggo il valore nuovo
    $stmt = $pdo->query("SELECT visits FROM counter WHERE id = 1");
    $count = (int)$stmt->fetchColumn();

} catch (PDOException $e) {
    // se DB giù, non rompo la pagina
    error_log("Counter error: " . $e->getMessage());
    $count = 0;
}

// Visualizzazione con immagini come prima
$display = (string)$count;
for ($i = 0; $i <= 9; $i++) {
    $display = str_replace((string)$i, "<img src=\"images/counter/$i.gif\" alt=\"$i\">", $display);
}

echo $display;
?>
