why does this simple counter fail?

Ryan Coleman editor at d3photography.com
Thu Mar 24 21:14:27 UTC 2011


Here's a quick and dirty option...
FIRST make sure your permissions on the folder you want to write the countfile to is either at RWX to all or is owned by the Apache run user (PHP by default runs under the Apache Service user).

I ran this in file test.php on my server. Give it a whirl.

<?
$dir = "/path/to/stored/counts/";
$dir = $_SERVER['DOCUMENT_ROOT']."test/"; //This was to test in "test" directory in VHOST root folder.
$file = $_SERVER['PHP_SELF'].".txt"; //this keeps it from being loaded in browser as a php-executable
#$file = urlencode($_SERVER['REQUEST_URI']).".txt"
	// 	This option if uncommented will make /file/path/filename.php?that=this&five=5 
	//	turn into "%2Ffile%2Fpath%2Ffilename.php%3Fthat%3Dthis%26five%3D5". Not pretty but functional.

if(!is_file($dir.$file)) {
	$dump = fopen($dir.$file, "x+");
	fclose($dump);
}
//Read current value
$fp_read = fopen($dir.$file, "r");
$count = fread($fp_read, filesize($dir.$file)+1);
fclose($fp_read);

//Convert count to integer
$new_count = ((int)$count);
//Increase count by 1
$new_count++;


//Reopen to write new value
$fp = fopen($dir.$file, "w+");
fwrite($fp, $new_count);
fclose($fp);

echo "A count was added. It was #".$new_count;
?>

On Mar 24, 2011, at 1:53 PM, Gary Kline wrote:

> On Wed, Mar 23, 2011 at 11:47:16AM -0500, Ryan Coleman wrote:
>> Do you have an error for it?
>> 
>> If not... add after the first <?
>> error_reporting(9);
>> 
>> And see what it reports.
>> 
>> --
>> Ryan
>> PHP dev.
>> 
> 
> 	save the bandwidth...
> 
> 
> Ok, i added the error_reporting line to both scripts.  No change
> from the count.php, and the same output as prev from my script that
> tries to pick a random entry from some 70 quotes.  here is what the
> randomquote.php scipt output onto the home page:
> 
> 
> 
> 
> Last updated:
> 17 February, 2011
> 
> echo "err-9 line below:\n"; $number-1){ // If ran out of quotes,
> start again! $num=0; } if (file_exists($directory.$quotecountfile))
> { $nu = fopen ($directory.$quotecountfile, "w"); fputs($nu,$num); }
> else { die("Cant Find $quotecountfile"); } } ?>
> 
> 
> Note that i added the echo line just now.  
> 
> Having a quote isn't as meaningful as giving users the latest
> pagecount.  That still fails without any errors.  
> 
> gary
> 
> 



More information about the freebsd-questions mailing list