Php-push Technology realizes refresh function

Source: Internet
Author: User
Tags add exit ereg flush ftp connection header html page sleep
"Push" technology that was hot for a while before the Server push was refreshed, but most of the internet is CGI data, and occasionally see a French website there is such an introduction, but the 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, Web site news updates, and so on all kinds of frequently updated pages.

Previous refreshes were mainly tagged on the page.

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


Or use JavaScript's timeout+reload, but this refresh method depends on the time set, not continuous data transmission and time is not OK. Servers with server push create a permanent connection to the client after a client makes a request, and the server continues to push packets to the server based on the client's request. Those delays that you don't perceive 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 examples 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, 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 is the key oh, 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 (constantly in the case of the network), constantly outputting the contents of the picture, the following is the page called.
In use you will find a problem: how the picture does not automatically update. This is because the client has not made a request to the server for a period of time, which means that no new content has been entered into the browser for a certain period, and a connection timeout may occur. What solution? You can add an empty signal to the browser in the execution page, similar to the FTP connection, and add a print ("") between Do...while (1) on the page above.



The above is the turn of the section, because more interested in Google, looked for a bit, you 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. 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 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 With my php script to make the stream viewable under IE. Beware that's 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 are no blank lines outside in?> script. That would 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 so references the image to compensate for IE ' s lack of "innovation":

<HEAD>
?
if (Ereg ("MSIE", $HTTP _server_vars["Http_user_agent")) {
echo "<meta http-equiv=\" refresh\ "content=\" 4;\ ">\n";
}
?>
</HEAD>



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.