Hello guys!


I want to make a viewer function that adds up everytime the script runs.

I want to do it with a text file, something like this i guess..

$fp = fopen("video1.txt", 'a');

        ///I guess i need to get the current value of the file, then add an additional view point somehow.
	fwrite($fp, "What do i do here?  +1 on what?");
	fclose($fp);

I would love you for a thousand years if u wrote this little thing as it should.

/Regards, Sorcher

Dani AI

Generated

Both and offered simple, workable file-based solutions for incrementing a view counter. Those approaches are acceptable for personal or very low-traffic sites. The main risk is lost increments under concurrent requests: if two processes read and write at the same time, one update can overwrite the other. For reliable behavior, either use proper file locking or switch to a lightweight datastore that handles concurrency safely.

A compact, robust option is SQLite via PDO: it is file-based (no separate DB server), ACID-compliant, and supports atomic UPDATE statements. The example below shows creating a small table, ensuring a row exists, atomically incrementing the counter, and returning the new value.

<?php
$pdo = new PDO('sqlite:/full/path/to/views.sqlite');
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

$pdo->exec('CREATE TABLE IF NOT EXISTS video_views (id TEXT PRIMARY KEY, views INTEGER NOT NULL)');

$key = 'video1';
$pdo->prepare('INSERT OR IGNORE INTO video_views (id, views) VALUES (:id, 0)')->execute([':id' => $key]);
$pdo->prepare('UPDATE video_views SET views = views + 1 WHERE id = :id')->execute([':id' => $key]);
echo (int) $pdo->prepare('SELECT views FROM video_views WHERE id = :id')->execute([':id' => $key]) ?: 0;

Notes: use an absolute filesystem path and ensure the webserver user can write the file. For purely file-based counters, consult PHP file-locking behaviour to avoid race conditions (flock documentation). For the SQLite approach, see PDO SQLite details (PDO_SQLite reference). For high-traffic sites, consider an in-memory store (Redis/Memcached) or a proper RDBMS.

Recommended Answers

All 3 Replies

<?php function incrementCounter($counterFile) { //INCREMENT THE COUNTER
	$current=readCounter($counterFile);
	$current++;
	if(!$handle = fopen($counterFile, "w")) { return false; }
	else { if(fwrite($handle, $current) === FALSE) { return false; } }
	fclose($handle);
	return true; }
function readCounter($counterFile) { //OUTPUT THE CURRENT COUNT
	$contents=file_get_contents($counterFile);
	if(is_numeric((int)$contents)) { return ((int)$contents); }
	else { return 0; } }
$counterFile="video1.txt";
incrementCounter($counterFile); //INCREMENT THE COUNTER
echo readCounter($counterFile); //OUTPUT THE CURRENT COUNT
?>

There is another simpler way to do this:

$content = 'I want to add this line at the end of text file.';
$filePath = 'sometext.txt';

// THIS CODE FIRST OPENS THE FILE, APPENDS CONTENT TO IT AND THEN WRITES IT BACK
file_put_contents($filePath, file_get_contents($filePath) . $content);

Or in your case:

$filePath = 'sometext.txt';

// THIS CODE FIRST OPENS THE FILE, APPENDS CONTENT TO IT AND THEN WRITES IT BACK
file_put_contents($filePath, intval(file_get_contents($filePath)) + 1);

Thanks guys! I'll try both options and flag as solved as soon as i got it working, peace

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.