Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make php script run another php script

Tags:

php

I'm trying to do exactly the same thing as in my previous Python question, but in PHP. See my previous (answered) question

PHP script from previous question does something and then runs another PHP script on the same server, does something and then quits (while second script still continues its work). How do I achieve this?

Please note that PHP script is also a web page at the same time (so maybe we can use it like in previous question where answer to my question was snippet that made python just open url instead of running subprocess... although I don't have a clue if that's useful information, maybe it's different in PHP, I'm not too experienced in PHP)... and that I want to make each scripts independent - so if first php script will finish I would like second php script to continue working even though first one ended.

What do you think is most elegant way to do this? Would echoing iframe work or should I do this differently?

like image 501
Phil Avatar asked Jul 10 '09 15:07

Phil


People also ask

How do I run one PHP file from another?

It is possible to insert the content of one PHP file into another PHP file (before the server executes it), with the include or require statement. The include and require statements are identical, except upon failure: require will produce a fatal error (E_COMPILE_ERROR) and stop the script.

Can PHP be run client side?

No. PHP cannot be run in browser.

How do I run a batch file from PHP?

You can run the php-script from the task scheduler. Just let the task scheduler run php.exe and set the location of the php-file as the argument of the task. worked perfectly.


2 Answers

This is a bit of unix/linux-only hack that might not work on shared web servers:

file1.php

<?php
$somearg = escapeshellarg('blah');
exec("php file2.php $somearg > /dev/null &");   

file2.php

<?php
//do some stuff that will take a while
//$argv should contain 'blah', and also it seems the name of the php file
//this script will continue to run. You might want to set max_execution_time to a sensible value.
like image 145
Tom Haigh Avatar answered Oct 16 '22 22:10

Tom Haigh


You can do it by placing header('Location: second.php'); at the end of first.php and again at the end of second.php to start executing third.php and so on. Even you can create cycles between two scripts, i.e after ending first.php, second.php will be started and after executing second.php again the first.php, again second.php and so on.

But remember, this is a browser dependent feature and how many script can be executed one after another, totally depends on how many times that browser permits redirection. I found mozilla allows 23 redirections, that means you can executes 23 scripts one after another.

like image 34
Unknown Avatar answered Oct 16 '22 22:10

Unknown