UnkwnTech
UnkwnTech

Reputation: 90921

Running PHP after request

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?

Upvotes: 2

Views: 323

Answers (4)

Quamis
Quamis

Reputation: 11087

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.

Upvotes: 2

Karsten
Karsten

Reputation: 14642

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);

Upvotes: 4

benlumley
benlumley

Reputation: 11382

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).

Upvotes: 0

Evan Fosmark
Evan Fosmark

Reputation: 101731

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.

Upvotes: 2

Related Questions