473,403 Members | 2,222 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,403 software developers and data experts.

Weird incrementing problem

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
6 1682
> 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
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
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
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

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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Mothra | last post by:
Here's what I'm trying to do (kill off old Unix logins): --------------------- $i=0; while (<$who>) { chomp($_); my @line = split(/\s+/, $_); # Split it into an array next unless ($line...
4
by: Sahil Malik [MVP] | last post by:
Okay so lets say I have a valuetype - lets say DateTime. Look at this code . List<DateTime> dt = new List<DateTime>() ; dt.Add(new dateTime(1999,12,1)) dt.AddDays(1) ; <--- This statement...
4
by: Peter Afonin | last post by:
Hello, I have a weirdest issue I've ever had. I have a function that enters some data into the Oracle table and returns the sequential row number for the new record (autonumber): Private...
10
by: pozz | last post by:
Hi all, I need to write a simple incrementing/decrementing function like this: unsigned char change( unsigned char x, unsigned char min, unsigned char max, signed char d); x is the value...
5
by: jerry | last post by:
I need to modify the code of a command-line tool. The source code starts out like this: int main(int argc, char *argv) { int ch, A, B ; while ((ch = getopt(argc, argv, "AB")) != -1)...
3
by: Adam Sandler | last post by:
Hello, I'm able to reproduce my problem but I haven't been able to figure out why it is happening. MS does have an article about such behavior in...
53
by: subramanian100in | last post by:
I saw this question from www.brainbench.com void *ptr; myStruct myArray; ptr = myArray; Which of the following is the correct way to increment the variable "ptr"? Choice 1 ptr = ptr +...
4
by: parez | last post by:
Hi, GetNextSequenceNumber2 worked for me in a different class.. but now it does not work.. it does not increment.. will it not get incremented after the return?
9
by: subramanian100in | last post by:
The following portion is from c-faq.com - comp.lang.c FAQ list · Question 6.13 int a1 = {0, 1, 2}; int a2 = {{3, 4, 5}, {6, 7, 8}}; int *ip; /* pointer to int */ int (*ap); /* pointer to...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.