Jump to content

Issue With Text File Base Hit Counter


phpQuestioner

Recommended Posts

Is there a way some one; if they had no idea what the text file name was; could alter a text file? I have been using a php text file based hit counter and the text file will have like "2250" in it and then just all of a sudden; maybe a few days/weeks/months later;  the text file will have like a lot smaller number.

 

Here is the code I am using:

 

<?php
$fp = fopen ('1a3c5e.txt', 'r');
$now = fgets ($fp);
fclose ($fp);
if (empty ($_COOKIE['visit1a3c5e']))
{	
$fp = fopen ('1a3c5e.txt', 'w');	
$now = $now + 1;	
fwrite ($fp, $now);	
setcookie ('visit1a3c5e', 1, time()+315360000);
fclose($fp);
}
?>

Link to comment
Share on other sites

it probly the hits ur getting is to much for a textfile to handle try using mysql it free........

 

ignore this statement was stupid, i thort flat files didnt have the abilty to work

or act as fast as a real database and load server time was at fault sorry.......

Link to comment
Share on other sites

Your code is not bothering to check the status returned by the fopen() statement and when there are concurrent visitors, the file is locked by one and cannot be opened by another. This results in $now being a null value and you add 1 to it, causing your counter to start over. You then blindly write this incorrect value back to your file.

 

You need to check that the fopen() worked and only proceed with writing to the file when the read was successful.

 

An integer can hold 2,147,483,647. If you site was getting that many hits, you would know it. This has nothing to do with what a text file can handle.

Link to comment
Share on other sites

Your code is not bothering to check the status returned by the fopen() statement and when there are concurrent visitors, the file is locked by one and cannot be opened by another. This results in $now being a null value and you add 1 to it, causing your counter to start over. You then blindly write this incorrect value back to your file.

 

You need to check that the fopen() worked and only proceed with writing to the file when the read was successful.

 

An integer can hold 2,147,483,647. If you site was getting that many hits, you would know it. This has nothing to do with what a text file can handle.

 

that kind of does make sense; I am going to try this instead:

 

<?php

$fp = file_get_contents("1a3c5e.txt");
$now = $fp + 1;

if (empty ($_COOKIE['visit1a3c5e']))
{
$fp = fopen ('1a3c5e.txt', 'w');
fwrite ($fp, $now);
setcookie ('visit1a3c5e', 1, time()+315360000);
fclose($fp);
}

?>

 

 

it probly the hits ur getting is to much for a textfile to handle try using mysql it free........

 

that may be what I have to do; if this does not work

Link to comment
Share on other sites

quick go lol probly wrong theo

<?php
$file = 'hits.dat'; // File where data is stored
$fp = fopen($file, "r");  // Open file as read only
$count = fread ($fp, filesize($file)); // Read hits
fclose($fp); // Close file
$count++; // Add 1 to the count
$fp = fopen($file, "w"); // Open file for writing
fwrite($fp, $count); // Write the count
fclose($fp); // Close file
echo "Load $count times";
?>

Link to comment
Share on other sites

file_get_contents() returns a FALSE if it fails to read the file, resulting in the same problem.

 

From the file_get_contents in the php manual -

 

On failure, file_get_contents() will return FALSE.

 

Your code must check that you actually read the value before blindly incrementing it and writing it back out.

Link to comment
Share on other sites

redarrow, I am going to try my text file version first and if that does not work; I will try your version - thanks.

 

 

thank you to all of you for your input. :)

 

file_get_contents() returns a FALSE if it fails to read the file, resulting in the same problem.

 

From the file_get_contents in the php manual -

 

On failure, file_get_contents() will return FALSE.

 

Your code must check that you actually read the value before blindly incrementing it and writing it back out.

 

 

if the file exist and has a value; why would it return false? I guess I could use file_exists() to make sure the file is there and I could use a if condition to check and make sure $fp variable (the file_get_contents variable) value is not null.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.