Script still runs after stopped browser - php

I make a fetching script to get data from web pages. It saves some data to a mysql database.
When I hit stop or close the browser I see through phpmyadmin that it still adds records to infinity.
I killed the process many times but I cant figure out what is wrong to fix it.
Is it script's fault or server's ?
EDIT :
I close the browser i close my pc and the script still runs at the server.
Its not the code or the script.

try to set ignore_user_abort
ignore_user_abort(true);

Related

PHP Stops loading on html, but still processes on server

So I am using a form with a file input to submit an excel file and upload the data into a MySQL database. The script imports everything just fine.
Only problem is that the process seems to timeout on the client browser, even though it seems to continue it's process on the server, but it seems to timeout on the client browser. I have already added all the common fixes, I.E.:
ini_set('max_execution_time', 0);
set_time_limit(0);
ini_set('memory_limit', '-1');
ignore_user_abort(true);
I am currently using a flush(); and ob_end_flush(); to output a message every 1000 records that get scanned for upload.
Unfortunately, after exactly 2 minutes, the page stops loading.
On the flip side, it does continue to upload the records all records get scanned and uploaded regardless of the page. This leads me to believe that somehow the request is continuing to process on the web server, but somehow the browser stops getting info from the server after a certain amount of time. Based on my calculations, the browser stops getting the request after exactly 2 minutes.
I have tried this on both Internet Explorer and Chrome and I get the same result. I have not made any changes to the php.ini file for the security reasons.
Using Old Zend Server with Apache2.

PHP MySQL Query; Max Execution Time and Memory Limit don't work on browser output

So I am using a form with a file input to submit an excel file and upload the data into a MySQL database. The script imports everything just fine.
Only problem is that the process seems to timeout on the client browser, even though it seems to continue it's process on the server, but it seems to timeout on the client browser. I have already added all the common fixes, I.E.:
ini_set('max_execution_time', 0);
set_time_limit(0);
ini_set('memory_limit', '-1');
ignore_user_abort(true);
I am currently using a flush(); and ob_end_flush(); to output a message every 1000 records that get scanned for upload.
Unfortunately, after exactly 2 minutes, the page stops loading.
On the flip side, it does continue to upload the records all records get scanned and uploaded regardless of the page. This leads me to believe that somehow the request is continuing to process on the web server, but somehow the browser stops getting info from the server after a certain amount of time. Based on my calculations, the browser stops getting the request after exactly 2 minutes.
I have tried this on both Internet Explorer and Chrome and I get the same result. I have not made any changes to the php.ini file for the security reasons.
Using Old Zend Server with Apache2.

How do I view the status of my msql query?

I recently executed a mysql query via chrome and closed it out. How exactly does a browser stop a PHP script using the stop button? I thought PHP was a server-side language and could not be controlled through a client.
*UPDATE*I'm now aware of SHOW PROCESSLIST, but this only shows you which threads are running.Is there a SQL command I can use to view a executed query with great detail?
A client (Chrome) has nothing to do with the execution of scripts (PHP) on the server, which in turn have no control over database processes (MySQL query).
Look at your servers process list to see what's going on in general (Apache processes).
Or even better: use SHOW PROCESSLIST; on the MySQL console to find the long running query. You may quit it by using KILL ###ID_OF_QUERY###;.
No, you don't need to keep it open. If you exit a running car, does the car turn off? No.
Sorry, that came off a little snotty, but it wasn't intended too.
The browser, in your case Chrome, is not actually running the actual code. The server is. Thus, once the instruction is executed, closing the browser no longer matters as the request has been given to the server.
two functions are essential for executing time consuming php scripts.
it has nothing to do with the browser (as other users already pointed out)
lookup ignore_user_abort and set_time_limit
The script will continue to execute regardless of browser closure. You can free up your browser by sending the response and allowing the php process to continue on.
ignore_user_abort(true);
$response = "Processing!";
header("Connection: close");
header("Content-Length: " . mb_strlen($response));
echo $response;
flush();
// Insert your lengthy query here
The Answer is it depends, as others mentioned you can check what is running on the mysql server by using the show processlist;
If it is a single query that takes a long time, the it will most likely carry on running after the browser has closed. PHP will have sent the request to the Database and will in effect be sat waiting for it to complete, in turn the browser will be waiting for the webserver to finish building the page/resource that is on that url
so the request is: browser <-> web server (<-> php ) <-> mysql in an ideal world if the user cancels the request everything would tidy itself up nicely, but that in my experience sadly is not the case, if one of the chain decides not to wait, the process that it is waiting for doesn't necessarily know until it tries to send the response back and fails
Come on guys, this is PHP 101. Quoted from the manual:
You can decide whether or not you want a client disconnect to cause
your script to be aborted. Sometimes it is handy to always have your
scripts run to completion even if there is no remote browser receiving
the output. The default behaviour is however for your script to be
aborted when the remote client disconnects.
Execution will stop at the next tickable event after the connection flag is set to ABORTED - which will be detected when PHP attempts to flush output to the client
The current MySQL query will finish executing (as the next event that PHP has control over doesn't occur until after the query has completed), but your script would not make it past that, unless you explicitly set ignore_user_abort. It's always important to account for this when writing code.
There are two ways around this
Set ignore_user_abort to true for the invocation of your script
Do not print anything back to the client until after all of your processing is complete - since a connection closed status won't be detected until output is flushed

Close window STOP php script?

I have tried searching for this forever, but unfortunately I could not find the answer.
I am calculating a whole lot of Pearson correlations on huge matrixes on my server. I do this by opening example.org/testscript.php.
The script itself will terminate about a 2 days after it has started and will perform many INSERT INTO databases for recommendation purposes.
I was wondering when I closed the window of my browser, whether the PHP script would stop or not. I am assuming not; however I am not a 100% sure.
P.S. I have noticed that in some browsers on some computers I will receive an internal server error (500) when starting the script after about 10 minutes. The script itself however was still running as it was still inserting rows in my database.
On this computer however I have not received such error and therefore I was wondering what would happen when I closed the tab.
The php script will terminate after reaching the timeout. You can change the timeout: http://php.net/manual/en/function.set-time-limit.php
The browser will however timeout much sooner, if you are not producing any output. The browser timeout is of course browser-specific.
As Dagon suggested, the correct way would be to execute the php script on the server in the background.

What happens to canceled requests to a PHP page?

When a long-running PHP file is executing, and the user cancels the page request in their browser midway, is the rest of the script ran on the server?
PHP normally terminates script execution once it realizes that the connection is closed:
PHP will not detect that the user has aborted the connection until an
attempt is made to send information to the client. Simply using an
echo statement does not guarantee that information is sent, see
flush().
You can keep your script running using ignore_user_abort().
Also, there is a default time limit for which scripts are allowed to run. You may want to override that using set_time_limit().
I tested this once on a long running process and discovered that the script will continue to run once it has not exceeded the maximum time to execute.
For me (using XAMPP on an older computer), cancelling the web page in the middle of an operation meant to process one million primary records, with a couple of other tables being updated in the process, the MySQL database operations kept going, which tells me that the PHP script kept running on the server. By "cancelling," I mean clicking the stop button on the tab, but not closing the tab altogether. BTW: I had established set_time_limit() to 0 to keep the script from timing out before completing in absence of cancelling.

Categories