I'm using jQuery to call php functions. This works fine except that when a user clicks on a link (if the PHP function hasn't completed executing), the new page will not start loading until the PHP function finishes.
Is there a setting in jQuery I need to change?
Are you using session handling in your PHP code?
If 2 PHP processes are running using the same session, one process will block (wait) until the second one finishes.
Try calling session_write_close() as soon as possible to prevent this from happening.
See http://php.net/manual/en/function.session-write-close.php for more details.
Related
I have a long running php script that requires $_POST data from the user. I would like the use to be able to submit the data and get a "Successfully started" page. However, after I show the user that page I still want to run a PHP script in the background that will take a while. Any thoughts on how to do this?
Forking, multiprocessing, calling external script? Keep in mind I need to submit the $_POST data to the long running script.
I have a PHP script that loads a function which uses Curl to log itself in to another webpage to get some data. This operation takes about 14 seconds altogether and some users might become impatient. I have a little busy loader to indicate activity.
I have seen this on other websites and want to know what technology I need to implement the following:
How can I send little notification messages back to the main site while the PHP function is running so the user knows about the progress.
Messages could be in the following form:
Logging in to website
Extracting data
Sorting data
Closing connection
You can do it using ajax, do the following.
1) Keep your curl code in the seperate php file.
2) While click the button, using js or jquery call that php file using ajax.
3) Before ajax return the reponse, you can display the div with your message, once you
got response, you can hide the div.
I'm in a weird situation. I just found out that near 50% of my users don't have javascript enabled. Most of my website is based on a lengthy search function. Once users click submit, the function executes, and we send them an email when it's finished - this can sometimes take up to 10 minutes.
My issue is that I don't know how to tell users the button was clicked successfully to start this long function if they have javascript disabled. Any ideas?
Once submitted, spawn a php process that does the search and return immediately with the page saying the job was submitted.
The php reference talks about this, with a comment stating that you would call the process using nohup so that it doesn't die when the caller returns.
php.net exec function
As an idea: if you can detect, that some users don't have javascript enabled, maybe for that users you will show page without javascript, html with simple 'type="submit"' button?
you can check it on server side... when the form is sent to server just check if the submit button value is set.
If a user clicks a button that will make an ajax post call to a php file, then navigates away from the website or closes the window, will the php file run completely until it finishes?
i want the file to download stuff to my server and post a bunch of information into a mysql database. This could take a minute or two. But i want the task to finish completely no matter what the user does.
if the ajax post/request gets sent, will the file run through completely?
thanks for any info.
This should help:
ignore_user_abort(true);
set_time_limit(0); // number of seconds (0 infinity)
Check out the documentation for those two functions for more insight. In general, you shouldn't have a problem, as long as you don't run up against the time limit. This should have all the info you need:
http://us3.php.net/manual/en/features.connection-handling.php
this helped me understand:
http://www.php.net/manual/en/features.connection-handling.php
This is the case. At test.php, I have a function dotask(a,b,c,d);
This function need to do task that need 2-4 minutes to complete.
Task including insert new record into db, curl call to other url to do task and etc.
However, I want test.php to:
Just make sure dotask(a,b,c,d) is called, no need to wait until task completed then only return and continue with the remaining code at bottom.
Reason: test.php is a thank you page. I can't expect the user to wait few minutes for the task to be completed. Because I need to show thank you messages etc at that page to user.
What can I do?
You can't fork a process in PHP without a lot of hackery. I'd recommend using a queue and worker pattern instead. See this answer: PHP- Need a cron for back site processing on user signup... (or fork process)
I seen a few solutions here that require you to pass in the page that you want to run, eg:
BackgroundProcess::fork('process_user.php');
But my case is at test.php, I have dotask(a,b,c,d)
I need to pass in parameters from this site to continue the work.
So should I just pass in this few parameter into a new dbtable call pendingprocess, then at process_user.php , I read from database and continue the task instead?
I can't embed parameter into taskpage.php right...
Another solution I can think of is at thankyou.php I do a body onload ajax call by passing in parameter to process_user.php to perform task. Anyone can advice whether this is a good way?
Will the process stop executing when user click STOP at browser? What if they go to refresh the browser.