3

E.g

<?php

//GetParameters here

//send response/end connection

//keep executing the script with the retrieved parameters.
secretformula
  • 6,414
  • 3
  • 33
  • 56
Not Amused
  • 942
  • 2
  • 10
  • 28
  • What would be the purpose of such behaviour? Can you give an example? – Jocelyn Nov 15 '12 at 01:37
  • Well, getting the data, storing them, but close the connection, while the "storing data" is in progress. That would reduce the php-cgi.exe procedures. Right now I have 30+ php-cgi.exe running all the time. I don't want that. – Not Amused Nov 15 '12 at 01:42
  • @DeusDeceit But even if you close the connection you php script would keep executing while saving your data, so I don't see why you think this would save you those 30 php-cgi processes. – Nelson Nov 15 '12 at 01:48
  • Btw, to close the http response connection with the browser, see http://stackoverflow.com/questions/138374/close-a-connection-early – Nelson Nov 15 '12 at 01:49
  • from what i read php-cgi.exe is running for each connection that is active not for every php file that runs. – Not Amused Nov 15 '12 at 01:52
  • 1
    Yes your php processes are launch to handle the incoming requests, but dropping the connection from the script wont terminate the process, to terminate the process just run `exit;` .. – Nelson Nov 15 '12 at 01:56
  • and that will terminate the whole script? or just the running connection? – Not Amused Nov 15 '12 at 02:00

1 Answers1

1

You could do this, it just might take some tinkering. Instead of trying to close the connection on the first script, you need to process the data with a different script.

<?php

//Get Parameters

//Send output to user

//now use curl to access other script

$post = http_build_query($_POST); // you can replace this with a processed array

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://www.example.com/otherscript.php");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 0);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
curl_exec($ch);
curl_close($ch);

?>

otherscript.php

<?php

header( "Connection: Close" );

// do your processing

?>

Just to explain, when curl connects it gets a connection closed header so curl quits. Meanwhile the "otherscript" is processing the data with no open connections.

I'm pretty sure using exec() may also be an option. You could simply call otherscript using php on the command line passing the variables as cmd line arguments. Something like this should work for you if you are running linux:

exec("nohup php -f otherscript.php -- '$arg1' '$arg2' < /dev/null &");

Now otherscript.php is running in the background under a different process id

James L.
  • 4,032
  • 1
  • 15
  • 15
  • I had the same idea, but wanted to see if it was possible to do that without changing scripts. And apparently there's no better idea so far – Not Amused Nov 15 '12 at 03:05