By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,223 Members | 1,434 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,223 IT Pros & Developers. It's quick & easy.

Weird incrementing problem

P: n/a
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.

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

$qid = $data + 1;
$fp = fopen("lastquote.cfg", "w");
echo "<br>".$qid;
fwrite($fp, $qid);
fclose($fp);
?>
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?
Jul 17 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
> 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?


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...
Mike
Jul 17 '05 #2

P: n/a
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
-----------------------------------
http://www.DevPlug.com -- Connecting Developers
Jul 17 '05 #3

P: n/a
Brian
I would be inclined to look at
a. ensuring $data is cast to an integer on reading (see php manual for
cast)
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

"Brian" <Th*************@ddress.com> wrote in message
news:Xn*************************@24.93.43.121...
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.

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

$qid = $data + 1;
$fp = fopen("lastquote.cfg", "w");
echo "<br>".$qid;
fwrite($fp, $qid);
fclose($fp);
?>
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?

Jul 17 '05 #4

P: n/a
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
problem?

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...
Mike


Jul 17 '05 #5

P: n/a

Brian wrote:
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 problem?


How is the script invoked?

Ken

Jul 17 '05 #6

P: n/a
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.

"Ken Robinson" <ke******@rbnsn.com> wrote in news:1112288578.518770.10090
@g14g2000cwa.googlegroups.com:

Brian wrote:
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
problem?


How is the script invoked?

Ken


Jul 17 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.