Home > Article > Backend Development > PHP server actively sends data to the client solution
1. Use a hidden iframe on the client, and its src points to the control code on the server, such as server.php
2. In server.php, use while to implement a never-ending request, and within the loop, implement breakpoints, such as sleep( 2), which means looping every 2 seconds
3. Output the fragment code to the client in each loop
echo "<script>parent.$('#list').append('<li> xxxxxxxxx</li>')</script>";
ob_flush();
flush();
The overall server-side test code is implemented as follows:
$i = 0; while ($i < 20){ echo '<script> parent.$(\'.contentlist\').append("<div>xxxxxxxxxxxxx</div>"); </script>'; ob_flush(); flush(); sleep(2); $i++; }
The above introduces the solution for the PHP server to actively send data to the client, including the relevant content. I hope it will be helpful to friends who are interested in PHP tutorials.