Hi there,
I'm writing a PHP counter and am having some trouble getting it to work!
Here is my code stored in counter.php:
$logpath = ("counter.txt");
$pad = 5;
if (file_exists($logpath)) {
$count = file_get_contents($logpath);
}else{
$count = 0;
}
$count++;
$file = fopen($logpath, "w"); // the 'w' indicates writing mode
if (flock($file, LOCK_EX)) { // do an exclusive lock
fwrite($file, $count); // write out the updated count
flock($file, LOCK_UN); // release the lock
}
fclose($file);
$count = str_pad($count, 5, '0', STR_PAD_LEFT);
echo $count;
The counter.txt file simply contains 0
The script used to call the script is on the index.php page:
<?php include_once("includes/phpcounter/counter.php"); ?>
When sent to the server and tested, I get these replies:
Warning: fopen(counter.txt): failed to open stream: Permission denied in /home/public_html/TEST/includes/phpcounter/counter.php on line 30
Warning: flock(): supplied argument is not a valid stream resource in /home/public_html/TEST/includes/phpcounter/counter.php on line 31
Warning: fclose(): supplied argument is not a valid stream resource in /home/public_html/TEST/includes/phpcounter/counter.php on line 35
00001
As you can see, THEN the warnings are followed by what I want to see - the counter!
Whoever can help, I'd be eternally grateful!
Thank you!
Best,
rach