0

I'm programming a visitcounter for my website...

The textfile should look like this:

  • index.php: 4 views
  • contact.php: 6
  • views etc.

Here is my code:

function set_cookie(){ 
    setcookie("counter", "Don't delete this cookie!", time()+600); 
}

    function count_views(){
        $page         = basename($_SERVER['PHP_SELF']);
        $file         = fopen("counter.txt","r+");
        $page_found   = false;

        if (!isset($_COOKIE['counter'])) {
            while (!feof($file)) {
                $currentline = fgets($file);
                if(strpos($currentline, ":")){
                    $filecounter = explode(":", $currentline);
                    $pif = $filecounter[0]; $counterstand = $filecounter[1];
                    if ($pif == $page) {
                        $counterstand = intval($counterstand);
                        $counterstand++;
                        fseek($file, -1);
                        fwrite($file, $counterstand);
                        $page_found = true;
                        set_cookie();
                    }
                }
            }
            if (!$page_found) { fwrite($file, $page . ": 1\n"); }
            fclose($file);
        }
    }

And now my problem: Everytime i visit the page he is not able to update the new value. so at the end it looks like this

  • home.php: 1
  • index.php: 1
  • 2222

It looks like he takes the 1 from the correct line after the filename, and prints it at the end of the file...

how can I write the new value in the correct line?

ciaodarwin
  • 483
  • 3
  • 7
  • 22
  • try [this](http://stackoverflow.com/questions/7859840/php-fetching-a-txt-file-and-editing-a-single-line?rq=1) – Drixson Oseña Feb 20 '14 at 08:49
  • 1
    you can store in your texfile a json array which contains your data. each time store value in array and encode it in json and store in textfile. when you want to retrieve it. read file decode json in array and use it. – Satish Sharma Feb 20 '14 at 08:49
  • 1
    http://stackoverflow.com/questions/3004041/how-to-replace-a-particular-line-in-a-text-file-using-php http://stackoverflow.com/questions/12489033/php-modify-a-single-line-in-a-text-file http://stackoverflow.com/questions/18991843/replace-line-in-text-file-using-php http://www.dreamincode.net/forums/topic/207017-how-to-change-a-certain-line-of-a-text-file/ http://forums.devshed.com/php-development-5/how-to-change-specific-line-in-text-file-85294.html http://www.linuxquestions.org/questions/programming-9/php-read-file-line-by-line-and-change-a-specific-line-523519/ – ɹɐqʞɐ zoɹǝɟ Feb 20 '14 at 08:51

1 Answers1

0

this is another method to store your data in textfile which frequently changed.

function count_views(){
    $page = basename($_SERVER['PHP_SELF']);
    $filename = "counter.txt";


    if (!isset($_COOKIE['counter'])) 
    {
        $fh = fopen($filename, 'r+');
        $content = @fread($fh,filesize($filename));
        $arr_content = json_decode($content, true);

        if(isset($arr_content[$page]))
        {
            $arr_content[$page] = $arr_content[$page]+1;
        }
        else
        {
            $arr_content[$page] = 1;
        }

        $content = json_encode($arr_content);
        @ftruncate($fh, filesize($filename));
        @rewind($fh);
        fwrite($fh, $content);

    }
}

here we use a array which key is the page and the value is the counter.

and we store it in json_encode format in it.

whenever we want to update a particular page count. read the json written in file decode it in php array and update the count if page exists or assign 1 with new page if page index not exists in array.

then we again encode it in json and store it in textfile.

Satish Sharma
  • 9,547
  • 6
  • 29
  • 51