Page 1 of 1

Starting a PHP script and ending browser output

Posted: Tue Aug 17, 2004 4:17 pm
by visionmaster
Hello,

I would like to do following on a website:

1. Click a link on a website => A page is outputed with a message like "Script was successfully started".
2. At the same time the script that produces the HTML output, will start a php script which initiates a import.

Background: The script which starts the import can run a few hours. Would I start the script an doutput a message to the browser, the blue progress bar would build up very slowly and at some point the browser would output "Page can not be shown". The php process on the server would still be running. Would be o.k., but not very user friendly.

=> That's why I would like to output a message to the browser immediately and in the background start a specific php-script. In this was I would stop browser outputting and start a php-script in the background.

=> How do I do that? Any specific example?

=> Is there a possibilty to show the user (if he opens up a specific browser page a few hours afterwards) if a specific php-process is still running? Like "Process Skript1 running"...

Thanks

LAMMP-Server

Posted: Tue Aug 17, 2004 4:23 pm
by feyd
here's one way:

use the database to know what state the actual processing script is in, when the front-end script is called, it checks this status.. if it's not running, it starts it as says so. If it's still running, it says so.

the actual backend script updates the status at certain intervals to tell the outside world "I'm 10% through the data" or whatever.. until it's done. You can set an auto-refresh on the front-end script so they can "read" the status, if they'd like.. :)

Posted: Tue Aug 17, 2004 4:45 pm
by visionmaster
Hello,

Thanks for your reply. I actually have no idea how to realize your concept.

Do have any examples for the first part of my question?

1. Click a link on a website => A page is outputed with a message like "Script was successfully started".
2. At the same time the script that produces the HTML output, will start a php script which initiates a import. This of course should not start any browser ouput!

Thanks!

Posted: Tue Aug 17, 2004 4:54 pm
by feyd
basics in some pseudo code:

Code: Select all

look in the database for current status

if status is not-started:
write out standard starting message (with auto refresh?)
start the import scripts, either through include or some other means.
end script.
endif;

if status is not-finished:
output current mile marker the script is on (if that's stored in the status), (with auto refresh?)
endif;

if status is done:
display results, if needed.
reset the state to not-started so the process can be repeated.
endif;

Posted: Wed Aug 18, 2004 2:56 am
by visionmaster
Thanks for your response. But that still dosn't answer my question.

I must be possible to start a script from another script!
I open up "prozess_start.php", this scripts outputs "Test" in the browser and then browser outputting is ended. Script "prozess.php" should then be started. This script holds an infinite loop, which writes the content of $i in a logfile.

==>"Test" is correctly outputted. But my script "process.php" is not started. Probably because I have to open it up with the php interpreter. How do I do that and is what I am planning possible? (I actually think it should be possible!)


prozess_start.php
------------------------

PHP-Quellcode:

<?
print "Test";
exec('prozess.php');

?>



prozess.php:
------------------

PHP-Quellcode:

<?php

# Logfile
$fp = fopen("../logfiles/prozess.txt","a");
for (; ; ) {
$i++;
fputs($fp,$i." \r\n");
}
fclose($fp);

?>

Posted: Wed Aug 18, 2004 3:28 am
by visionmaster
O.k., I have to include 'prozess.php', found that out.

But unfortunately the browser still thinks it receives data, the browser bar progresses until a server time out is shown. I want to print something out, stop the browser output and start a php script in the background, which runs very, very long.

process_test.php:
--------------------

Code: Select all

<?

print "Test";
include('prozess.php');

?>

Code: Select all

process.php:
--------------
<?php

for (; ; ) {
    $i++;
}

?>

Posted: Wed Aug 18, 2004 3:32 am
by feyd
if you set up this script as a cgi, and then exec'd it, I think it may work.. you'd have to add the shebang to the process.php of course.. it wouldn't terminate the original script though.. although setting a low time out, and temporarily turning off the warning could potentially work to do it..

Posted: Wed Aug 18, 2004 4:11 am
by visionmaster
feyd wrote:if you set up this script as a cgi, and then exec'd it, I think it may work.. you'd have to add the shebang to the process.php of course.. it wouldn't terminate the original script though.. although setting a low time out, and temporarily turning off the warning could potentially work to do it..
O.k., what script do I have to set up as a cgi and how do I do that?

"add the shebang" What does that mean?

Thanks

Posted: Wed Aug 18, 2004 4:18 am
by feyd
for setting CGI, you'll need a directory set up as the CGI folder.. in Apache you'll need an AddHandler that has php processing.. similar for IIS and other servers..

Code: Select all

#!/usr/bin/php
or whatever

:: shebang, short for sharp-bang or the #! bit :)