PHP-Push technology refresh function

Source: Internet
Author: User
Tags ftp connection
PHP-Push technology implements the refresh function. repost the "Push" technology, which was very popular some time ago before Serverpush. However, most of the information on the Internet is cgi, occasionally, I see this introduction on a French website, but most of the information on the Internet is cgi, once in a while, I saw such an introduction on a French website. Unfortunately, I cannot understand French, but I can only understand something in his program. now I will give you some examples to learn. It can be used for data transmission in chat rooms, news updates on websites, and various frequently updated pages.

Previously, we used to add tags to the page.

 
& Lt; META HTTP-EQUIV = refresh content = "time; URL = url" & gt;


Or use the timeout + reload of javascript. However, the refresh method depends on the time settings, and the data cannot be transmitted continuously and the time is difficult to determine. The Server that uses Server push establishes a permanent connection with the client after the client makes a request, and then the Server will continuously push the data packets to the Server according to the client's requests. The latency that you don't notice will make you feel that the server's response and your request have reached the level of synchronization.

Let's take a look at the example and explain it again.

Img. php

<? Php
Set_time_limit (0 );
$ File = "./1.jpg ";
$ Sep = "gIrLsKiCkAsSiTsAySsOoNaTsHiRt ";
If (ereg (". * MSIE. *", $ HTTP_SERVER_VARS ["HTTP_USER_AGENT"]) {
// If it is an IE browser, it will exit after direct output, and IE does not support it. I have never tried it.
Header ("Cache-Control: no-cache ");
Header ("Pragma: no-cache ");
Header ("Content-type: image/jpeg ");
Header ("Content-size:". filesize ($ file ));
Readfile ($ file );
} Else {
Header ("Content-Type: multipart/x-mixed-replace; boundary = $ sep ");
// Here is the key. let's take a look at the MIME type description.

// You will understand
Print "-- $ sep
";
Do {
Print "Content-Type: image/jpeg
";
Readfile ($ file );
Print"
-- $ Sep
";
Flush ();
$ Mt = filemtime ($ file );
Do {
Sleep (1 );
Clearstatcache ();
} While ($ mt = filemtime ($ file ));
} While (1 );
}
? >




This is a permanently executed page (with the network constantly) that constantly outputs the image content. The following is the called page ., Token changes ). Then you will see that the image on the called page is automatically updated.

You will find a problem: why is the image not updated automatically. This is because the client has not requested the server for a period of time, that is, no new content is input to the browser within a period of time, and connection timeout may occur. How can this problem be solved? You can add an empty signal to the browser on the execution page, similar to the ftp connection method. print ("") is added between do... while (1) on the above page ("");

After reading this example, you should know how to deal with constantly updated content. The key is img. processing on the php page (new content can be checked and output). img, script, frame, and so on can be called.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.