HTML not loading while PHP script running in background

php exec
php system
php execute shell script with parameters
php exec not working
running shell command in php with real time output
php shell
php shell_exec not working
php command

I am currently creating a stock market simulation and am working on the moment that the user logs into the simulation. I have a PHP script that will generate a certain price for a company four times and update it into my MySQL database while running. I currently have the following code:


if (isset($_SESSION['userId']))
  $isPlaying = 0;
  while ($isPlaying <= 3)
    $priceTemp = (rand(3300, 3700) / 100);
    $sql = "UPDATE pricestemp SET price = $priceTemp WHERE companyName = 'Bawden';";
    mysqli_query($conn, $sql);
  echo '<h1>Welcome to the simulation</h1>';

I am aiming for these updates to happen in the background once the user has logged into the simulation. When refreshing my database every second, the updated prices are shown which is one of my objectives. However, what I would like it to do is still load the HTML onto the page (to say "Welcome to the simulation") while updating the database with every second with an updated price.

So far, when I log in, I have to wait 4 seconds before the HTML will load. In the future, I hope to have it consisently updating until a certain condition is met but when I have set an infinite loop earlier the HTML never loaded.

What do I have to do to allow the HTML to load once logged in and have the prices being generated and updated in the MySQL database in the background with no delay in either of these tasks happening?

You have a fundamental misunderstanding of how web-based requests work.

What you need to understand is that PHP is a server-side language. PHP generates any combination of HTML, CSS, JavaScript, JSON, or any other forms of data you want and sends it to your web browser when it's finished. While it's doing that, it can also manage data within a database or perform any other number of actions, but it will never send what the web browser can make use of until it finishes setting everything up. So if you're within an infinite loop, it will never finish and therefore nothing will be sent back to the web browser.

To remedy this, you need to use something called "asynchronous JavaScript", more commonly referred to as "ajax". Specifically, you first send some initial HTML to the web browser in one request and let the request end immediately. This allows the user to see something without waiting around for an indefinite period of time. Then, on the web browser end, you can use JavaScript to automatically send a second request to the server. During this second request to the server, you can perform your data processing and send back some data when you're finished to display to the user.

If you want to periodically update what you show the user, then you would repeat that second request to refresh what is shown on the user's webpage.

Any time you see some kind of "real-time" updating on a website, it's not coming from a single, persistently open connection to the web server--it's actually a series of repeated, broken up requests that periodically refresh what you see.

Broken down, standard web request workflows look something like this:

  1. Web browser asks the web server for the webpage. Web browser waits for a reply.
  2. Web server generates the webpage and sends the webpage to the web browser. Web server is done.
  3. Web browser receives the webpage and shows it to the user. Web browser stops waiting for a reply.
  4. Web browser runs any JavaScript it needs to run and requests data from the web server. Web browsers waits for a reply.
  5. Web server processes the request and sends the requested data back to the web browser. Web server is done.
  6. Web browser receives the requested data and updates the HTML on the webpage so the user can see it. Web browser stops waiting for a reply.

As you can see, each series of requests is 1) initiated by the web browser, 2) processed by the web server, and 3) any replies from the web server are then handled by the web browser after the web server is finished up. So each and every request goes browser -> server -> browser. If we add steps 7., 8., and 9. to the above, we will see them repeat the exact same pattern.

If you want to avoid adding JavaScript into the mix, preferring to refresh the entire page every time, then keep your data processing short. Optimize your database calls, fix your infrastructure (make sure your server and database have a LAN connection, that your hardware is good enough, etc.), make your code more efficient... do whatever you need to do to keep the processing time to a minimum.

This is all incredibly simplified and not 100% accurate, but should hopefully help you with your specific problem. The short version of all of this is: you can't show your HTML and process your data at the same time the way you're doing things now. You need to fundamentally change your workflow.

1. PHP: What, Why, and Where?, Not only that, you'll run your script, upload it to your web server, and access your script with a web One of the most difficult things to get a handle on when it comes to PHP Web browsers know all they need to know in order to load and display an HTML Now you've got WampServer running happily in the background. I would like to request a walk through of how to add a loading gif while a php script is running, called from a POST event. I have found various ways and suggestions in my searches, but (as usual with the web) the solutions I could understand did not work and I don't monkey with code I can't follow!

You have to do this in 2 network calls. The first network call should fetch the html. Then you have to use Javascript to fire another call to update your data. Once that api call returns it will update the html.

Program execution Functions - Manual, Also, while in safe mode you must consider the safe_mode_exec_dir directive. For those who want to execute a .php script to run in the background, from a Another command which I've had working in the cmd but NOT with php is: '"C:\my � The problem here is that often the site document is already loaded while the images are still loading. This results in empty sections on your page where an image slowly loads in. Not the thing you

The scheduling model to manage the frequency of a background operation based on the frequency of requests at the front end is a very difficult problem. It's also a problem you don't need to solve. The data doesn't need to be changed when nobody is looking at it. You just need to store when the data was last looked at and apply greater deltas to older data.

Command line usage - Manual, It will be automatically prepended to any PHP file run from the command line. However, in CLI mode the HTTP headers are not printed. told emacs to drop into c-mode automatically when i loaded the file for (in the older cgi versions, it suppressed html output when the script was run) but it myvar; // red background ? Emulating a Long-running Process. The easiest way to make a script go on for a long time without actually doing anything is to run a loop. It also helps to free up processor time by calling a sleep function within the loop. PHP is a great language to use for the purposes of emulating a long-running process and it does indeed have a sleep

system - Manual, For practical reasons, it is currently not allowed to have .. components in the path Finally, I found a comment in a blog by a certain amazing guy that solved my problems. to make it running in background, note that this process is killed when the An example of using the system to call the file command on a linux server. DEVELOPER TIP: If the script does not execute as expected and you need to adjust the script and run it again, click the browser’s back button to return to the Scripts - Background window with the script you ran. Add Sample Users. In this section of the tutorial, you run a script in Scripts - Background to create some sample user records.

Launch PHP Script in Background, When I load the page for the first time every day, it launches an external PHP script with a list Before I figured out how to launch an external PHP script from a web page and run the script in the background, the personal My thought was that the page load would then not wait for the script. Links Without HTML 'A' Tags. If a program is started with this function, in order for it to continue running in the background, the output of the program must be redirected to a file or another output stream. Failing to do so will cause PHP to hang until the execution of the program ends.

jQuery.getScript(), Success Callback. The callback is fired once the script has been loaded but not necessarily executed. Scripts are included and run by referencing the file name:� javascript – How can I get the background image URL in Jquery and then replace the non URL parts of the string jquery – Angular 8 click is working as javascript onload function javascript – window.addEventListener causes browser slowdowns – Firefox only.

  • Output buffering may help
  • Note: The object-oriented interface to mysqli is significantly less verbose, making code easier to read and audit, and is not easily confused with the obsolete mysql_query interface. Before you get too invested in the procedural style it’s worth switching over. Example: $db = new mysqli(…) and $db->prepare("…") The procedural interface is an artifact from the PHP 4 era when mysqli API was introduced and ideally should not be used in new code.
  • Thank you for this explanation, I will now read up about Ajax and research how to create the requests that I need.