Run PHP Script in Background

Leopold Joy

I have two PHP scripts—we will call them script A and script B. Script A starts running when the user does a certain interaction. Script A needs to run script B in the background—thus return script A's result while script B is still running. I could do this inside script A: exec('scriptB.php &'), however since I'm on shared hosting exec is not allowed. Also, an Ajax solution (initiate both scripts on the client-side) will not work since script B MUST run—I can't have the user maliciously stopping the script from running.

Is there any solution that does not involve using a shell command or Ajax?

Thanks in advance!

Leopold Joy

I ended up using this method: http://w-shadow.com/blog/2007/10/16/how-to-run-a-php-script-in-the-background/

function backgroundPost($url){
    $parts=parse_url($url);
    $fp = fsockopen($parts['host'], 
        isset($parts['port'])?$parts['port']:80, 
        $errno, $errstr, 30);
    if (!$fp) {
        return false;
    } else {
        $out = "POST ".$parts['path']." HTTP/1.1\r\n";
        $out.= "Host: ".$parts['host']."\r\n";
        $out.= "Content-Type: application/x-www-form-urlencoded\r\n";
        $out.= "Content-Length: ".strlen($parts['query'])."\r\n";
        $out.= "Connection: Close\r\n\r\n";
        if (isset($parts['query'])) $out.= $parts['query'];
        fwrite($fp, $out);
        fclose($fp);
        return true;
    }
}

//Example of use
backgroundPost('http://example.com/slow.php?file='.urlencode('some file.dat'));

Rachael, thanks for the help.

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

How to run a shell script in background?

분류에서Dev

Python script run in background not writing to file?

분류에서Dev

Run bash script from php

분류에서Dev

Run a PHP (CLI) script indefinitely

분류에서Dev

PHP executing background command line script

분류에서Dev

Call a script and continue running it in background in PHP?

분류에서Dev

Run PHP script without webpage waiting for it to finish

분류에서Dev

run bash script from php exec()

분류에서Dev

Long PHP script does not run completely

분류에서Dev

Cannot run cron job for a php script

분류에서Dev

I can't get the html form to run a php script

분류에서Dev

How to share CPU resource to run PHP script faster?

분류에서Dev

run php script from database record without using eval()

분류에서Dev

Systemd: How to start/stop Python script that should run in background, inside Virtualenv

분류에서Dev

BASH: Run script.sh in background and output that instance's pid to file?

분류에서Dev

How to run a command in background always?

분류에서Dev

how to run closed application In background?

분류에서Dev

Script will only run in Powershell

분류에서Dev

Run a script on git push

분류에서Dev

PHP and background processes

분류에서Dev

Run script on login (script with sudo) or startup

분류에서Dev

How to run Python Script on powerBI

분류에서Dev

Run script in a non interactive shell?

분류에서Dev

Run python oneliner in bash script

분류에서Dev

Start powershell process and run a script

분류에서Dev

Python Script to run a C Program

분류에서Dev

Run 1 function of bash script

분류에서Dev

Run script after foreach list

분류에서Dev

Shell script does not run properly