# Disconnect Browser in PHP



## cassamine (Nov 22, 2002)

Hey All,

I have this situation where I must disconnect the connected browser/user and continue working in my PHP script.
Lets say a PHP page is opened in firefox and some data is passed onto it via a query string.

Now i want to disconnect the browser after echoing back OK or THANKS etc so it may not keep waiting till some time-intensive tasks, which follow, are completed. I can easily keep session active after browser disconnection via ignore_user_abort() and set_time_limit(0) but I really couldn't google anything on how to explicitly disconnect the user from server-side. 

Any help or ideas?

Thanks,
Johl


----------



## Nick Tompson (Jul 12, 2008)

You can't - however you could use ajax to effectively run the intensive tasks in the "background", and the same methods of ignore_user_abort() can be used to keep the ajax called script to continue running after user closes browser.

The page could print your OK message and then load some client script to start up the intensive tasks on the server - in this way it will look to the user that the page has finished loading.
Sometimes the solution isnt a combination screwdriver and hammer, but just to use a screwdriver and hammer - in this case two scripts, one to show the OK message and load the client code which calls a second script to do the intensive tasks.


----------



## cassamine (Nov 22, 2002)

Are you positive about the "You can't" thing? cuz ajax doesn't solve the issue, it just hides the processing as the ajax query would still keep on waiting for the response from server till it times out.

Also in my scenario the browser isn't firefox; it is a c# based compact-framework application running on a WM6 based mobile which would have no user interactivity, its sole purpose is to keep on uploading small chunks of data to an online php server over and over again. Now as the c# app it isn't multi-threaded or directly socket based I need to somehow disconnect it so it can create another parallel request while earlier chunk of uploaded data is being processed by the php page.

Thanks
Johl


----------



## Nick Tompson (Jul 12, 2008)

If its only small chunks, how long it the wait?
You can't make a disconnection of the client from the server - that would be doing something that is exclusively for the user to control. (Unless someone can correct me if I am wrong, but in my experience with PHP, there isn't a way).

Depending on the size of the chunks to upload, would it be practical to just wait until the data is finished uploading, and then create another request to upload the next chunk of data?


----------



## cassamine (Nov 22, 2002)

Nick the chunk of data is quite small about 200 bytes (+/- 50 b) but the processing on top is quite large and time consuming. The wait averages to about 3 minutes. Any ideas ? Don't think of simple insert statements, think of voice data analysis. Also, as the data is acquired by C# app at irregular intervals I can't even club multiple chunks for combined uploads.


----------



## Nick Tompson (Jul 12, 2008)

What are the chances of using something else other than a PHP script?
My experience is limited in other areas, but perhaps what you want can be done with a different technique.


----------



## cassamine (Nov 22, 2002)

Well I think there isn't a method to disconnect a client's connection in PHP  sad.
I'll leave this question unsolved for a while for someone else to have a look & get some bright ideas.

Thanks for your input Nick.


----------



## Nick Tompson (Jul 12, 2008)

Not a problem.


----------



## cpscdave (Feb 25, 2004)

if I understand your problem correctly.

The user makes a request which will take some time to complete. 
You want the browser to stop loading while the server does the work. 
Then after the work is done you want the browser to load the result?

IF thats the case you'd have to do it via ajax. I dont know of any other method to get this behaviour.

OR

do you want:

User sends the request, 
Browser says thanks, 
and server chugs away in the background, sending no further info the the user?

I had a similar issue a cpl years ago. The system would generate PDF's that were taking 3-4 minutes. as a complete cludge we had the script that fired off the pdf generate call a command via http://php.net/manual/en/function.exec.php

It worked but was ugly


----------

