Linux/PHP question

ref tbs-Gb/NUjX2UK8 at public.gmane.org
Tue Nov 4 11:46:57 EST 2008


Hi Myrle,

as some people have pointed out, there are a couple of problems with
your program. The code I have a stuck at the bottom of this email fixes
most/all of them and gives you a working program. It may be hard to read
with line wrap in the email, but if you C&P it into a text editor it
will lok fine.

Your problem list is this ...

1. there is no file locking, so you will lose hit counts if there are
two 'hits' trying to write to the file at the same time.
2. You are reading the contents of the counter file before you connect
to it, so it could have changed by the time you add one to it and try to
write it again ..

here is a way to do it with locking, and 'read-after-lock' to ensure
that the values are correct. The nifty little trick here is to open the
file handle in READ mode, NOT write mode. This allows you to read the
file contents. the 'rewind' command resets the file handle pointer to
the beginning of the file before you write the new hit counter value.

Notice, the locking loop will try to get a lock on the file 20 times
before it gives up. Obviously you can adjust this to suite your
taste ...

I ran this in a test environment (Debian Linux) and it worked fine ...

Good luck :

----------- php locking counter code -------------

<?php

/**
 * Program to safely lock and increment a hitcounter file
 * Written for Myrle
 * 
 */


$count_my_page = ("hitcounter.txt"); // designated log writing file
$lockedFile = false; 	// assume we dont have a lock to begin with
$maxAttempts = 20; 		// number of times to try and get a lock on the
file
$attempts = 0;     		// current attempts number

$fileHandle = fopen($count_my_page , "r+"); 		// open for reading only
while (($attempts < $maxAttempts) || $lockedFile)   // start of attempt
loop
{
	while (!($lockedFile))  				// loop until we get a lock
	{
		if (flock($fileHandle, LOCK_EX)) 	// attempt lock
		{
			// we got lock, get file contents
			$hits = (int) fgets($fileHandle,filesize($count_my_page));
			$hits += 1;  					// increment hit count
			rewind($fileHandle);
			fwrite($fileHandle, $hits); 	// write hit count to file
			flock($fileHandle, LOCK_UN); 	// release the lock
			$lockedFile = true; 			// set release flag
			fclose($fileHandle); 			// release the file handle
		}
		else
		{
			$lockedFile = false;			// we got no lock
		}
	}
	++$attempts;							// increment attempt counter
}

// show the results of the operation
if ($lockedFile)
{
	echo "hit count now $hits";
}
else 
{
	echo "could not lock file, hits not incremented";
}
?>



-----------------end of php code -----------------


HTH, good luck

Richard



On Mon, 2008-11-03 at 14:53 -0500, Myrle Francis wrote:
> HI,
> 
>  I created a simple counter for my website and it doesn't seem to be
> working. I copied it from a windows/apache installation on my local windows
> machine and it works there (just not on linux)
> 
> The file that contains the hit count hitcount.txt has the attributeof rw-
> rw- rw.
> 
> The program itself counter.php has the attribute of rwx r-x r-x
> 
> The  code is as followes:
> <?php
> $count_my_page = ("hitcounter.txt");
> $hits = file($count_my_page);
> $hits[0] ++;
> $fp = fopen($count_my_page , "w");
> fputs($fp , "$hits[0]");
> fclose($fp);
> echo $hits[0];
> ?>
> 
> could anyone please provide me with a clue as to what i'm doing wrong.
> I've written another simple pnp programs on this site (rootshell.be) and I
> know php works.
> 
> 
> Thank you
> Myrle
> 
> 
> -- 






More information about the Discuss mailing list