14

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?

9 Answers 9

18

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.
5

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.

3

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);
3
  • 1
    file_get_contents just gets the contents of it, right? How does it help to execute the file? Correct me if I'm wrong, sorry, am learning. Commented Jan 25, 2016 at 20:42
  • Yeah - i agree.. file_get_contents just got me the text of the file.. it didn't execute it
    – jcansell
    Commented Dec 1, 2016 at 10:57
  • 1
    you get content of webpage it mean its executed ,
    – user889030
    Commented Oct 5, 2017 at 18:23
2

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

2
  • 4
    Stackoverflow needs auto replace for w3schoolds with MDN. developer.mozilla.org/en/docs/AJAX Commented Aug 8, 2013 at 1:33
  • Bad because basically just a link answer, and no explanation of how to use ajax from a php script to call another php script
    – Andrew
    Commented Sep 3, 2015 at 19:40
2

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 :)

1

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.

1

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

3
  • Does that method work on Apache? This comment on documentation (us2.php.net/manual/en/function.pcntl-fork.php#49949) says no, but others say kinda.
    – Matchu
    Commented Jul 10, 2009 at 15:38
  • Additionally, ignore_user_abort only involves the user pressing stop, not the script itself closing the connection early.
    – Matchu
    Commented Jul 10, 2009 at 15:39
  • No, pcntl_fork will not work under mod_php. The OP did not specify if the script would used for web or cli.
    – shadowhand
    Commented Jul 13, 2009 at 16:14
0

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 :)

0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.