0

Possible Duplicate:
close a connection early

I'm looking to accomplish something along the lines of this:

  1. User requests foo.html
  2. Page starts TCPIP socket and HTTP session, echos request header information
  3. Page echos content for the file
  4. Page closes socket, user has file, everyone's happy, no more HTTP transactions going on.
  5. Function FooBar() gets invoked which... adds numbers, sends an email, updates a database, or some other task that doesn't block output of the page to the user.

In concept, my pseudo-PHP code might look like:

<?php
//Send content to the user
echo "Hello world!!";

//This terminates the script, 
//I simply want to close the 
//HTTP part without terminating
exit();

//That only took a few milliseconds

//Send an email to someone 
//containing a sum of numbers
//in the Fibonacci sequence

//This task might take minutes to do.
mail(
    "foo@example.com",
    "Your sum is ready",
    fibonacci(100)
);

But I don't see a clear way to do this as exit() terminates the script, and I don't see any methods that give me control over the HTTP socket.

I've seen close a connection early and it's an interesting answer however I'm looking to accomplish this in PHP 5.3 without output buffering and flushing.

Community
  • 1
  • 1
Incognito
  • 20,537
  • 15
  • 80
  • 120
  • 1
    Stuff that might take minutes should be delegated to a job queue. Have a script periodically checking the queue and handling stuff that's waiting. – ceejayoz Feb 15 '12 at 19:35

2 Answers2

1

As was answered by Timbo White in this question (and is very similar to the code I actually use so I know that it works) try this:

// buffer all upcoming output
ob_start();

// get the size of the output
$size = ob_get_length();

// send headers to tell the browser to close the connection
header("Content-Length: $size");
header('Connection: close');

// flush all output
ob_end_flush();
ob_flush();
flush();

//now you can do anything down here no matter how long it takes 
//because the script appears to have returned to the user.
Community
  • 1
  • 1
hackartist
  • 5,172
  • 4
  • 33
  • 48
  • Shouldn't `ignore_user_abort(true)` be there too? (you want +10 rep, find out and you;ll get it) – Vyktor Feb 15 '12 at 19:51
  • So it turns out that it doesn't have to be. ignore_user_abort(true) is useful when running long scripts if you don't want the user to be able to stop the progress (or the browser to time out even if the user is willing to wait) but since we are returning here from the browser's point of view, the browser can't interrupt anyway so you don't need it but it wouldn't hurt anything to have it. – hackartist Feb 15 '12 at 23:16
0

Look into register_shutdown_function http://php.net/manual/en/function.register-shutdown-function.php

Andrew Ellis
  • 1,175
  • 1
  • 12
  • 29
  • 1
    And what with it? This seems like useless link to manual. Give here some of your own ideas experiences. – Vyktor Feb 15 '12 at 19:41
  • Why? The OP asked what the manual describes: "Registers a callback to be executed after script execution finishes or exit() is called." – Andrew Ellis Feb 15 '12 at 19:43
  • So edit your answer and put an example there .) – Vyktor Feb 15 '12 at 19:46
  • Again, why? The manual has an example. The manual is there for a reason; to be referenced. There is no reason I should take anything out of that page and put it here, it won't help the OP fully understand what that function does. – Andrew Ellis Feb 15 '12 at 19:47
  • 2
    @AndrewEllis Posting links only are NOT answers by StackOverflow definition, that's why. – Levi Morrison Feb 15 '12 at 19:48