1

I have a PHP file (accessible on my site) that modifies rows in a MySQL database. If there are a small number of rows, the file loads quickly. However, if there are a large number of rows (over a thousand and climbing), there is a noticable pause and I would assume that this could potentially time out. Additionally, for security reasons I would prefer that the user not interface with this file directly.

I would like to make a PHP file that the user can interface with directly, but when they click ok, this page shows "Finished" immediately. However, it should connect to the first PHP file in the background and allow the rows to be modified. I don't have a lot fo experience with PHP... how should I do this? Thanks.

Jack Humphries
  • 13,056
  • 14
  • 84
  • 125
  • If you will modify these rows using single SQL query, the issue should be probably gone. Otherwise you may wish to just use some kind of queues (simple solution involves adding tasks to one table and reading them by separate script fired using cron). – Tadeck Aug 14 '12 at 04:50
  • @vini Wouldn't that require the client's computer to connect directly to the first PHP file? – Jack Humphries Aug 14 '12 at 04:53

2 Answers2

2

You can do something like that:

echo "<html><h1>Finished!</h1></html>";
// now continue with your logic here
// ...
Nir Alfasi
  • 53,191
  • 11
  • 86
  • 129
  • If the user loads a different page even if the PHP file isn't done, will all of the rows still be modified? – Jack Humphries Aug 14 '12 at 04:54
  • @JackHumphries once the query starts executing - it will continue even if the user bails-out. Try it! :) – Nir Alfasi Aug 14 '12 at 04:55
  • @JackHumphries when user triggered the event to send a `request` to the server, and then moves the page, request is still being `processed` on server but he won't get a feedback unless you have a way to notify them. – Gntem Aug 14 '12 at 04:57
  • @GeoPhoenix according to the description he doesn't want to notify the user - just continue processing the records without making the user wait – Nir Alfasi Aug 14 '12 at 04:58
  • btw, another (and a bit more complicated) option is to implement this: http://stackoverflow.com/a/3833095/1057429 – Nir Alfasi Aug 14 '12 at 05:00
  • Thanks guys. So as long as the user accesses the PHP file and it starts executing, it will finish executing on the server even if they leave the page. Thanks for your help. – Jack Humphries Aug 14 '12 at 05:01
  • Nice! In addition, make sure to increase the time limit. – irrelephant Aug 14 '12 at 05:01
  • @irrelephant What will increasing the time limit do? It won't simply stop when it's done? – Jack Humphries Aug 14 '12 at 05:03
  • See http://php.net/manual/en/function.set-time-limit.php - the script'll only run for 30 seconds by default. (which is probably enough anyway though?) – irrelephant Aug 14 '12 at 05:03
  • @irrelephant It looks like 30 seconds should be enough for now. If I ever did need more time, would calling `set_time_limit(0)` cause any type of issue (such as a memory issue)? – Jack Humphries Aug 14 '12 at 05:06
  • I guess there might be an issue if the script loops infinitely? In your case it doesn't seem like it will. – irrelephant Aug 14 '12 at 05:09
  • @irrelephant Ok, I'll call `exit` anyway. Thanks. – Jack Humphries Aug 14 '12 at 05:27
0

Make the "work" PHP runnable in CLI, and in the "interface" PHP, use this:

(Windows)

<body>
<?php
pclose(popen("start /B php work.php","r"));
?>
<h1>Finished!</h1>
</body>

(Linux)

<body>
<?PHP
pclose(open("php work.php > /dev/null","r"));
?>
<h1>Finished!</h1>

If you need to pass parameters, store them in some place (e.g. database or temp file) and let the "work" PHP read them there.

You can use set_time_limit(0); in the "work" PHP to make it run without time limit.

Passerby
  • 9,715
  • 2
  • 33
  • 50