Contor PHP cu fisier text

Sa zicem ca avem o pagina de download si vrem sa monitorizam de cate ori a fost accesata acea pagina. Pentru un lucru atat de banal ne complicam cu stocarea datelor intr-o baza de date. Hai sa ne rezumam la un fisier DAT in care incrementam un numar.

<?php
if (file_exists('counter.dat'))
{
	$exist_file = fopen('counter.dat', 'r');
	$new_count = fgets($exist_file, 255);
	$new_count++;
	fclose($exist_file);
	
	echo $new_count;
	
	$exist_count = fopen('counter.dat', 'w');
	fputs($exist_count, $new_count);
	fclose($exist_count);
} 
else
{
	$new_file = fopen('counter.dat', 'w');
	fputs($new_file, '1');

	echo 1;

	fclose($new_file);
}

Ps. fisierul are nevoie de drepturi de scriere pe server.

4 thoughts on “Contor PHP cu fisier text”

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.