Php-push Technology for refresh function _php

Source: Internet
Author: User
Keywords Refresh implement Technology File header page
Tags ereg ftp connection readfile
Server push a period of time before the hot "push" technology, but most of the internet is CGI information, occasionally see a French website there is such an introduction, unfortunately French can not understand, only from his program to understand something, now finishing an example out of everyone to learn. Can be used for chat room data transmission, news updates on the site, and so on various types of frequently updated pages.

Before doing the refresh mainly by tagging on the page.

 
< META Http-equiv=refresh content= "time; Url=url ">


Or use JavaScript's timeout+reload, but this method of refresh depends on the time setting, not the continuous data transmission and the time is not OK. Servers with server push make a persistent connection to the client after the client makes a request, and the server pushes the packet to the server based on the client's request. Those delays you don't notice will make you feel that the server's response and your request have reached a level of synchronization.

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

img.php

< PHP
Set_time_limit (0);
$file = "./1.jpg";
$sep = "Girlskickassitsayssoonatshirt";
if (Ereg (". *msie.*", $HTTP _server_vars["Http_user_agent"])) {
If it is IE browser, the direct output on the exit, IE does not support oh, I did not try to come out
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's the key Oh, look at the MIME type description

You'll 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 (the network is constantly in the case), constantly output the contents of the picture, the following is the page called. , then open your Netscape or other non-IE browser to view the call page, as if nothing changes ah, don't worry, Then how to change the 1.jpg this picture, write an additional PHP page to test it, such as to get 2 pictures by Time to cover 1.jpg (this method to think, with a copy overwrite also line, as long as 1.jpg has changed). You will then see the image of the calling page automatically updated.

In use you will find a problem: how the picture is not automatically updated. This is because the client has not requested a server for a period of time, that is, there is no new content to the browser for a certain period of time, a connection timeout may occur. What way to solve it? You can send an empty signal to the browser in the execution page, similar to the FTP connection mode, and the page above Do...while (1) to add a print ("");



The above is the part of the turn, because more interested in Google on the search, we can look at the following information.

Requirements
Works with APACHE-1.3.14/PHP4.0.3PL1 server and Various Netscape clients. Probably many other server combos. Tested on Netscape 4.7x and 6.0/mozilla.
Does not work with IE. The Internet exploiter does not the X-mixed-replace Server-push as far as I know. If a browser have "MSIE" in its user-agent string the script would display one image and exit.

Update 20020108:poked around Freshmeat for a bit and found Andy Wilcock ' s Cambozola Java applets 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\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 is no blank lines outside the In your script. That'll cause Screwey headers to be sent too soon.
Reference the script in your HTML page as if it is an image:



Use this bit of PHP on the page, 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.