473,216 Members | 1,355 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,216 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 7787
>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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Damien Renwick | last post by:
I have a php script which simply stops midway through a while loop that processes records returned by a MySQL query. The HTML page continues trying to load the page but the php has stopped running...
2
by: TonyJeffs | last post by:
I'm new to this, and stole/doctored my code from the "Ugly JavaScript Handbook" I have a slide show program. First theres stuff to set up the array, and the time between slides Then a function...
5
by: Paul O. Morris | last post by:
Is there a script that I can run to stop a particular SQL server service on Win2003 server? I'm looking for a similar script to restart that service as well. Thanks.
4
by: Vlad Hrybok | last post by:
I am using Application_End to send out a notification about application being unloaded. I found that those notifications are not being sent because the app seems to get unloaded without...
9
by: Rea | last post by:
Hi eb I set some 'Stop' statements and also visual breakpoints in asp code (vbscript). I am doing that in Microsoft Script debugger. Than I refresh the original page and expect execution to halt...
2
by: ANarula | last post by:
I am running into a strange problem. I have perl script which reads the "APPDATA" environment variable, and does some work on that directory. When I a launch this script from Command Prompt, it...
6
by: John (Z R) L | last post by:
Hi all, I am very new to programming, and I chose to study the Python language before C++. I am currently using the Wikibooks "Non-Programmer's Tutorial for Python", and am up to the section "Who...
39
by: Alan Isaac | last post by:
This may seem very strange, but it is true. If I delete a .pyc file, my program executes with a different state! In a single directory I have module1 and module2. module1 imports random and...
3
by: WP | last post by:
Hello, I have a very simple script (or would you call it a batch file?) with the following content: connect to mydb2; DROP TABLE staff_employee_address; DROP TABLE...
1
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
0
by: veera ravala | last post by:
ServiceNow is a powerful cloud-based platform that offers a wide range of services to help organizations manage their workflows, operations, and IT services more efficiently. At its core, ServiceNow...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: mar23 | last post by:
Here's the situation. I have a form called frmDiceInventory with subform called subfrmDice. The subform's control source is linked to a query called qryDiceInventory. I've been trying to pick up the...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
by: jimatqsi | last post by:
The boss wants the word "CONFIDENTIAL" overlaying certain reports. He wants it large, slanted across the page, on every page, very light gray, outlined letters, not block letters. I thought Word Art...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....

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.