JonnoTheDev Posted October 24, 2008 Share Posted October 24, 2008 This is something I have had a play with but can never get quite right. Lets say I have a php script that may take 30 seconds to a few minutes to process, maybe it processes a large number of database records and writes them to a text file or multiple files or alternatively reads in a file and creates a database, tables and imports thousands of records. Normally I would opt to write a command line tool that can just sit there and run, then display a message when it is complete. However in this instance I require the script to run through the browser so what I dont want is for the user to sit there for 5 minutes whilst the process is running viewing a blank screen. Basically I need to display maybe an animated gif or a peice of text notifying the user that the process is running. Maybe the text will change when it gets to certain parts of the process. I know the ob functions are designed for this but its where to fit them in that gets me. So lets say we have an example script (process.php) // display a message to the screen that the process is running print "Please be patient whilst process is running"; // start running the process // ========================= // open file fopen(); // read data from database mysql_query() // loop through records while() { // write to file fwrite(); // display a message to the screen that the record is written - clear all other screen messages print "Written record X to file. Please be patient. X number of records remaining"; } // close file fclose(); // display complete message print "All results written to file. File can now be downloaded from ......"; exit(); Where do the output methods fit in to something like this? Quote Link to comment https://forums.phpfreaks.com/topic/129934-solved-output-buffering/ Share on other sites More sharing options...
MadTechie Posted October 24, 2008 Share Posted October 24, 2008 read up on ob_start / flush on php.net.. thats what your looking for ie <?php ob_start(); echo "Please wait"; ob_flush(); flush(); //long wait... ?> EDIT: Oh yeah.. you should be able to just use flush(); before the long process (or even in steps) that would output the html to the page.. but i have had some problems using just that.. read this UC i worked it out then saw that post.. lol Quote Link to comment https://forums.phpfreaks.com/topic/129934-solved-output-buffering/#findComment-673601 Share on other sites More sharing options...
Orio Posted October 24, 2008 Share Posted October 24, 2008 In my opinion this is what you have to do: Use ignore_user_abort() and set_time_limit(0) and just redirect your user to some other page so he could continue and do whatever he wants. This way the user won't have to be stuck in the updating page. This way, if you want to notify the user if the process was finished successfully you could do something like this: Add a column to your users table called "alert". Default value is 0. At the end of your long script, update the alert column to 1 if the task was completed successfully. Other values could symbolize different errors that could occur. The rest of your pages (let's say in your header or whatever, something that is included in all of the other pages), the alert column would be checked to see if a certain task was finished successfully or if it failed. A message would show up in that case (as a javascript pop up for an example). You'd have to update back the alert column to 0 of course. This notifying system is somewhat similar to SMF's PM notification system (which asks you if you want to open the message in a new window when you receive one). Anyway, this solution seems to me much cleaner and nicer. Orio. Quote Link to comment https://forums.phpfreaks.com/topic/129934-solved-output-buffering/#findComment-673603 Share on other sites More sharing options...
JonnoTheDev Posted October 24, 2008 Author Share Posted October 24, 2008 Ok thanks Orio Back to madTechie. This is where I get confused. By inserting the following before the processing part of the script: <?php ob_start(); echo "Please wait"; ob_flush(); flush(); //long wait... ?> It still waits till the process has completed before displaying the text 'please wait' to the screen Quote Link to comment https://forums.phpfreaks.com/topic/129934-solved-output-buffering/#findComment-673609 Share on other sites More sharing options...
MadTechie Posted October 24, 2008 Share Posted October 24, 2008 Just remove the ob_start(); should do it Quote Link to comment https://forums.phpfreaks.com/topic/129934-solved-output-buffering/#findComment-673619 Share on other sites More sharing options...
JonnoTheDev Posted October 24, 2008 Author Share Posted October 24, 2008 Nope Heres what ive done to simulate a process: ob_start(); echo "Please wait"; ob_flush(); flush(); // now sleep for 10 seconds sleep(10); The sleep() function is where I would have processing code. Ive put a sleep in to simulate 10 seconds of processing. However the line "Please wait" is still only displayed after 10 seconds of waiting where I would expect it to be displayed straight away. Quote Link to comment https://forums.phpfreaks.com/topic/129934-solved-output-buffering/#findComment-673621 Share on other sites More sharing options...
MadTechie Posted October 24, 2008 Share Posted October 24, 2008 Just remove the ob_start(); should do it <?php //ob_start(); //commented out echo "Please wait"; ob_flush(); flush(); // now sleep for 5 seconds sleep(5); echo "<br>Done"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/129934-solved-output-buffering/#findComment-673623 Share on other sites More sharing options...
JonnoTheDev Posted October 24, 2008 Author Share Posted October 24, 2008 removing ob_start() doesnt make any difference but I have managed to implement. Quote Link to comment https://forums.phpfreaks.com/topic/129934-solved-output-buffering/#findComment-673761 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.