Solved

What's up with my counter?

Posted on 2002-06-24
5
201 Views
Last Modified: 2006-11-17
I'm trying to put a simple little invisible counter on my website that basically writes the hit number to a file.
For some reason, it will only increment to 1.  I think what's happening is that it always reads 0 from the file and then increments it resulting in 1.
I checked and the file (counter.txt) has full 777 permissions (which i'll fix later)

Can anyone help me out?

function updateCounter()
{
        // Check if the file exists
        if(!file_exists("counter.txt"))
                print "FILE doesn't exists";
 
        //Update counter
        $counterFile = fopen("counter.txt", 'w+');
        if($counterFile)
        {
                $chunk = fread($counterFile, 10); // Get the last counter value
                $chunk = intval($chunk);
//              print "CHUNK IS: $chunk<BR>";
                fclose($counterFile); // Close the file to rewind the pointer
                $counterFile = fopen("counter.txt", 'w+');
                if($counterFile)
                {
                        //print "CHUNK: $chunk<BR>";
                        $chunk = $chunk + 1; // Increment counter
                        fwrite($counterFile, $chunk); // Write the new value
                }
                else
                {
                        print "Did not update chunk value";
                }
//              print "Just changed the counter<BR>";
                return 0;
        }
        else
        {
//              print "failed to change the counter<BR>"; // It failed
                return -1;
        }
        fclose($counterFile); // Close the counter file
}
0
Comment
Question by:myuen
  • 3
5 Comments
 
LVL 7

Expert Comment

by:axis_img
ID: 7106408
Hi...

Your first fopen() is the problem. Change the 'w+' to 'r+', and it should work fine.

I will post a follow-up to explain in a little more detail, as well as give a modified version of your code as an example.

Regards,
Barry
0
 
LVL 7

Accepted Solution

by:
axis_img earned 50 total points
ID: 7106418
ok...

Here is some revised code. Give it a try and let me know if you have any questions...


function updateCounter()
{
     // Open counter file
     $counterFile = fopen("counter.txt", "r+");

     // Verify open
     if($counterFile)
     {
          // Read in counter value
          $chunk = fread($counterFile, 10);
          $chunk = intval($chunk);
          print "Counter Before: $chunk<BR>";

          // Rewind file pointer
          rewind($counterFile);

          // Increment counter
          $chunk++;
          print "Counter After: $chunk<BR>";

          // Write out new counter value
          fwrite($counterFile, $chunk);
          fclose($counterFile);
          return(0);     // Success
     }
     else {
          print "Unable to open counter file...<br>";
          return(-1);     // Error
     }
}




A few notes on the changes I made.

1.) The "w+" in the fopen() was changed to "r+". When you open a file in "w+", it automatically truncates the file. The + is supposed to specify that it can also read from the file, which it can, but not in the way you think. It truncates the file _BEFORE_ allowing you to read from it. I know it may seem a bit silly, but that is the way it works. Use "r+", so that you can read from the file to get it's original contents, as well as have the ability to write to the file.

2.) I removed the use of the second fopen(), as it saves resources to only use one fopen(). Use the rewind($counterFile) or fseek($counterFile, 0) function to reset the file pointer rather than closing and opening the file again.

http://www.php.net/manual/en/function.rewind.php
http://www.php.net/manual/en/function.fseek.php

That's about it... Everything else looks ok. Hope that helps.

Barry
0
 
LVL 40

Expert Comment

by:RQuadling
ID: 7106874
What about 2 simultaneous hits?

Using a DB is a MUCH safer option as you can get the query to retry until is suceeds in updating.

UPDATE CounterTable SET Counter = Counter + 1

Works for me.
0
 
LVL 7

Expert Comment

by:axis_img
ID: 7108201
That goes without saying. :)
0
 
LVL 1

Author Comment

by:myuen
ID: 7111587
Thanks.

I didn't know about that rewind stuff.
Later
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Both Easy and Powerful How easy is PHP? http://lmgtfy.com?q=how+easy+is+php (http://lmgtfy.com?q=how+easy+is+php)  Very easy.  It has been described as "a programming language even my grandmother can use." How powerful is PHP?  http://en.wikiped…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now