How to get progress message in a php script

OK I know PHP is server-based and completes processing before page result s presented as HTML code. But I have some scripts with relatively long processing times since they are accessing a large database and I want to report on progress. The options I have considered are

  1. Split the large script into smaller ones and echo status at end of each script. Each script then calls next in sequence - this makes maintenance more complicated.

  2. Keep a large script but have it complete one section, echo progress and then call itself with a var to know which section to process next - not sure if it will cause problems with a script calling itself multiple times and not sure best way to pass the var.

  3. Look at another technology such as Ajax, never used it, reluctant to learn another discipline but willing if it gives a solution.

I am not looking for someone to give me a script or anything like that but just advice, comments and ideas on my current options or other ideas to research


I can’t help thinking, that if the scripts are taking so long to process that you need a progress report; you should first check that your database is properly optimised, with appropriate indexes, etc.

1 Like

Unless the accessing they’re doing is done in stages, i doubt you’d be able to get anything meaningful from the PHP script anyway…

No, database is fine, I just want to investigate a way of displaying progress thanks

That’s what I was thinking, separate main script into stages…

I have a similar situation, and had coded up a solution that worked with emptying buffers, etc. Then when PHP7 was installed it all broke and I realized I was probably relying on some quirk of the I/O rather than anything standard. So I haven’t fixed it. The only thought I have is some AJAX type of solution, but haven’t tried that.

In my case, I code for fun, so I don’t have a pressing need to solve this, which is why it’s been sitting for a while :slight_smile:

How long are we talking? If minutes, wouldn’t it be OK to just refresh the page every so often? If hours, wouldn’t it better to send an email once it’s done so people can get on with their lives in the meantime, rather than looking at some progress bar the entire time? :wink:

Also, how are you running the longer scripts now, given that PHP times out after 30 seconds by default?

I’m curious to know more about process details and the amount of data, etc.

I download three XLS spreadsheets with about 50,000 records containing ref, author, book title, price and a couple of other fields. Once downloaded the data is converted to CSV files and then imported into MySQL tables. Originally processing took over a minute but now only takes about twelve seconds. The processing used to be done daily but is now every hour. This time could also be drastically reduced if CSV files were used instead of XLS spreadsheets.

I’ve been struggling for a long time with a 6$ VPS hosting with a single CPU with 1GB of memory and weekly backup. Just invested in a new German based server…

4 vCores, 8 GB RAM & 200 GB SSD for just $6.99 with free Snapshot backups and email!

… looking forward to comparing perfo

Well, my problem is related to Wordpress. I know Wordpress is known to be slow, but backing up is even worse. I know the tricks for cleaning the database and reducing file size and I can log in to server and manually export tables. But exporting each table and creating zip file just takes time and whilst not imperative, it would be nice to be able to glance at the screen every now and then and get reassurance it is still running and some idea where it is up to.

We are talking around 3-5 minutes currently. I am not quite sure what you mean by the above suggestion to refresh -sorry.

The script basically performs several separate tasks so does not time out

Doesn’t your hosting provider make backups for you? Backups really are a solved problem that you shouldn’t try to solve with custom code.

1 Like

Backups are available but too costly for me on my small, retired, budget. My code works fine, I just want to try and introduce a couple of progress alerts.

There are two common algorithms for thouse problem.

  1. AJAX starts the server script, script makes some part of job and responses with actual state. Than JS on client gets response, shows state in progress bar and starts server script anew. And so on untill job is done.

  2. AJAX starts the server script, script makes some part of job and starts itself again with a popen(). And so on untill job is done. Client sends AJAX requests, that checking job state (e.g. from DB), and than client shows this state in progress bar.

1 Like

That sounds promising, I will have a look at Ajax and see where it takes me. Thanks