Home > Backend Development > PHP Tutorial > PHP server actively sends data to the client solution

PHP server actively sends data to the client solution

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-07-29 09:06:01
Original
1532 people have browsed it

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++;
        }
Copy after login

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.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Issues
Ajax submit script submission error
From 1970-01-01 08:00:00
0
0
0
How to use the created hook in the setup script
From 1970-01-01 08:00:00
0
0
0
javascript - script tag async attribute
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template