PHP-Push technology for refresh

Source: Internet
Author: User
Tags ereg ftp connection
Serverpush was a very popular "push" technology some time ago, but most of the information on the Internet is cgi. sometimes I see such an introduction on a French website. Unfortunately, I cannot understand French, 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 many other frequently updated page Server push technologies that have been very popular some time ago, however, most of the information on the Internet is cgi. sometimes I see such an introduction on a French website. Unfortunately, I cannot understand French, but I can only understand something in his program, let's take a look at an example. 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 ("");



The above is the transfer part. For more information, please refer to the following documents on GOOGLE.

Requirements
Works with Apache-1.3.14/PHP4.0.3pl1 server and varous Netscape clients. Probably extends other server combos. Tested on Netscape 4.7x and 6.0/Mozilla.
Does not work with ie. internet Exploiter does not support x-mixed-replace server-push as far as I know. if a browser has "MSIE" in its User-Agent string the script will display one image and exit.

Update 20020108: Poked around freshmeat for a bit and found Andy Wilcock's Cambozola java applet which seems to work well with my php script to make the stream viewable under IE. beware that the current version doesn't work under "Name-based" virtual hosts but I'll have a patch for it soon.
Source
Download

$ File = "./latest.jpg ";
$ Sep = "gIrLsKiCkAsSiTsAySsOoNaTsHiRt ";

If (ereg (". * MSIE. *", $ HTTP_SERVER_VARS ["HTTP_USER_AGENT"])
{
# If IE, spit out one pic and exit
Header ("Cache-Control: no-cache ");
Header ("Pragma: no-cache ");
Header ("Content-type: image/jpeg ");
Header ("Content-size:". filesize ($ file ));
Readfile ($ file );
}
Else
{
# If not IE, give the browser a try
Header ("Content-Type: multipart/x-mixed-replace; boundary = $ sep ");
Print "-- $ sep \ n ";
Do {
Print "Content-Type: image/jpeg \ n ";
Readfile ($ file );
Print "\ n -- $ sep \ n ";
Flush ();
$ Mt = filemtime ($ file );
Do {
Sleep (1 );
# We won't output the same image twice.
Clearstatcache ();
} While ($ mt = filemtime ($ file ));
} While (1 );
}
?>


Make sure there are no blank lines outside In your script. That will cause screwey headers to be sent too soon.
Reference the script in your HTML page as if it was an image:



Use this bit of PHP on the page that references the image to compensate for IE's lack of "innovation ":


If (ereg ("MSIE", $ HTTP_SERVER_VARS ["HTTP_USER_AGENT"]) {
Echo" \ N ";
}
?>

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.