0

So. I spent a lot of time working out the logistics of a special PHP hit counter. I spent lots of time, research, and hours of typing, however the script either doesn't work, or will return with an internal server error. My script is supposed to create a file path for the file, if non existent, then read and write the new count. I followed my code knowledge, then got tired and went to sourcing, then after finding a successful script proceeded to break it. Here is the code:

<?php

// Add correct path to your countlog.txt file.
$path = __DIR__ . '/PathTo/countlog.txt';

if (file_exists($path)) {} else {file_put_contents($path, "New")}

// Opens countlog.txt to read the number of hits.
$file  = fopen( $path, 'r' ) or die("Cannot read file");
$count = fgets( $file, 1000 );
fclose( $file );

// Update the count.
$count = abs( intval( $count ) ) + 1;

// Output the updated count.
echo "$count hits\n";

// Opens countlog.txt to change new hit number.
$file = fopen( $path, 'w' );
fwrite( $file, $count );
fclose( $file );
?>

If you can help me, I would be very grateful.

  • In what way is it broken? What's the original code that worked (and why were you trying to change it if ti worked, did you need additional functionality)? – El_Vanja Nov 24 '20 at 14:01
  • The original code was a test to see if I could write to files. For whatever reason when I run the code nothing shows up on screen. @El_Vanja – Super BUFF Meatballs Nov 24 '20 at 14:07
  • Does this answer your question? [PHP parse/syntax errors; and how to solve them](https://stackoverflow.com/questions/18050071/php-parse-syntax-errors-and-how-to-solve-them) – El_Vanja Nov 24 '20 at 14:16
  • I'd advise you to turn on error reporting. If you're unsure how to check for errors, see [this post](https://stackoverflow.com/questions/1053424/how-do-i-get-php-errors-to-display). – El_Vanja Nov 24 '20 at 14:16

1 Answers1

1

What exactly do you mean it "doesn't work or will return with an internal server error"? You haven't really said what your problem is. What is the error message? On which step does it fail -- creating the countlog.txt file and writing "New" if it doesn't exist? Giving the "Cannot read file" error on fopen(), or something after that?

I can take a few guesses:

  • Echo $path to see what file path it's actually trying to use. Does the PathTo folder exist? Do you have permissions to write to files in it? File_put_contents creates a filename that doesn't exist, but not a directory. You need to create the folder -- manually, or using mkdir() in your code (file_exists() or is_dir() can check whether the folder exists).
  • If the script creates your file and writes "New" in it, $count doesn't contain a number in the next step, which doesn't make much sense. Although intval() of an alphabetical string will usually return 0, that is unpredictable behavior. Logically, a hit counter should just start with the number 0 (or 1, if not incrementing again for the first hit) and increment from there. You also don't need to take the absolute value of a hit counter that starts at zero and only goes up.
  • Add some debugging code if you don't know where the script is failing. I.e, fgets() returns false if there's an error. You could have an if statement that says "Error on fgets" if it's false to narrow the problem down to that line.
  • For a web page hit counter, you probably want to output HTML formatting for the results. So echo <br> instead of \n for a line break.

More generally, your code is much longer than it needs to be and has extra file operations. You only need to read the file once, and then write to the file once. Something like this:

<?php

$path = __DIR__ . '\PathTo\countlog.txt';

if(file_exists($path))
{
    $count = file_get_contents($path);
}

else
{
    $count = 0;
}

$count++;

file_put_contents($path, $count);

echo "$count hit(s)<br>";
?>

There are ways this could be made shorter , such as using the ternary conditional operator instead of the long if-else statement, but this code is easier to understand. File_put_contents will produce a warning (not an error) if the file doesn't exist: you can suppress it with an @ symbol before the function call, or create the file using fopen() inside the else {} block.

brensnap
  • 129
  • 2