Re: [PHP] Closing a connection to browser without exiting the script
Well, really you can't do that. A PHP request only takes interrupt input when it starts, from the GET, POST, COOKIE, and SESSION magic variables. It doesn't run as a daemon. A fresh Ajax call to the script starts a new instance of the script. Fortunately, PHP's engine is fast enough that the performance is still not that much of a problem unless you're grossly over-including code. The only way to have a "stay-resident" PHP daemon for Ajax callbacks would be to either (a) Use an Opcode cache, which is not really a stay-resident but would eliminate a lot of the repetitive initialization in the engine or (b) write a CLI PHP script that you load on the server as a daemon that listens on a port, essentially same way that apache or MySQL or postfix or any other daemon does. For the latter you may have to use an off-port. For what you're trying to do, I'd suggest just writing your script normally and letting the Zend engine take care of it for you. If you're really concerned about performance you can have two include conditions, one for a full page and one for an Ajax call, but you have to be very careful about how you structure your code then. An Opcode cache would be easier and more flexible. As for persistent data across requests, that's what sessions are for. On Sunday 05 November 2006 06:04, David Négrier wrote: > Hi All, > > Thanks a lot for your numerous answers. > I've learned a lot from all your suggestions. > Actually, combining all your answers, I'll come up with a solution. > To be more explicit on what I want to do, I want in fact to start a > script, I want that script to > display a page, and then, I want that script not to stop, but I want to > be able to access that script > back using AJAX calls from the web page. So my script is opening a > network connection after having displayed the > web page and is waiting for incoming calls. Although no solution given > here is fully satisfying (because I don't want > to launch antother script, I want to remain in the same) I still can > find a way to close my connection to the browser (using > a header specifying the connection length) and then go back to my script > using AJAX. > > Thanks a lot for the time you took to propose solutions. > Best regards, > David. > www.thecodingmachine.com > > Eric Butera a écrit : > > On 11/1/06, David Négrier <[EMAIL PROTECTED]> wrote: > >> Hello there, > >> > >> I'm having a somewhat unusual question here, and I cannot find any way > >> to solve it. > >> > >> I have a PHP page that displays a message, and then, performs a very > >> long operation. Note that it displays the message first. > >> I do not intend to give some feedback to the user when the operation is > >> done. > >> > >> I've seen I can use ignore_user_abort() to prevent the user from > >> stopping the ongoing operation, but that solves only part of my problem. > >> Because as long as the page is not fully loaded, the mouse cursor in the > >> user's browser is showing a watch. > >> > >> So ideally, what I would like is to be able to close the connection from > >> the server-side, but without using the exit() function, so my script > >> keeps running afterwards. > >> > >> I know I could use a system() call to launch another process to do the > >> processing, but I would like to avoid doing that, because there are many > >> variables in the context that I cannot easily pass in parameter. > >> > >> I also tried to use the register_shutdown_function() to perform my > >> operation after the page is displayed but since PHP 4.1.0, the > >> connection is closed after the function is called > >> > >> Would any of you have an idea on how I could close that connection? > >> > >> Thanks a lot, > >> David > >> www.thecodingmachine.com > >> > >> -- > >> PHP General Mailing List (http://www.php.net/) > >> To unsubscribe, visit: http://www.php.net/unsub.php > > > > If you haven't gone with any of the listed methods yet you could use > > fsockopen with a timeout value to accomplish your goal. > > > > frontend.php: > > > $fp = fsockopen("localhost", 80); > > fwrite($fp, "GET /long_processing_script.php HTTP/1.0\r\n\r\n"); > > stream_set_timeout($fp, 1); > > echo "done!"; > > ?> > > > > long_processing_script.php: > > > ignore_user_abort(); > > set_time_limit(0); > > // do stuff here > > ?> -- Larry Garfield AIM: LOLG42 [EMAIL PROTECTED] ICQ: 6817012 "If nature has made any one thing less susceptible than all others of exclusive property, it is the action of the thinking power called an idea, which an individual may exclusively possess as long as he keeps it to himself; but the moment it is divulged, it forces itself into the possession of every one, and the receiver cannot dispossess himself of it." -- Thomas Jefferson -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Closing a connection to browser without exiting the script
Hi All, Thanks a lot for your numerous answers. I've learned a lot from all your suggestions. Actually, combining all your answers, I'll come up with a solution. To be more explicit on what I want to do, I want in fact to start a script, I want that script to display a page, and then, I want that script not to stop, but I want to be able to access that script back using AJAX calls from the web page. So my script is opening a network connection after having displayed the web page and is waiting for incoming calls. Although no solution given here is fully satisfying (because I don't want to launch antother script, I want to remain in the same) I still can find a way to close my connection to the browser (using a header specifying the connection length) and then go back to my script using AJAX. Thanks a lot for the time you took to propose solutions. Best regards, David. www.thecodingmachine.com Eric Butera a écrit : On 11/1/06, David Négrier <[EMAIL PROTECTED]> wrote: Hello there, I'm having a somewhat unusual question here, and I cannot find any way to solve it. I have a PHP page that displays a message, and then, performs a very long operation. Note that it displays the message first. I do not intend to give some feedback to the user when the operation is done. I've seen I can use ignore_user_abort() to prevent the user from stopping the ongoing operation, but that solves only part of my problem. Because as long as the page is not fully loaded, the mouse cursor in the user's browser is showing a watch. So ideally, what I would like is to be able to close the connection from the server-side, but without using the exit() function, so my script keeps running afterwards. I know I could use a system() call to launch another process to do the processing, but I would like to avoid doing that, because there are many variables in the context that I cannot easily pass in parameter. I also tried to use the register_shutdown_function() to perform my operation after the page is displayed but since PHP 4.1.0, the connection is closed after the function is called Would any of you have an idea on how I could close that connection? Thanks a lot, David www.thecodingmachine.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php If you haven't gone with any of the listed methods yet you could use fsockopen with a timeout value to accomplish your goal. frontend.php: long_processing_script.php: -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Closing a connection to browser without exiting the script
On 11/1/06, David Négrier <[EMAIL PROTECTED]> wrote: Hello there, I'm having a somewhat unusual question here, and I cannot find any way to solve it. I have a PHP page that displays a message, and then, performs a very long operation. Note that it displays the message first. I do not intend to give some feedback to the user when the operation is done. I've seen I can use ignore_user_abort() to prevent the user from stopping the ongoing operation, but that solves only part of my problem. Because as long as the page is not fully loaded, the mouse cursor in the user's browser is showing a watch. So ideally, what I would like is to be able to close the connection from the server-side, but without using the exit() function, so my script keeps running afterwards. I know I could use a system() call to launch another process to do the processing, but I would like to avoid doing that, because there are many variables in the context that I cannot easily pass in parameter. I also tried to use the register_shutdown_function() to perform my operation after the page is displayed but since PHP 4.1.0, the connection is closed after the function is called Would any of you have an idea on how I could close that connection? Thanks a lot, David www.thecodingmachine.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php If you haven't gone with any of the listed methods yet you could use fsockopen with a timeout value to accomplish your goal. frontend.php: long_processing_script.php: -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Closing a connection to browser without exiting the script
I think the Ajax solution that John suggests is the ticket. I did something like this for a site that books reservations for various services. But before the booking can occur, the customer has to get info on the various service options, which are returned from a separate database server. This takes time and what the company needed in the meantime was a message saying that the query was being processed, please wait. My solution was to print the please wait message to the screen, while accessing the database using an Ajax call. Since with an Ajax call you are not waiting for a page to load there's no hour glass or clock, etc., and from the user's perspective there's a seamless experience. I used Perl for my server-side script, but if you prefer PHP you can create a CGI script using PHP and run the same process as your original PHP page. Just put #!/usr/bin/php at the top of the script, before the Myron John Comerford wrote: You could also use an Ajax call from the main window to start the processing without opening a second window. HTH, John Ed Lazor wrote: Here's another idea: I have a PHP page that displays a message, and then, performs a very long operation. Note that it displays the message first. I do not intend to give some feedback to the user when the operation is done. I've seen I can use ignore_user_abort() to prevent the user from stopping the ongoing operation, but that solves only part of my problem. Because as long as the page is not fully loaded, the mouse cursor in the user's browser is showing a watch. So ideally, what I would like is to be able to close the connection from the server-side, but without using the exit() function, so my script keeps running afterwards. -- _ Myron Turner http://www.mturner.org/XML_PullParser/ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Closing a connection to browser without exiting the script
You could also use an Ajax call from the main window to start the processing without opening a second window. HTH, John Ed Lazor wrote: Here's another idea: display your message in the original browser window, launch a new browser window for the processing script, have the window set behind the first with javascript. When your script is finished, have it output javascript that closes the "processing" window. On Nov 2, 2006, at 12:37 PM, [EMAIL PROTECTED] wrote: Original Message Subject: [PHP] Closing a connection to browser without exiting the script From: David Négrier <[EMAIL PROTECTED]> Date: Wed, November 01, 2006 2:24 pm To: php-general@lists.php.net Hello there, I'm having a somewhat unusual question here, and I cannot find any way to solve it. I have a PHP page that displays a message, and then, performs a very long operation. Note that it displays the message first. I do not intend to give some feedback to the user when the operation is done. I've seen I can use ignore_user_abort() to prevent the user from stopping the ongoing operation, but that solves only part of my problem. Because as long as the page is not fully loaded, the mouse cursor in the user's browser is showing a watch. So ideally, what I would like is to be able to close the connection from the server-side, but without using the exit() function, so my script keeps running afterwards. I know I could use a system() call to launch another process to do the processing, but I would like to avoid doing that, because there are many variables in the context that I cannot easily pass in parameter. I also tried to use the register_shutdown_function() to perform my operation after the page is displayed but since PHP 4.1.0, the connection is closed after the function is called Would any of you have an idea on how I could close that connection? Thanks a lot, David www.thecodingmachine.com It's a bit different of an idea, but you could put your reqest to perform the operation in a text file in some directory. Then you have a seperate PHP script that checks that directory for any files that would tell it to operate. Then you tell Cron to run that script every minute, 10 minutes, whatever works best for you. With this idea you can write out the file and then tell the person that you have submitted a request. Then the cron job checks for the request and performs it. You could use a database instead of a file as well with the idea of a queue. That would probably work better, because then if you needed to you can keep track of which user's process was run, and then update the "user" table. Say the "user" table has a column "processing" with three possible values. 0 (not processed), 1 (in processing), 2 (finished processing). You could probably expand upon that idea quite a bit more if you need to. It's no single magic bullet function call, but that's how I'd probably do it. Ray --PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php --PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Closing a connection to browser without exiting the script
Here's another idea: display your message in the original browser window, launch a new browser window for the processing script, have the window set behind the first with javascript. When your script is finished, have it output javascript that closes the "processing" window. On Nov 2, 2006, at 12:37 PM, [EMAIL PROTECTED] wrote: Original Message Subject: [PHP] Closing a connection to browser without exiting the script From: David Négrier <[EMAIL PROTECTED]> Date: Wed, November 01, 2006 2:24 pm To: php-general@lists.php.net Hello there, I'm having a somewhat unusual question here, and I cannot find any way to solve it. I have a PHP page that displays a message, and then, performs a very long operation. Note that it displays the message first. I do not intend to give some feedback to the user when the operation is done. I've seen I can use ignore_user_abort() to prevent the user from stopping the ongoing operation, but that solves only part of my problem. Because as long as the page is not fully loaded, the mouse cursor in the user's browser is showing a watch. So ideally, what I would like is to be able to close the connection from the server-side, but without using the exit() function, so my script keeps running afterwards. I know I could use a system() call to launch another process to do the processing, but I would like to avoid doing that, because there are many variables in the context that I cannot easily pass in parameter. I also tried to use the register_shutdown_function() to perform my operation after the page is displayed but since PHP 4.1.0, the connection is closed after the function is called Would any of you have an idea on how I could close that connection? Thanks a lot, David www.thecodingmachine.com It's a bit different of an idea, but you could put your reqest to perform the operation in a text file in some directory. Then you have a seperate PHP script that checks that directory for any files that would tell it to operate. Then you tell Cron to run that script every minute, 10 minutes, whatever works best for you. With this idea you can write out the file and then tell the person that you have submitted a request. Then the cron job checks for the request and performs it. You could use a database instead of a file as well with the idea of a queue. That would probably work better, because then if you needed to you can keep track of which user's process was run, and then update the "user" table. Say the "user" table has a column "processing" with three possible values. 0 (not processed), 1 (in processing), 2 (finished processing). You could probably expand upon that idea quite a bit more if you need to. It's no single magic bullet function call, but that's how I'd probably do it. Ray -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] Closing a connection to browser without exiting the script
> Original Message > Subject: [PHP] Closing a connection to browser without exiting the > script > From: David Négrier <[EMAIL PROTECTED]> > Date: Wed, November 01, 2006 2:24 pm > To: php-general@lists.php.net > > Hello there, > > I'm having a somewhat unusual question here, and I cannot find any way > to solve it. > > I have a PHP page that displays a message, and then, performs a very > long operation. Note that it displays the message first. > I do not intend to give some feedback to the user when the operation is > done. > > I've seen I can use ignore_user_abort() to prevent the user from > stopping the ongoing operation, but that solves only part of my problem. > Because as long as the page is not fully loaded, the mouse cursor in the > user's browser is showing a watch. > > So ideally, what I would like is to be able to close the connection from > the server-side, but without using the exit() function, so my script > keeps running afterwards. > > I know I could use a system() call to launch another process to do the > processing, but I would like to avoid doing that, because there are many > variables in the context that I cannot easily pass in parameter. > > I also tried to use the register_shutdown_function() to perform my > operation after the page is displayed but since PHP 4.1.0, the > connection is closed after the function is called > > Would any of you have an idea on how I could close that connection? > > Thanks a lot, > David > www.thecodingmachine.com It's a bit different of an idea, but you could put your reqest to perform the operation in a text file in some directory. Then you have a seperate PHP script that checks that directory for any files that would tell it to operate. Then you tell Cron to run that script every minute, 10 minutes, whatever works best for you. With this idea you can write out the file and then tell the person that you have submitted a request. Then the cron job checks for the request and performs it. You could use a database instead of a file as well with the idea of a queue. That would probably work better, because then if you needed to you can keep track of which user's process was run, and then update the "user" table. Say the "user" table has a column "processing" with three possible values. 0 (not processed), 1 (in processing), 2 (finished processing). You could probably expand upon that idea quite a bit more if you need to. It's no single magic bullet function call, but that's how I'd probably do it. Ray -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Closing a connection to browser without exiting the script
On Wed, November 1, 2006 3:24 pm, David Négrier wrote: > I'm having a somewhat unusual question here, and I cannot find any way > to solve it. There is no way to do precisely what you are describing. The best thing to do, imho, is to have the PHP script queue up something in a datbase, text file, or staging directory. Then a cron job (or "Scheduled Task" in Windows) can run through the queue, file, or directory, and process the items, totally independent of the browser script. It's a lot less effort than it sounds like, and de-coupling the "request" for the task to be done completely from doing the task simplifies things, in the long run. -- Some people have a "gift" link here. Know what I want? I want you to buy a CD from some starving artist. http://cdbaby.com/browse/from/lynch Yeah, I get a buck. So? -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Closing a connection to browser without exiting the script
dejavu! This thread was just on the mailing list recently... check the mailing list archives. -Ed On Nov 1, 2006, at 1:24 PM, David Négrier wrote: Hello there, I'm having a somewhat unusual question here, and I cannot find any way to solve it. I have a PHP page that displays a message, and then, performs a very long operation. Note that it displays the message first. I do not intend to give some feedback to the user when the operation is done. I've seen I can use ignore_user_abort() to prevent the user from stopping the ongoing operation, but that solves only part of my problem. Because as long as the page is not fully loaded, the mouse cursor in the user's browser is showing a watch. So ideally, what I would like is to be able to close the connection from the server-side, but without using the exit() function, so my script keeps running afterwards. I know I could use a system() call to launch another process to do the processing, but I would like to avoid doing that, because there are many variables in the context that I cannot easily pass in parameter. I also tried to use the register_shutdown_function() to perform my operation after the page is displayed but since PHP 4.1.0, the connection is closed after the function is called Would any of you have an idea on how I could close that connection? Thanks a lot, David www.thecodingmachine.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php