Phil
Phil

Reputation: 3648

How to make php script run another php script

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?

Upvotes: 14

Views: 48872

Answers (9)

Tom Haigh
Tom Haigh

Reputation: 57815

You can try to start another PHP process in the background via the shell. It is somewhat of a hack, won't work on all operating systems, and 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.

Upvotes: 18

Taras Soroka
Taras Soroka

Reputation: 126

If your remote script is accessible as simple webpage via http, you might try variant with http get request where response timeout set to minimal time for example via CURL:

if( $curl = curl_init() ) {
  curl_setopt($curl, CURLOPT_URL, 'http://your_site/your_script.php');
  curl_setopt($curl, CURLOPT_TIMEOUT, 1);
  curl_exec($curl);      
  curl_close($curl);
}

but so you will have delay in 1 second in the your first script execution.

Upvotes: 0

Unknown
Unknown

Reputation: 61

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.

Upvotes: 6

Jinu Joseph Daniel
Jinu Joseph Daniel

Reputation: 6291

To run a second script on the same server

$appUrl = $_SERVER['HTTP_HOST'];
$path = 'second.php';//your path here
$appUrl = 'http://'.$appUrl.'/'.$path;
file_get_contents($appUrl);

Upvotes: 3

Matchu
Matchu

Reputation: 85784

I'm having trouble getting my previous answer to work, though I suspect it may be the fault of my own server, or perhaps new browsers refusing the close the connection when instructed (I'm really not a pro on how that shtuff works).

If that method doesn't work for you, either, try this article on pseudo-multi-threading in PHP, and see if you have better luck :)

Upvotes: 0

shadowhand
shadowhand

Reputation: 3201

You should look into pcntl_fork if you want a multithreaded application. Also look at ignore_user_abort.

Upvotes: 1

EvilChookie
EvilChookie

Reputation: 573

To the best of my knowledge, there isn't any way to run a second page (AJAX is the exception). As far as I know, PHP doesn't support multiple threads (please correct me if I'm wrong) and the 'single-thread' nature of the web seems to defeat it anyways.

I would rather be inclined to look at your specific application and find out why you need to have two separate pages run - and then re-engineer the process so it does not.

I'd be willing to bet that a re-engineer would end being less of a headache from a development standpoint, as well as a logic and implementation one.

Upvotes: 1

Matchu
Matchu

Reputation: 85784

If you need this other script to be run, depending on the client would not be wise.

What I'd do is use the technique described in the answer to this StackOverflow question (which points to this comment in the PHP documentation) and include the other script as your post-processing.

However, that comment was written in 2006, and things may have changed since then. Please give the technique a try (as I will be doing, just for fun) and see if it works for you :)

Upvotes: 2

Justin Giboney
Justin Giboney

Reputation: 3301

If a user will be using a web browser to reach the php script, I would use ajax to call the second page.

The user won't even know it is being called.

See w3schools for a tutorial on AJAX

Upvotes: 2

Related Questions