Skip to content

Instantly share code, notes, and snippets.

@parzibyte
Created February 16, 2021 20:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save parzibyte/cf105c5a763da9ddab6078562d3ee93f to your computer and use it in GitHub Desktop.
Save parzibyte/cf105c5a763da9ddab6078562d3ee93f to your computer and use it in GitHub Desktop.
<?php
/*
El contador de visitas más simple
https://parzibyte.me/blog
*/
$nombreArchivo = "contador.txt";
# Si no existe, lo creamos
if (!file_exists($nombreArchivo)) {
touch($nombreArchivo);
}
# Obtenemos su contenido
$contenido = trim(file_get_contents($nombreArchivo));
# Si está vacío, lo igualamos a cero
if ($contenido == "") {
$visitas = 0;
} else {
# Si no, las visitas son lo que tenga el archivo
$visitas = intval($contenido);
}
# Ya sea que las visitas son 0 o las que hayamos leído, las aumentamos
$visitas++;
# Y volvemos a escribir el valor en el archivo
file_put_contents($nombreArchivo, $visitas);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment