PHP Tutorials. Application Example 2

Source: Internet
Author: User
Tags add ftp connection header key readfile client
Tutorial | Application instance Php-push technology to realize refresh function
Server push some time before a very hot "push" technology, 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.

After reading this example, you should know how to deal with the constantly updated content, the key in the Img.php page processing (can be to check the new content and output), call can have IMG, script, frame and so on.


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.