user3188554
user3188554

Reputation: 67

Execute php script from php page asynchronously in WINDOWS system

I have been searching through several questions similar to this but i have found no answer suitable for me, especially for Windows system. I have installed PHP and Apache ona Windows machine. I have an app which calls Page1.php. In Page1 i execute some code and at an specific point I would like to launchanother php script, let`s call it Script.php asynchronously. I don´t need to pass any parameter to this second script -if I could it could be helpful but it´s not mandatory- and I don´t need to wait for the script to finish beacuse i don´t want to retain the communication with the user, since script makes a lot of operations with files and it can last quite a long time. I have seen and tried answers based on exec, popen, shell, system and so on...but I haven´t found the right one for me, maybe because I´m working on Windows.These have beeen some of the solutions I have tried without anysuccess:

exec('php script.php &> /dev/null &');
shell_exec('php script.php &> /dev/null &');
system('php script.php &> /dev/null &');
php script.php &> /dev/null &`

The schema could be more or less as follows:

PAGE1.PHP

<?php
echo "Hello World";
echo "\n";
....
if ($var>$var2) {
    HERE CALL SCRIPT.PHP ASYNC
}
...
?> 

SCRIPT.php

<?php
...
...
?> 

Upvotes: 2

Views: 2408

Answers (2)

iLot
iLot

Reputation: 98

Copied from php.net's comments on exec's page:

This will execute $cmd in the background (no cmd window) without PHP waiting for it to finish, on both Windows and Unix.

<?php 
function execInBackground($cmd) { 
    if (substr(php_uname(), 0, 7) == "Windows"){ 
        pclose(popen("start /B ". $cmd, "r"));  
    } 
    else { 
        exec($cmd . " > /dev/null &");   
    } 
} 
?>

I did not test this myself, but it was a similar direction to what I was thinking of. You can also look at this stackoverflow question for more details (might help).

Upvotes: 2

Kevin Kopf
Kevin Kopf

Reputation: 14210

What you call "asynchronous" is not actually asynchronous. You make a call for another script to execute in case $var > $var2 and it's called an if-statement.

If your script has to be in another file, you may try using include, include_once or require or require_once:

if ($var > $var2) {
    include "Script.php";
}

The rest depends on Script.php.

Upvotes: 0

Related Questions