471,316 Members | 973 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

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):

registerSessions();
sendHtmlToBrowsers();
incrementPageViews();
Is there any chance that incrementPageViews() 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):

$actionsToBeDone[] = "registerSessions";
$actionsToBeDone[] = "sendHtmlToBrowsers";
$actionsToBeDone[] = "incrementPageViews";

for ($i=0; $i < count($actionsToBeDone); $i++) {
$function = $actionsToBeDone[$i];
if ($allowed = isFunctionAllowed($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 incrementPageViews would
never execute?
What I actually do is have 3 such arrays, $pageEventsStart,
$pageEventsMiddle, 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 $pageEventsStart.

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 $pageEventsStart. 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() {

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

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

for ($i=0; $i < count($pageEventsEnd); $i++) {
$function = $pageEventsEnd[$i];
if ($allowed = isFunctionAllowed($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
$pageEventsStart, they work perfectly.
Jul 17 '05 #1
7 7646
>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?


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
Jul 17 '05 #2
<la*******@hotmail.com> wrote in message
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.


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.
Jul 17 '05 #3
lawrence wrote:
$actionsToBeDone[] = "registerSessions";
$actionsToBeDone[] = "sendHtmlToBrowsers";
$actionsToBeDone[] = "incrementPageViews";

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

Are you sure, *really* sure, that "registerSessions" went into
$actionsToBeDone[0]? And "sendHtmlToBrowsers" went into
$actionsToBeDone[1]? And "incrementPageViews" went into
$actionsToBeDone[2]?

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

foreach ($actionsToBeDone as $function) {
if ($allowed = isFunctionAllowed($function)) {
// ... etc ...
--
--= my mail box only accepts =--
--= Content-Type: text/plain =--
--= Size below 10001 bytes =--
Jul 17 '05 #4
Pedro Graca <he****@hotpop.com> wrote in message news:<c2*************@ID-203069.news.uni-berlin.de>...
lawrence wrote:
$actionsToBeDone[] = "registerSessions";
$actionsToBeDone[] = "sendHtmlToBrowsers";
$actionsToBeDone[] = "incrementPageViews";

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

Are you sure, *really* sure, that "registerSessions" went into
$actionsToBeDone[0]? And "sendHtmlToBrowsers" went into
$actionsToBeDone[1]? And "incrementPageViews" went into
$actionsToBeDone[2]?

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

foreach ($actionsToBeDone as $function) {
if ($allowed = isFunctionAllowed($function)) {
// ... etc ...

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.
Jul 17 '05 #5
Pedro Graca <he****@hotpop.com> wrote in message
Are you sure, *really* sure, that "registerSessions" went into
$actionsToBeDone[0]? And "sendHtmlToBrowsers" went into
$actionsToBeDone[1]? And "incrementPageViews" went into
$actionsToBeDone[2]?

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

foreach ($actionsToBeDone as $function) {
if ($allowed = isFunctionAllowed($function)) {
// ... etc ...

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.
//
// $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.

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

// 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_exists($function2)) {
$function2();
} else {
$this->controllerForAll->import($function);
if (function_exists($function2)) {
$function2();
} else {
$this->resultsObject->addToErrorResults("In runMainLoop(), in
the class McPageRender, in the loop for the array pageEventStart, we
tried to import the function called '$function', but controllerForAll
could not find it.");
}
}
} else {
$this->resultsObject->addToErrorResults("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->pageEventMiddle)) {
ksort($this->pageEventMiddle);
while (list($key, $val) = each($this->pageEventMiddle)) {
$function = $val;
$function2 = $allowed[$function];
if ($function2) {
if (function_exists($function2)) {
$function2();
} else {
$this->controllerForAll->import($function);
if (function_exists($function2)) {
$function2();
} else {
$this->resultsObject->addToErrorResults("In runMainLoop(), in
the class McPageRender, in the loop for the array pageEventMiddle, we
tried to import the function called '$function', but controllerForAll
could not find it.");
}
}
} else {
$this->resultsObject->addToErrorResults("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_exists($function2)) {
$function2();
} else {
$this->controllerForAll->import($function);
if (function_exists($function2)) {
$function2();
} else {
$this->resultsObject->addToErrorResults("In runMainLoop(), in
the class McPageRender, in the loop for the array pageEventEnd, we
tried to import the function called '$function', but controllerForAll
could not find it.");
}
}
} else {
$this->resultsObject->addToErrorResults("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
// renderMainContent.
global $pageEventEnd;
$pageEventEnd["testEndEvent"] = "testEndEvent";

?>

Then I wrote this function, but I got nothing:
function testEndEvent() {

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

}


Any ideas?
Jul 17 '05 #6
lawrence wrote:
it still doesn't work. (snip) Any ideas?


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 $pageEventMiddle;
var $pageEventEnd;

function runMainLoop() {
ProcessArray($this->pageEventStart);
ProcessArray($this->pageEventMiddle);
ProcessArray($this->pageEventEnd);
}
}

/* this function should be a method of Manipulator -- but I am not used
* to OOP and it's pitfalls */
function ProcessArray($arr) {
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->pageEventStart['ES1'] = 'fx';
$x->pageEventStart['ES2'] = 'fx';
$x->pageEventMiddle['EM1'] = 'fx';
$x->pageEventMiddle['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 =--
Jul 17 '05 #7
Pedro Graca <he****@hotpop.com> wrote in message news:<c3*************@ID-203069.news.uni-berlin.de>...
lawrence wrote:
it still doesn't work.

(snip)
Any ideas?


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


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

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Damien Renwick | last post: by
2 posts views Thread by TonyJeffs | last post: by
5 posts views Thread by Paul O. Morris | last post: by
4 posts views Thread by Vlad Hrybok | last post: by
39 posts views Thread by Alan Isaac | last post: by

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.