Jump to content

Recommended Posts

What's the easiest way for a script to spawn a second process?

 

I want to run something at the end of a pageload, but I don't want the user's browser to keep "loading" even after all the content has been sent.

 

If i do die() the browser stops loading, but the script is dead.

 

I noticed this: http://bugs.php.net/bug.php?id=29846

 

but, I'm wondering if there's a way around this..

Link to comment
https://forums.phpfreaks.com/topic/88462-creating-a-second-process/
Share on other sites

Let me get this correct. You first process the content and have it ready to display on the page. You want to display this content (so the page loads with the content) AND THEN continue with more PHP processing?

 

I've seen this hack before, a long time ago, I've spent hours scouring the web looking for it again, but I can't find it. However, another (more readily) solution is to use AJAX with an onload function to call a PHP page. This may be more feasible for your problem.

that relies on the user to load the secondary process, which is unreliable..

 

but yeah, i want to continue running the script after the browser output is complete. from what I can tell there's no efficient way to do this.

 

I was thinking of doing something really weird, like opening a stream to a url with that process code, which ignores timeout..  fopen(url), then right away fclose()..

 

but that seems really cumbersome.

PHP is what PHP does. PHP is a HyperText Pre-Processor which means it will always run before a page is loaded (thank god it runs at super-speed xD)

 

that relies on the user to load the secondary process, which is unreliable..

 

No it doesn't. All it relies on is that the user has javascript enabled. You can stick AJAX under a document.onload = runajax; and run it from there, and that doesn't require a user to do anything (you won't even see "Page Loading..." in the browser while the AJAX is calling the PHP page.)

that's unreliable.

 

reliable is knowing the script *will* run no matter what. not crossing your fingers hoping the user has javascript enabled..

 

 

 

You do realize you can check to see if the user has javascript enabled? You can have two schemas, one that runs the PHP completely, then outputs the page (for those w/o javascript) and then one that outputs and uses JavaScript to run the rest of the PHP.

Eitherway, it's bad policy to have something critical depend on the user.

 

I'd much rather a method whereby the server handles the process completely independent of the user.

 

register_shutdown_function() was a good way to do this, but it's function has been crippled and I need a new way to do what it did.. :(

Looking at the function, it's almost like the output buffers in controlling data, but there is no way to control what to run after you stop processing the PHP... Most likely because of a security issue (someone probably found a way around this or something, stopped it from running).

<?php
  // turn on buffering
  ob_start();
  // do our page thing
  echo "this will diaply in yer window";

  // get the buffer and length
  $output=ob_get_contents();
  $olen=ob_get_length();
  // stop buffering
  ob_end_clean();
  // tell browser when how big the contents of our page is
  header("Content-length: $olen");
  //output the page
  echo $output;
  // the following shud not display in the browser, reason using the file sample.txt as proof it continues processing
  $fh=fopen("sample.txt","w+");
  fwrite($fh,$msg="This shud not display in yer window, thus the script is still executing\n");
  fclose($fh);
  echo $msg;
?>

oops forgot some more things on it

<?php
  // turn on buffering
  ob_start();
  // do our page thing
  echo "this will diaply in yer window";

  // get the buffer and length
  $output=ob_get_contents();
  $olen=ob_get_length();
  // stop buffering
  ob_end_clean();
  // tell browser when how big the contents of our page is
  if($olen<256) {$output = str_pad($output, 256);$olen=256;} // IE Hack
  header("HTTP/1.1 200 OK");
  header("Content-Length: $olen");
  //output the page
  echo $output;
  // the following shud not display in the browser, reason using the file sample.txt as proof it continues processing
  $fh=fopen("sample.txt","w+");
  fwrite($fh,$msg="This shud not display in yer window, thus the script is still executing\n");
  fclose($fh);
  echo $msg;
?>

 

that shud work

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.