SCRIPT CONTADOR NUMÉRICO DE VISITAS SIN BASE DATOS
Alternativa a Countapi-xyz
El siguiente contador de visitas utiliza solamente un código Javascript, un código PHP y un archivo de texto. Este contador de visitantes no se resetea (No se reinicia su conteo) al eliminar las cookies, es decir, cuando se refresca la página.
Este contador gratuito será una buena alternativa, como reemplazo de aquel código de "Countapi-xyz", utilizado hace unos años.
Puede ver el contador de visitas funcionando al final de esta página.
1- Código donde se mostrará el contador de visitas (Página HTML):
<body>
<!-- CONTADOR DE VISITAS -->
<p><span id="visitCount">Loading...</span> visitas a este sitio web</p>
<script src="counter.js"></script>
<!-- FIN CONTADOR DE VISITAS -->
</body>
2- Crear "counter.js" (Javascript):
// Fetch the visit count from the server
fetch('counter.php')
.then(response => response.json())
.then(data => {
document.getElementById('visitCount').textContent = data.visits;
})
.catch(error => {
console.error('Error fetching visit count:', error);
});
3- Crear "counter.php" (PHP):
<?php
// Path to the file that stores the visit count
$counterFile = 'counter.txt';
// Check if the counter file exists, if not create it with initial value 0
if (!file_exists($counterFile)) {
file_put_contents($counterFile, '0');
}
// Read the current count from the file
$count = (int)file_get_contents($counterFile);
// Increment the count
$count++;
// Write the updated count back to the file
file_put_contents($counterFile, $count);
// Return the visit count as a JSON response
header('Content-Type: application/json');
echo json_encode(['visits' => $count]);
?>
4- Crear "counter.txt" (Archivo de texto - Block de Notas):
Escribir 0 (Cero) o el número que desee como inicio del contador en este archivo.
GUARDAR con la extensión ".txt" y con la Codificación "UTF-8".
DEMO > Al final de esta página.
Script para contador de múltiples DESCARGAS sin base de datos.html