Home > Backend Development > PHP Tutorial > PHP tutorial. Application example 2_PHP tutorial

PHP tutorial. Application example 2_PHP tutorial

WBOY
Release: 2016-07-13 17:22:33
Original
783 people have browsed it

PHP-Push technology realizes the refresh function
Server push The "push" technology was very popular some time ago, but most of the information on the Internet is cgi. I occasionally saw such an introduction on a French website, but it is a pity that it is in French. I can’t understand it, so I can only understand something from his program. Now I’ll put together an example for everyone to learn from. It can be used for data transmission in chat rooms, news updates on websites, and other frequently updated pages.
In the past, refreshing was done mainly by adding tags to the page.
 
 
Or use javascript's timeout+reload, but this refreshing method depends on the time setting, and cannot continuously transmit data and the time is difficult to determine. A server that uses Server push establishes a permanent connection with the client after the client makes a request, and then the server continuously pushes data packets to the server according to the client's request. Those delays that you are not aware of will make you feel that the server's response and your request have reached the level of synchronization.
Let’s look at an example first and then explain.
http://www.BkJia.com/DownloadFilesa2004-11-14img.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, output directly and exit, IE It's not supported, I haven't tried it yet
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, 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 (when the network is constant), constantly outputting the content of the image , the following is the page called. PHP tutorial. Application example 2_PHP tutorial, then open your Netscape or other non-IE browser to view the calling page. It seems that there is no change. Don’t worry, the next step is how to change the image 1.jpg. Write another php page to test it, such as Get 2 pictures to overwrite 1.jpg according to time (you can think of this method, you can also use copy to overwrite, as long as 1.jpg changes). At this time you will see that the picture on the calling page is automatically updated.
You will find a problem when using it: why the picture does not update automatically. This is because the client has not made a request to the server for a period of time, that is, there is no new content input to the browser for a certain period of time, and a connection timeout may occur. What's the solution? You can add an empty signal to the browser in the execution page, similar to the ftp connection method. In the above page, add a print("") between do...while(1);
After reading this example, you should Now you know how to handle the constantly updated content. The key lies in the processing in the http://www.BkJia.com/DownloadFilesa2004-11-14img.php page (which can be to check the new content and output it). When calling, you can have img, script, frame, etc.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/532317.htmlTechArticlePHP-Push technology implements refresh function Server push The "push" technology that was very popular some time ago, but on the Internet Most of them are cgi information. I occasionally saw something like this on a French website...
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template