I am trying to make a unique hit counter. I "borrowed" some code from the web but just could not get it to work. (BTW I'm a self taught newbie hack.) Even with the help from the folks here I could NOT get it to work. So I started from scratch writing my own code re-learning the 'f' statements. I now have it so that it will read all of the ip addresses in the text file and compare each one to the users ip, and get out of the loop if it has already been entered.
Now I'm trying to open the count file, read the one entry, increment it by one and write it back to the file. But it just won't work. I found a couple more posts on here and tried what they suggested like the 'cmod' command and all of the possible numbers, but nothing. Then I found how to turn on better error reporting. (Something suggested before but I didn't know how to do it.) Now I get a bunch of errors and I 'think' there is something on the server that's not letting me write to the files. Here's the code I'm using, just for the increment part:
ini_set('display_errors', 'On');
error_reporting(E_ALL);
// read contents of count.txt
$handle = fopen($count_file, "r");
$old_count=fgets($handle);
echo "Old count = " . $old_count . "<br><br>";
fclose($handle);
// write contents of count.txt
chmod($count_file, 0777);
$fp = fopen($count_file, 'ab');
if (false === $fp) {
throw new RuntimeException('Unable to open log file for writing');
}
$handle = fopen($count_file, "w");
$new_count = $old_count +1;
echo "New count = " . $new_count;
fwrite($handle, $new_count);
fclose($handle);
The old count and new count display properly but the new one can't write to the txt file. Here are the error messages that now display:
Warning: chmod() [function.chmod]: Operation not permitted in /home/users/tecitout/counter/fullarray1.php on line 50
Warning: fopen(count.txt) [function.fopen]: failed to open stream: Permission denied in /home/users/tecitout/counter/fullarray1.php on line 51
Fatal error: Uncaught exception 'RuntimeException' with message 'Unable to open log file for writing' in /home/users/tecitout/counter/fullarray1.php:53 Stack trace: #0 {main} thrown in /home/users/tecitout/counter/fullarray1.php on line 53
I really don't understand the errors. Is this an issue with my buddy's server or am I that bad at writing code. I do appreciate the help.