Re: how to run scripts after a page has already loaded and been sent to a users browser?

From: lawrence (lkrubner_at_geocities.com)
Date: 10/26/03


Date: 26 Oct 2003 13:23:39 -0800


"Lee Marsh" <burgermeister01@insightbb.com> wrote in message news:<xRHmb.22030$mZ5.86707@attbi_s54>...
> It's because PHP is a server side scripting language, so that you can only
> do stuff before you send stuff to user's web browser. So in other words
> there's no way to make any PHP script work after the page loads, so unless
> you can get it to work in a client side language like javascript, you're
> SOL.

Do you have a reference to explain your remark that PHP stops running
when the HTML is sent? I'd like to read that for myself.

As we've discussed on this newsgroup before, if a script starts
running before a page loads it will usually run till its done, even if
that is after the last HTML is sent to a web browser. I think the last
conversation came up in reference to redirection - if you redirect
someone does the script continue to run even after the person's web
browser has been sent elsewhere.

In this case we have a class method that is dying in the middle. You
can see the method below. Sending HTML to the webbrowser is something
that happens while $pageEventMiddle is being processed. But the stuff
in $pageEventEnd is not getting executed. Would PHP really cut this
method off when it is only half-way done?

Anyway, PHP is more than a server side scripting language. You can run
it from the command line of your machine. If it works when no HTML is
involved, why does it quit half-way when HTML is involved? Is it
Apache that stops its operation when it is loaded as an Apache module?

Here is the class method that is dying in the middle:

        
        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.
                //
                // $pageEventMiddle - this is for functions that should appear on
screen. Often, I assume, this would be in conjunction with
renderPageCancel=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. updateNumTimesArticleViewed()
would be an example.
        
                // 05-07-03 - it's time to get serious about architexture, so we're
going to enforce the
                // rule that $choiceMade can never be used on this page.
                // 06-21--03 - nice idea but hopeless. There's too much code that's
been written using choiceMade. We'd have to rewrite the whole
                // program if we wanted to definitely, absolutely, ban it from here.
We must find another path to security.
                //if ($choiceMade) die ("The programmer has made a mistake by using
the variable choiceMade on this page, where it is not allowed.");
        
                // 09-17-03 - this function is getting moved to the McPageRender
class today - lk
                for ($i=0; $i < count($this->pageEventStart); $i++) {
                        $function = $this->pageEventStart[$i];
                        if (!function_exists($function))
$this->controllerForAll->import($function, "n");
                        if (!function_exists($function)) {
                                print "The software is looking for a function called $function.
It's possible that you don't really need this function, in which case,
find the file that's calling for it and remove that file. First look
in the folder mcGlobalEvents, as it is likely to have unneeded files.
If, however, you need the function, then you need to reinstall the
software. Remember you can always find a copy at <a
href=\"http://www.publicDomainSoftware.org/\">Public Domain
Software</a>.";
                        } else {
                                $function();
                        }
                }
        
                for ($i=0; $i < count($this->pageEventMiddle); $i++) {
                        $function = $this->pageEventMiddle[$i];
                        if (!function_exists($function))
$this->controllerForAll->import($function, "n");
                        if (!function_exists($function)) {
                                print "The software is looking for a function called $function.
It's possible that you don't really need this function, in which case,
find the file that's calling for it and remove that file. First look
in the folder mcGlobalEvents, as it is likely to have unneeded files.
If, however, you need the function, then you need to reinstall the
software. Remember you can always find a copy at <a
href=\"http://www.publicDomainSoftware.org/\">Public Domain
Software</a>.";
                        } else {
                                $function();
                        }

                }
                        
                for ($i=0; $i < count($this->pageEventEnd); $i++) {
                        $function = $this->pageEventEnd[$i];
                        if (!function_exists($function))
$this->controllerForAll->import($function, "n");
                        if (!function_exists($function)) {
                                print "The software is looking for a function called $function.
It's possible that you don't really need this function, in which case,
find the file that's calling for it and remove that file. First look
in the folder mcGlobalEvents, as it is likely to have unneeded files.
If, however, you need the function, then you need to reinstall the
software. Remember you can always find a copy at <a
href=\"http://www.publicDomainSoftware.org/\">Public Domain
Software</a>.";
                        } else {
                                $function();
                        }
                }
        }

> --
> <=============>
> --Rich
> http://www.inaneasylum.org
> "lawrence" <lkrubner@geocities.com> wrote in message
> news:da7e68e8.0310251240.7c4df238@posting.google.com...
> > I've got some code that works if I run it before I send HTML to a web
> > site visitors browser, but doesn't seem to work afterward. Below you
> > can see the code I'm running before and after a page loads. It is the
> > same code.
> >
> > Is there any reason why a function wouldn't work after a page has been
> > loaded?
> >
> >
> >
> >
> >
> > global $controllerForAll;
> > $sql = & $controllerForAll->getObject("McSql");
> >
> > global $pageId;
> > $numOfViews = $sql->getJustOneField($pageId, "cbNumTimesViewed") ;
> > $numOfViews++;
> > $sql->setValue("cbNumTimesViewed", $numOfViews);
> > $sql->update($pageId, "n");
> >
> > echo "here is the page id : $pageId <hr>";
> > echo "Here is the number of times this page has been viewed:
> > $numOfViews ";
> >
> >
> >
> >
> >
> >
> >
> > function incrementPageViewCount() {
> > global $controllerForAll;
> > $sql = & $controllerForAll->getObject("McSql");
> >
> > global $pageId;
> > $numOfViews = $sql->getJustOneField($pageId, "cbNumTimesViewed") ;
> > $numOfViews++;
> > $sql->setValue("cbNumTimesViewed", $numOfViews);
> > $sql->update($pageId, "n");
> >
> > echo "Here is the number of times this page has been viewed:
> > $numOfViews ";
> >
> > }



Relevant Pages

  • Re: How do we get there from here?
    ... server-side-scripted html. ... This is a simple example with very little php scripting. ... means that the version of the php pre-processor on your web server must ... >>> The browser never sees anything not sent to it by the script. ...
    (comp.databases.pick)
  • Re: Can PHP run from command line render HTML?
    ... the web server via a browser). ... Can such a script render HTML? ... idea being that the PHP script would startup a browser session when the ...
    (comp.programming)
  • Re: Can Javascript work with Multiple lines of Text.
    ... The code generated is normal HTML code, ... HTML = HyperText Markup Language; PHP = PHP Hypertext Processor. ... file will be caught by the PHP parser and execute any script it finds, ... browser converts the markups into the DOM; ...
    (comp.lang.javascript)
  • Re: How do we get there from here?
    ... Unlike my dislike for PHP, my dislike for SQL is based on ... >>> up with at least html and php quite mixed into each other, ... Using a PHP script to send HTML ... code to a browser in no way taints that code. ...
    (comp.databases.pick)
  • PHP Program Randomly Stops
    ... "You can use a combination of PHP with client side code to keep the ... are pages to be processed print a client side script, e.g. JavaScript, ... This will reload until all pages are done - then ... "You are running the script from a browser. ...
    (comp.lang.php)