when does a PHP script stop running?

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

    when does a PHP script stop running?

    Suppose I create dynamic web pages with 3 functions (which call other
    functions to make everything happen, but these 3 you might think of as
    being the top layer):

    registerSession s();
    sendHtmlToBrows ers();
    incrementPageVi ews();


    Is there any chance that incrementPageVi ews() will be executed? Or, to
    turn that question around, does anyone know a reason why it wouldn't?
    Does PHP stop executing when the last of the HTML is sent to the
    webbrowser (I've heard both yes and no on this newsgroup and I'm
    hoping for a definitive answer).

    Okay, now lets suppose that I do this (this is closer to what I
    actually do):

    $actionsToBeDon e[] = "registerSessio ns";
    $actionsToBeDon e[] = "sendHtmlToBrow sers";
    $actionsToBeDon e[] = "incrementPageV iews";

    for ($i=0; $i < count($actionsT oBeDone); $i++) {
    $function = $actionsToBeDon e[$i];
    if ($allowed = isFunctionAllow ed($function)) {
    $function();
    } else {
    echo "<h1> Someone tried to commit a function called $function
    which is not allowed";
    }
    }



    Given this situation, is there a reason why incrementPageVi ews would
    never execute?


    What I actually do is have 3 such arrays, $pageEventsStar t,
    $pageEventsMidd le, and $pageEventsEnd, and I have a single function
    that has 3 for loops, like the one you see above. I am not able to get
    any functions to fire if I put them into $pageEventsEnd, yet the same
    functions work fine if I put them into $pageEventsStar t.

    There are some calls to the database that should be done after all the
    HTML is sent. Otherwise they just slow things down. Incrementing how
    many times a page has been viewed, or deleting all sessions that are
    more than 20 minutes old, are the kind of clean up work that I'd like
    to put into $pageEventStart . But it seems like PHP is quitting before
    it gets to $pageEventsStar t. I find this shocking because it means
    that PHP is quitting in the middle of a function, without even
    finishing the function (because, as I say, all three for loops are in
    one function).

    The other possibility is that an error happens but that I don't see
    the error message because it is generated after all HTML is sent to
    the browser. Can anyone think of how I might find this error message,
    if it existed?



    Just so you get it, the master function that runs my software looks
    like this:



    function runMainLoop() {

    $pageEventsStar t = $GLOBALS["pageEventsStar t"];
    $pageEventsMidd le = $GLOBALS["pageEventsMidd le"];
    $pageEventsEnd = $GLOBALS["pageEvents End"];


    for ($i=0; $i < count($pageEven tsStart); $i++) {
    $function = $pageEventsStar t[$i];
    if ($allowed = isFunctionAllow ed($function)) {
    $function();
    } else {
    echo "<h1> Someone tried to commit a function called $function
    which is not allowed";
    }
    }

    for ($i=0; $i < count($pageEven tsMiddle); $i++) {
    $function = $pageEventsMidd le[$i];
    if ($allowed = isFunctionAllow ed($function)) {
    $function();
    } else {
    echo "<h1> Someone tried to commit a function called $function
    which is not allowed";
    }
    }

    for ($i=0; $i < count($pageEven tsEnd); $i++) {
    $function = $pageEventsEnd[$i];
    if ($allowed = isFunctionAllow ed($function)) {
    $function();
    } else {
    echo "<h1> Someone tried to commit a function called $function
    which is not allowed";
    }
    }



    }





    Does anyone see a reason why runMainLoop() would die before finishing?
    If there is an error, it is not in the functions that I'm trying to
    execute, because, as I say, when I move them from $pageEventsEnd to
    $pageEventsStar t, they work perfectly.
  • Guest's Avatar

    #2
    Re: when does a PHP script stop running?

    >The other possibility is that an error happens but that I don't see[color=blue]
    >the error message because it is generated after all HTML is sent to
    >the browser. Can anyone think of how I might find this error message,
    >if it existed?[/color]

    I have yet to find any good reason why anyone would not use error logging
    to a file, regardless of the other methods they choose to implement for
    error logging.

    On a production website, you would normally have display_errors set off, and
    your
    error logging to a file should be what you use to figure out what if any
    problems are
    occuring.

    _______________ _______________ _______________ ____
    Wil Moore III, MCP | Integrations Specialist | Senior Consultant


    Comment

    • lawrence

      #3
      Re: when does a PHP script stop running?

      <laidbak69@hotm ail.com> wrote in message[color=blue]
      > I have yet to find any good reason why anyone would not use error logging
      > to a file, regardless of the other methods they choose to implement for
      > error logging.
      >
      > On a production website, you would normally have display_errors set off, and
      > your
      > error logging to a file should be what you use to figure out what if any
      > problems are
      > occuring.[/color]

      Good point. My project isn't yet in production and has drifted from
      server to server in a discouraging way. In particular, its kept me
      from setting things up correctly on the server. But things are more
      stable now, so I should go ahead and set things up properly.

      Still, having said that, the question remains: when does a PHP script
      stop running.

      Comment

      • Pedro Graca

        #4
        Re: when does a PHP script stop running?

        lawrence wrote:[color=blue]
        > $actionsToBeDon e[] = "registerSessio ns";
        > $actionsToBeDon e[] = "sendHtmlToBrow sers";
        > $actionsToBeDon e[] = "incrementPageV iews";
        >
        > for ($i=0; $i < count($actionsT oBeDone); $i++) {
        > $function = $actionsToBeDon e[$i];
        > if ($allowed = isFunctionAllow ed($function)) {
        > $function();
        > } else {
        > echo "<h1> Someone tried to commit a function called $function
        > which is not allowed";
        > }
        > }[/color]


        Are you sure, *really* sure, that "registerSessio ns" went into
        $actionsToBeDon e[0]? And "sendHtmlToBrow sers" went into
        $actionsToBeDon e[1]? And "incrementPageV iews" went into
        $actionsToBeDon e[2]?

        If there is a 'hole' in the array the for() loop will fail miserably. Do
        a foreach loop instead:

        foreach ($actionsToBeDo ne as $function) {
        if ($allowed = isFunctionAllow ed($function)) {
        // ... etc ...
        --
        --= my mail box only accepts =--
        --= Content-Type: text/plain =--
        --= Size below 10001 bytes =--

        Comment

        • lawrence

          #5
          Re: when does a PHP script stop running?

          Pedro Graca <hexkid@hotpop. com> wrote in message news:<c27sip$1p pbv8$3@ID-203069.news.uni-berlin.de>...[color=blue]
          > lawrence wrote:[color=green]
          > > $actionsToBeDon e[] = "registerSessio ns";
          > > $actionsToBeDon e[] = "sendHtmlToBrow sers";
          > > $actionsToBeDon e[] = "incrementPageV iews";
          > >
          > > for ($i=0; $i < count($actionsT oBeDone); $i++) {
          > > $function = $actionsToBeDon e[$i];
          > > if ($allowed = isFunctionAllow ed($function)) {
          > > $function();
          > > } else {
          > > echo "<h1> Someone tried to commit a function called $function
          > > which is not allowed";
          > > }
          > > }[/color]
          >
          >
          > Are you sure, *really* sure, that "registerSessio ns" went into
          > $actionsToBeDon e[0]? And "sendHtmlToBrow sers" went into
          > $actionsToBeDon e[1]? And "incrementPageV iews" went into
          > $actionsToBeDon e[2]?
          >
          > If there is a 'hole' in the array the for() loop will fail miserably. Do
          > a foreach loop instead:
          >
          > foreach ($actionsToBeDo ne as $function) {
          > if ($allowed = isFunctionAllow ed($function)) {
          > // ... etc ...[/color]


          Nice! That is the kind of double-check on my thinking that I
          appreciate. I will go test that. These newsgroups are at their most
          useful when people catch me on my assumptions like that.

          Comment

          • lawrence

            #6
            Re: when does a PHP script stop running?

            Pedro Graca <hexkid@hotpop. com> wrote in message[color=blue]
            > Are you sure, *really* sure, that "registerSessio ns" went into
            > $actionsToBeDon e[0]? And "sendHtmlToBrow sers" went into
            > $actionsToBeDon e[1]? And "incrementPageV iews" went into
            > $actionsToBeDon e[2]?
            >
            > If there is a 'hole' in the array the for() loop will fail miserably. Do
            > a foreach loop instead:
            >
            > foreach ($actionsToBeDo ne as $function) {
            > if ($allowed = isFunctionAllow ed($function)) {
            > // ... etc ...[/color]


            Sadly, it still doesn't work. I redid the loops so that they now use
            list() and each() and the array has a string index. Then I did a
            simple test. As I mentioned before, any functions put into the first
            two loops seem to run fine, but it is as if PHP stops running once all
            the HTML is sent. Here is the class method that handles the 3 loops:






            /**
            * 12-31-03 - manipulator
            *
            * All the events that happen while this software is running must be
            triggered by some event
            * loaded into one of these arrays. This software goes through the
            arrays and runs the allowed
            * functions.
            *
            * public
            * returns void
            */
            function runMainLoop() {
            // 05-13-03 - the architexture of this software is simple, put your
            functions in one of these 3 arrays:
            //
            // $pageEventStart - this is for functions that must execute before
            any HTML is sent to the browser - stuff dealing with cookies, for
            instance.
            //
            // $pageEventMiddl e - this is for functions that should appear on
            screen. Often, I assume, this would be in conjunction with
            renderPageCance l=true. XML sitemaps, for instance. renderPage(), too,
            most obviously.
            //
            // $pageEventEnd - this is for functions that should execute after
            all the HTML is sent to the visitor. updateNumTimesA rticleViewed()
            would be an example.

            // 11-15-03 - I put this next line here just to keep things a little
            cleaner - lk
            $allowed = $GLOBALS["arrayOfAllAllo wedFunctions"];

            // 09-17-03 - this function is getting moved to $pageRender today -
            lk


            if (is_array($this->pageEventStart )) {
            ksort($this->pageEventStart );
            while (list($key, $val) = each($this->pageEventStart )) {
            $function = $val;
            $function2 = $allowed[$function];
            if ($function2) {
            if (function_exist s($function2)) {
            $function2();
            } else {
            $this->controllerForA ll->import($functi on);
            if (function_exist s($function2)) {
            $function2();
            } else {
            $this->resultsObjec t->addToErrorResu lts("In runMainLoop(), in
            the class McPageRender, in the loop for the array pageEventStart, we
            tried to import the function called '$function', but controllerForAl l
            could not find it.");
            }
            }
            } else {
            $this->resultsObjec t->addToErrorResu lts("In runMainLoop(), in the
            class McPageRender, in the loop for the array pageEventStart, we
            sought a function called '$function' in the official array of allowed
            functions, but nothing came back. Please check spelling and check the
            array.");
            }
            }
            }


            if (is_array($this->pageEventMiddl e)) {
            ksort($this->pageEventMiddl e);
            while (list($key, $val) = each($this->pageEventMiddl e)) {
            $function = $val;
            $function2 = $allowed[$function];
            if ($function2) {
            if (function_exist s($function2)) {
            $function2();
            } else {
            $this->controllerForA ll->import($functi on);
            if (function_exist s($function2)) {
            $function2();
            } else {
            $this->resultsObjec t->addToErrorResu lts("In runMainLoop(), in
            the class McPageRender, in the loop for the array pageEventMiddle , we
            tried to import the function called '$function', but controllerForAl l
            could not find it.");
            }
            }
            } else {
            $this->resultsObjec t->addToErrorResu lts("In runMainLoop(), in the
            class McPageRender, in the loop for the array pageEventMiddle , we
            sought a function called '$function' in the official array of allowed
            functions, but nothing came back. Please check spelling and check the
            array.");
            }
            }
            }


            if (is_array($this->pageEventEnd )) {
            ksort($this->pageEventEnd );
            while (list($key, $val) = each($this->pageEventEnd )) {
            $function = $val;
            $function2 = $allowed[$function];
            if ($function2) {
            if (function_exist s($function2)) {
            $function2();
            } else {
            $this->controllerForA ll->import($functi on);
            if (function_exist s($function2)) {
            $function2();
            } else {
            $this->resultsObjec t->addToErrorResu lts("In runMainLoop(), in
            the class McPageRender, in the loop for the array pageEventEnd, we
            tried to import the function called '$function', but controllerForAl l
            could not find it.");
            }
            }
            } else {
            $this->resultsObjec t->addToErrorResu lts("In runMainLoop(), in the
            class McPageRender, in the loop for the array pageEventEnd, we sought
            a function called '$function' in the official array of allowed
            functions, but nothing came back. Please check spelling and check the
            array.");
            }
            }
            }
            }







            As a simle test, I put this file in the folder where all events are
            stored:

            <?php

            // 10-04-03 - I hope this turns out to be a good idea. These global
            events, rather than have them hardcoded in a lot of
            // scattered files, I'm going to try to group them in a folder called
            GlobalEvents. All of these events will be automatically
            // included. The events assigned to the global arrays are actually
            function names that should be called by runMainLoop() or
            // renderMainConte nt.
            global $pageEventEnd;
            $pageEventEnd["testEndEve nt"] = "testEndEve nt";

            ?>





            Then I wrote this function, but I got nothing:


            function testEndEvent() {

            echo "<h1>hello</h1>";

            }




            Any ideas?

            Comment

            • Pedro Graca

              #7
              Re: when does a PHP script stop running?

              lawrence wrote:[color=blue]
              > it still doesn't work.[/color]
              (snip)[color=blue]
              > Any ideas?[/color]

              Simplify your script to a bare minimum, identify the error there (or
              post that bare minimum here), keep adding bits of code and reverifying
              until you get the error again



              <?php
              class Manipulator {
              var $pageEventStart ;
              var $pageEventMiddl e;
              var $pageEventEnd;

              function runMainLoop() {
              ProcessArray($t his->pageEventStart );
              ProcessArray($t his->pageEventMiddl e);
              ProcessArray($t his->pageEventEnd );
              }
              }

              /* this function should be a method of Manipulator -- but I am not used
              * to OOP and it's pitfalls */
              function ProcessArray($a rr) {
              if (!is_array($arr )) return;
              ksort($arr);
              while (list($key, $val) = each($arr)) {
              $val($key);
              }
              }

              function fx($x) {
              echo "fx('$x') worked.\n";
              }

              $x = new Manipulator();

              $x->pageEventSta rt['ES1'] = 'fx';
              $x->pageEventSta rt['ES2'] = 'fx';
              $x->pageEventMiddl e['EM1'] = 'fx';
              $x->pageEventMiddl e['EM2'] = 'fx';
              $x->pageEventEnd['EE1'] = 'fx';
              $x->pageEventEnd['EE2'] = 'fx';

              $x->runMainLoop( );
              ?>
              --
              --= my mail box only accepts =--
              --= Content-Type: text/plain =--
              --= Size below 10001 bytes =--

              Comment

              • lawrence

                #8
                Re: when does a PHP script stop running?

                Pedro Graca <hexkid@hotpop. com> wrote in message news:<c302vp$21 op0o$1@ID-203069.news.uni-berlin.de>...[color=blue]
                > lawrence wrote:[color=green]
                > > it still doesn't work.[/color]
                > (snip)[color=green]
                > > Any ideas?[/color]
                >
                > Simplify your script to a bare minimum, identify the error there (or
                > post that bare minimum here), keep adding bits of code and reverifying
                > until you get the error again[/color]

                Thank you, I'll try it. The experimental method can teach much. I was
                asking here to see if this was a bug or expected behavior. You seem
                confident this is a bug, so I will try your experiment.

                Comment

                Working...