Weird incrementing problem

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View

Here's the example:

You have a world writable file called lastquote.cfg on your server with a
one or two digit number in it.  When you run the script below, it reads the
value, increments the value by one and then rewrites it into the file.

$fp = fopen("lastquote.cfg", "r");
$data = fread($fp, 10);
echo "Last quote: ".$data."<br>";

$qid = $data + 1;
$fp = fopen("lastquote.cfg", "w");
echo "<br>".$qid;
fwrite($fp, $qid);

Sounds simple, right?  My problem is that one one computer, this does
exactly as it should.  It increments the value and writes the file.  But on
my production server, the number will magically be incremented by two,
sometimes by one and I can't find an explanation.  The strange thing is
that if I have it echo what it's going to write into the file, it echos the
correct value.  But when it writes it, it's incorrect!  Any idea what the
hell is going on?

Re: Weird incrementing problem

Quoted text here. Click to load it

If I may take a wild guess, the script is somehow invoked twice. Have you
checked whether there is only a single connection to your webserver
(it's being used on a webserver, isn't it?) that reqests the page
invoking the script? Maybe a forgotten code snippet on some other page?

Perhaps you should try to change the script's name? Or, even better,
simply log each invocation (with the filename/command-line of the script
that, say, included this script) to some local file, so that you are
able to check whether or not it is called from the right pages?

As I said, just some wild guesses...

Re: Weird incrementing problem

Aha! That's what's going on.  I used mail() to send me an email when it
runs, and I got two of them.

But why would the code snippet I posted earlier, put into a non-public PHP
file still run twice?  Does it sound like an Apache or PHP configuration

Quoted text here. Click to load it

Re: Weird incrementing problem

Brian wrote:
Quoted text here. Click to load it
non-public PHP
Quoted text here. Click to load it

How is the script invoked?


Re: Weird incrementing problem

Through a web browser.

The only way I could get it from loading twice was to record the epoch that
it last incremented and if it loads again within one second, don't
increment again.  Apache has to be misconfigured somewhere because I've
never seen this behavior before.

Quoted text here. Click to load it

re:Weird incrementing problem

Yeah, i think you invoke the script twice somehow! Good idea, change
the name of it and the file it uses and run it again!

 Good Luck,Ovidiu

----------------------------------- -- Connecting Developers

Re: Weird incrementing problem

I would be inclined to look at
a.  ensuring $data is cast to an integer on reading (see php manual for
b.  ensure $qid is cast to a string before writing
c. write spaces into the file beyond $qid, so that on reading back you don't
pick up other characters.
b.  ensuring that each read or write is done from the right place  (do a
reset on file in both cases.

failure to do point b could explain why it works when you print out $qid,
because printing it will force it to be a string.

- S

Quoted text here. Click to load it

Site Timeline