Weird incrementing problem

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Brian

    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("lastquot e.cfg", "r");
    $data = fread($fp, 10);
    fclose($fp);
    echo "Last quote: ".$data."<b r>";

    $qid = $data + 1;
    $fp = fopen("lastquot e.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?
  • Micha³ Wo¼niak

    #2
    Re: Weird incrementing problem

    > But on my production server, the number will magically be incremented[color=blue]
    > 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?[/color]

    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

    Comment

    • rovisoft

      #3
      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


      -----------------------------------
      http://www.DevPlug.com -- Connecting Developers

      Comment

      • Simon Stewart no spam

        #4
        Re: Weird incrementing problem

        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" <ThisIsNotMyRea l@ddress.com> wrote in message
        news:Xns9629E20 F59B43nonenonec om@24.93.43.121 ...[color=blue]
        > 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("lastquot e.cfg", "r");
        > $data = fread($fp, 10);
        > fclose($fp);
        > echo "Last quote: ".$data."<b r>";
        >
        > $qid = $data + 1;
        > $fp = fopen("lastquot e.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?[/color]


        Comment

        • Brian

          #5
          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
          problem?



          [color=blue]
          > 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
          >[/color]

          Comment

          • Ken Robinson

            #6
            Re: Weird incrementing problem


            Brian wrote:[color=blue]
            > Aha! That's what's going on. I used mail() to send me an email when[/color]
            it[color=blue]
            > runs, and I got two of them.
            >
            > But why would the code snippet I posted earlier, put into a[/color]
            non-public PHP[color=blue]
            > file still run twice? Does it sound like an Apache or PHP[/color]
            configuration[color=blue]
            > problem?
            >[/color]

            How is the script invoked?

            Ken

            Comment

            • Brian

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



              "Ken Robinson" <kenrbnsn@rbnsn .com> wrote in news:1112288578 .518770.10090
              @g14g2000cwa.go oglegroups.com:
              [color=blue]
              >
              > Brian wrote:[color=green]
              >> Aha! That's what's going on. I used mail() to send me an email when[/color]
              > it[color=green]
              >> runs, and I got two of them.
              >>
              >> But why would the code snippet I posted earlier, put into a[/color]
              > non-public PHP[color=green]
              >> file still run twice? Does it sound like an Apache or PHP[/color]
              > configuration[color=green]
              >> problem?
              >>[/color]
              >
              > How is the script invoked?
              >
              > Ken
              >
              >[/color]

              Comment

              Working...