tags:

views:

117

answers:

4

I would like to be able to start a second script (either PHP or Python) when a page is loaded and have it continue to run after the user cancels/navigates away is this possible?

+2  A: 

Use process forking with pcntl. It only works under Unix operating systems, however.

You can also do something like this:

exec("/usr/bin/php ./child_script.php > /dev/null 2>&1 &");

You can read more about the above example here.

Evan Fosmark
+4  A: 

You can send Connection:Close headers, which finishes the page for your user, but enables you to execute things "after page loads".

There is a simple way to ignore user abort (see php manual too):

ignore_user_abort(true);
Karsten
well,also call session_write_close() :)
Quamis
A: 

Another approach if you can't use the others is to include an img tag at the bottom of your output that requests a php page that does whatever it is you are wanting to do.

It will still show the loading animation though, so I think Karsten's suggestion is probably better (I'll try that next time I need to do this type of thing I think).

benlumley
+2  A: 

for keeping the current script:

  • ignore_user_abort(true);
  • set_time_limit(0);

for running another script:

  • see $sock=fsockopen('http://localhost/path_to_script',80,$errorStr,3600) + stream_set_timeout($sock,3600);
  • see exec('php path_to_script'); - this will cause your script to run from the CLI, so you'd have to install php-cli on that server.
Quamis