Calling a PHP script from within PHP

Calling a PHP script from within PHP

Posted by: ntm
Posted on: 2007-07-09 05:17:00

Hi -

I'm trying to execute PHP script 2 from within PHP script 1 - the idea is that script 2 will carry on processing data until it has finished doing so, even if the user logs off or browses to a different part of the website.

The main problem I am encountering is that if you do the following (the 'bash -c etc.' part serves to background the process and return control to PHP) -

exec('bash -c "exec nohup setsid /usr/local/bin/php /path/to/php/script/2.php > /path/to/output/file.txt 2>&1 &"');

- then file.txt will end up containing the output of script 1 - NOT script 2. I have tried to find some information on this, and the only post I could find addessing the issue mentioned that calling PHP from within PHP does not seem to be possible. The same happens if you try to execute the script from within a bash script and use exec to call sh, rather than calling PHP.

Any suggestions? I could use cron, but would prefer to be able to call script 2 when the user prompts it.

Re: Calling a PHP script from within PHP

Posted by: crimsondryad
Posted on: 2007-07-10 15:13:00

You can't do a combination of both? Set Script 1 to set a flag to 1 or whatever in a database. Then run your cron every minute or whatever to check if that flag has been set. If it has, run Script 2.

I can see multiple reasons why this wouldn't be working right, primarily among them that shell users and web ftp users would have different permissions. Allowing any web-facing script to run exec() seems like a substantial security risk.

================================
Angela Gann
CrimsonDryad Web Design Services
Web Design, Custom Software Development
http://www.crimsondryad.com

Tags: php scriptusr