If I do an AJAX call, would the AJAX call be canceled if the user pressed "ESC" or leaved that page?
If the AJAX call gets canceled, would my PHP script continue to run until it finished what it was doing (provided time limit doesn't get reached or any other server configuration stops.), or would that process get killed at the same time as the Apache child it belongs to?
If the process does get killed with the Apache child even if it didn't finish, what would be the best way to keep that alive be or what other options should I consider? (ZendX_Console_Process_Unix not an option).
Thanks!
Later discoveries:
The AJAX call actually gets canceled if I hit "ESC" in Firefox (checked in firebug). The PHP process continues and is not affected by hitting ESC or closing the tab.
If the server process starts before the user ends the ajax call (closing the window, or moving to another website) then it will be carried out until its final result.
But an interrupted ajax call (meaning the transmission of data was not completed) the server will not process the call.
Note: hitting ESC will not end an ajax call per se, unless you javascripted that keypush behaviour.
One note... if you invoked your AJAX call from a hyperlink, using the href attribute, then YES, the user can press ESC and stop the transmission.
This can be canceled by pressing ESC
This can NOT be canceled by pressing ESC
Related
A few times a day, when our website is busy, we have more than 1000 requests per second on our database.
During these busy times, when a user clicks on an element and makes an AJAX call and then clicks another element and makes another AJAX call, the second call will wait for the response of the first call.
How can I have this AJAX calls run simultaneously? Is this time space between two calls because of the server being busy? If yes, how can we handle simultaneous AJAX calls?
I had similar problem as you do in the past. Then it was open session on server side. Even if ajax call was async, then it have to wait for server because of lock on session file.
Try to close session write when you do not write anything and then, check you ajax again.
Here you have reference to proper method: session_write_close
I have a situation where I need to run some PHP, specifically where I need to send out a SOAP request and wait for the response, and then do something with that, however sometimes these requests can be slow and take up to 9 seconds.
Now I don't really want the user sitting there waiting 9 seconds for this to complete.
Basically the user flow is..
User comes to payment page
User clicks button to pay via payment gateway (Paypal)
User then returns to the site (SOAP request and all that need to be finished at this stage)
I was thinking of running it with the Paypal IPN notification but then didn't think it would be finished by the time the user got back to the site.
So, I'm wondering if I could send off a call when the user hits the first page via Ajax and have it run whilst the user is submitting payment and by the time they get back to the site it should be done -- it's not a big deal if they don't end up going through with payment, so I'm not worried about running this code before confirming payment.
My question is, if I fire this off to be run via AJAX, will the code still be executed if the user leaves the page before it has finished? If not, any ideas?
Once a request is sent to the server, irrespective of whether you navigate away from the page the server side of the request will get completed.
The only thing that will not happen is the execution of client side callback method.
If you are using php , there is a php.ini setting ignore_user_abort that tells php what to do when the client aborts the request.
Its value is false by default.
http://www.php.net/manual/en/misc.configuration.php#ini.ignore-user-abort
I am trying to set up a comment system, in which when the users sends the comment it is displayed on their screen as if it were already stored on the database.
My question is: what would happen if the users send comments and then navigate away
(or most specifically close the window immediately) or they lose connection after the ajax post?
On the code side I have ajax({})...
Then I have code that takes the user input from the textarea and adds it to a div.
This means that the user gets to see the comment they entered instantaneously. But I would like to be sure if the server will get the post info even if the connection was lost, window was closed or the user navigated away.
More info for the question:
A user sends a post to the server with 1mb of values, then right after one millisecond
he/she clicked on the button that made the post the browser window was closed.
Does the server receive and parse the response with ignore_user_abort(true);
inside the file; was the post info received?
Any difference if it were get instead of post for this case?
Assume website.com?myget=value
Trying to connect then closing the window immediately, on a browser window for example,
just hitting that on the address bar and then closing very right away, imagine it to
be automatically.
step 1 go to website.com?myget=value (don't wait at all for any server response, just
straight away (a millisecond or whatever it takes the script to do so) close completely
the window.
Would $_GET['myget'] be received server side at index.php of website.com?
This is a UX problem, not a technical one. What you want to do is display the new comment only after it has been stored. The workflow should go something like this:
User types message
User clicks on "submit" button
System grays out "submit" button and displays a message that reads,
"Posting..."
When System can confirm that the message has been successfully
stored, System will remove "Posting..." text and display actual new
message.
This way the user knows not to close their browser or navigate away until the request is done.
Alternatively you can you onbeforeunload to warn your users to wait before closing the browser or navigating away. Workflow being something like:
Prerequisite: You have a persistent counter somewhere (cookie, local storage, hidden field, etc). When the page loads, it starts at 0.
User types message
User clicks on "submit" button
AJAX request is sent
Counter is increased by 1
Request is complete, you get a response (whether it's successful or not - error handling is another issue), decrease the counter by 1
If at any point, the unload event is triggered, System will check the counter. If greater than 0, warn the user that their request has not been completed and that they might loose their comment (a-la-Gmail).
Will add my five cents. With ajax({}) you will ask browser to start communication with your server. It needs some time to establish connection (ping time) and send data to server. Both parts require some time to be completed. In order for PHP to start execution, browser must sent all data it has to send. No matter is it POST or GET. If user will break sending procedure (browser crashed, tab closed, computer turned off) PHP will not even be started. For instance, you can try to send some large file and see with a debugger when PHP script will be started - only after file is delivered completely (you can even close your browser before file is uploaded and see if your script is executed at all). It makes sense to start PHP execution only after all data delivered to server and ignore connections broken before data delivered. Otherwise there could be problems with data being corrupted. And nobody wants that. Plus, imagine that PHP is started before everything is delivered to server: you would never be sure that $_POST["something"] is not available because it was never entered by user or its data is not yet delivered.
There is no difference if you are using regular form submit or XMLHTTPRequest. In both cases browser need some time to establish connection with server and pass a data to it.
In most cases, whatever action you perform against the server will continue to be executed until such a point as the PHP running on the server tries to output results back to the browser. Only at this point will PHP check whether the connection still exists and do whatever is should do based on the user abort settings. So, if for example you wanted to receive the post, update a database entry, and then echo back some sort of success message, the database activity should continue so long as you have not made any output before the database is queried.
POST vs. GET makes no difference in this behavior.
User clicks some link which execute some ajax request - lets say this request takes 20 secs.
Before the request is complete user clicks oder link which redirects (no ajax) whole page to another page.
What will happen with ajax request? It will be always completed on server side but the response wont come anywhere? Or maybe ajax request on server side will be immidiately "killed?
I ask because I have some script which takes some time to run but user doesn't have to now the result - it's just fire and forget - maybe there is even some option in ajax to force it not to send any response?
The browser should kill the AJAX request, closing the connection to the server; however, this does not mean that your processing on the server is necessarily killed too: ignore_user_abort()
The server will complete the request, unaware that the client has "moved on." The server will return the response to the client like it normally does. The client will simply ignore the response.
So expect everything server-side to happen as normal, so the "fire and forget" method will work (since the client has moved on and has "forgotten"). But if you want to do anything client-side in response (which would negate the "forget" part) then there's no way for the new page to intercept the response. The browser will ignore it.
I wrote a script that has no time limit and max_execution_time is 0
I should work until the end of it's job or on User abort (canceling from browser).
How can I be sure when user aborts page load from browser the script is killed by server?
Does this kills script immediately or __destructor of classes are called before?
Assuming this is called by ajax, you could add a event handler for when the user leaves the webpage that sends an ajax request that is handled by a script that kills the other script.
This could be implemented by having the script create a file when it starts. Then, every one and a while it checks to see if that file is still there. If it is it continues if not it quits.
The trick is finding the filename. The way I do this is give every page request a unique user ID. And both the 'worker' script and the 'kill' script are called with the same user ID in the url (GET).
To my understanding there is no way with php to stop script execution when a user aborts the page load on their end. I am assuming by "Aborting" you mean clicking stop or closing the browser while the page is still loading.