Reputation: 103
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.
Upvotes: 0
Views: 1370
Reputation: 103
Gentleman, Thanks for all your help. I copied all the files over to the web space I rent and found success. Unfortunately the impressive code offered by AbraCadaver read and incremented the number in the file but it neither wrote the new number or gave me any errors. However the poorly scripted code I wrote up above does in fact work on my server. The whole problem was in fact permissions on the server. At least I got help before I had pulled all my hair out!
Upvotes: 0
Reputation: 6593
If you want to write to a file, you need to have write permissions on it. Also, to chmod a file, you need to either own it, or be logged in as root.
So your error messages, in order:
Warning: chmod() [function.chmod]: Operation not permitted in /home/users/tecitout/counter/fullarray1.php on line 50
corresponds to chmod($count_file, 0777);
. It would seem that the user that php is running under does not own the file.
Warning: fopen(count.txt) [function.fopen]: failed to open stream: Permission denied in /home/users/tecitout/counter/fullarray1.php on line 51
This corresponds to the $fp = fopen($count_file, 'ab');
and means that the file is not writeable, because the chmod did not work.
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
is because of the if (false === $fp)
and is expected, given the above.
Upvotes: 1
Reputation: 79024
Delete count.txt
and as long as the webserver has permissions to write to the directory where you want to put count.txt
, just do this:
$c = 0;
$count_file = 'count.txt';
if(file_exists($count_file)) {
$c = (int)file_get_contents($count_file);
echo "Old count = $c<br><br>";
}
file_put_contents($count_file, $c++);
echo "New count = $c<br><br>";
Upvotes: 0