Home > Backend Development > PHP Tutorial > php minimize data transfer: store data on client side

php minimize data transfer: store data on client side

WBOY
Release: 2016-08-08 09:34:04
Original
8596 people have browsed it

Exporting programs to other languages ​​is one of the things programmers love, and on the WEB we have
Two different programming environments: client (browser) and server side, according to the definition of HTTP protocol,
We can write server programs that output other languages ​​on the client. We chose as the server
Server-side language and JavaScript are used as client output. In this question we will show you how to use
The solution stores the data on the client side and displays it in chat rooms, news systems or anywhere else you want
The implemented application achieves minimal data transmission between the server and the client (browser).

Requesting support from:
php4
Javascript
Frames

Main idea:
​​​
We have been trying to use PHP to develop a chat room based on HTTP protocol (HTTP CHAT ROOM),
Although the HTTP protocol is not a good protocol for chatting, it is not affected by firewalls or proxies,
PHP can completely implement this function without using JAVA APPLETS. There are two main problems with chat rooms:
First, since IE does not support SERVER PUSH technology, we can only use CLIENT PULL technology (both
Automatic refresh on the client), the second problem is even deeper: because the idea is to refresh on the client, so the server
The server must transmit all messages every time, which means a large amount of data transmission, which is also the main cause of chat room delay
For the main reasons, this article attempts to solve the problem:
Using frames technology (frames) you can refresh the specified page without having to reload other pages, which can
To reduce the amount of service/client (C/S) data transmission. Our model is based on this scheme.
"master" file: defines the framework structure
"Loader" page: Import data
"display" page: display data
In this solution, the "loder" box is automatically refreshed every "x" seconds - the idea is to store the data in the "master"
file, so that the "loder" page only needs to request data from the server that the client does not have. We use timestamp (timestamp)
Note each message to determine which messages must be sent to the client and which ones must not be transmitted. We use PHP4.0 session management (session)
Store the client's last updated timestamp so that the timestamp is visible to both the server and the client. When "loader" file
When data is received from the "master" file (note: the "master" file is large, but it is only transmitted once), refresh the display page ("diaplay")
The "display" page simply calls the javascript function named "displaymsgs()" in the "master" file to display the message. This function is displayed dynamically
The data stored in the "master" file, the following is the general flow chart:
1. The browser requests the "master" page (frame), the "master" page is transmitted from the server to the client (browser), and then "master"
The file generates the framework and transfers the "loader" and "display" pages to the client.
2. On the server side, the "loader" file will be analyzed: If the client does not define the "timestamp" session variable, the "loder" file will
Get all the data from the server and generate javascript code to save the data to the "master" file, and then save the "timestamp" variable as
session variable.
3. The "loder" page generates javascript code to refresh the "display" page.
4. The refresh request causes the "display" page to call the "diaplaymsgs()" javascript function to display data
5. Go back to step 2 every "x" seconds

We can think of it as follows:
================================================== ======
"master" file: very large, defines the displaymsgs() function and stores data and initial values.
"Loader" file: small, retrieves data from the server and generates javascript code
"display" file: very small, calling the "diaplaymsgs()" function of the "master" file
================================================== =======
Note: The "master" file is only sent once
"loder" and "display" files are refreshed every "x" seconds
                                                                                    “loder” may be very large when transmitted for the first time, but it will be very small in the future
The "diaplay" file remains unchanged

If you are still not clear about the above ideas, below we will set up a chat room to explain the method in detail. This chat room is just for a simple demonstration
So it may not be very useful, but you can definitely use this idea to build more complex chat rooms. Remember that this idea is not just for chat rooms. :)

First, please use the MySQL database form:
         ============================
           create table testeable (
timestamp datetime,
​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​ );
============================
The "master" file is as follows:
================================================
                                                                                                               lines=new Array();
function displaymsgs() {
                                                                                                                                                                                                                         for(i=0;i                       display.document.write(lines[i]);
                       display.document.write('
');
                                                                                                                }
                                                                                                       
           
           
           
                                                                                               ==================================================
​ ​ ​ Note: The "form" file is a speech box that provides users with input speech boxes.

"display" file content:
          =====================
                                                                                                             top.displaymsgs();
                                                                                                         ====================
Is the "display" file small? :)
​​​​​
"Loader" file:
         ====================
                                                                               
session_start(); // Use Sessions here!

If(!isset($timestamp)) {
//If "timestamp" is not defined, define it and set it to 0
               $timestamp=0;                                                     } 

         $dab=mysql_connect("localhost","user","passWord"); //Open the database
Mysql_select_db("testbase",$dab);

​​​​//Find information that the client does not have
          $query="select * from testeable where timestamp>'$timestamp'";
         $result=mysql_query($query,$dab);
         $msgs=array();

                   // In this loop, we store the latest news/data and set "timestamp" to the current maximum value
​​​​​
​​​​while($res=mysql_fetch_array($result)) {
              $msgs[]=$res["message"];
If($res["timestamp"]>$timestamp) {
                 $timestamp=$res["timestamp"];
                                                                                             } 
Session_register("timestamp"); // Register the "timestamp" variable

echo '<script>'; <br> <br>               // In this loop we generate javascript code <br>                   // Store the latest data obtained from the server into the "master" page (note: use "top" to point to the top window (master) <br> ​​​​<br> for($i=0;$i<$count($msgs);$i++) { <br />                                                                               top.lines[top.lines.length]="<?php PRint("$msgs[$i]"); ?>"; <br>                                                                                                                                                   <br> ​​​​ //Now we will generate "javascript" code to refresh the "display" page <br> <br> ​ ​ ?> <br> ​​​​​top.display.location.reload();​​<br>                                                                                      <br>           <!-- Note that the "setInterval()" method of javascript is used to make the "loader" page refresh every 4 seconds --> <br> ​​​​<body onLoad="window.setInterval('location.reload()',4000);"> <br>                                                                                 ======================================= <br> ​​​​​<br> "form" page: <br>          ==================== <br>                                                                                 <br> Session_start(); <br> <br> If (!isset($timestamp)) { <br>                          $timestamp=0;                                         } <br> <br>                 // Display the form and generate the "timestamp" variable. <br> If (isset($msg)) { <br>                  $dab=mysql_connect("localhost","root","seldon"); <br>                       mysql_select_db("testbase",$dab); <br>                 $query="insert into testeable(timestamp,message) values(now(),'$msg')"; <br> Mysql_query($query,$dab); <br>                                                                                                                                                                                                                                              // All messages after getting timestamp <br>                     $query="select * from testeable where timestamp>'$tt'"; <br>                     $result=mysql_query($query,$dab); <br>                    $msgs=array();$i=0;$timestamp=0; <br>                                                                                                                                                                                                         while($res=mysql_fetch_array($result)) { <br>                    $msgs[]=$res["message"];  <br>                     if($res["timestamp"]>$timestamp) {  <br>                             $tt=$res["timestamp"];  <br>                     }  <br>                 }  <br>                 session_register("timestamp");  <br>                  <br>             ?>  <br>               <script>  <br>               <?php  <br />                  for($i=0;$i<$count($msgs);$i++) {  <br />             ?>  <br>                 top.lines[top.lines.length]="<?print("$msgs[$i]");?>";  <br>             <?php  <br />              }  <br />               ?>  <br>             top.display.location.reload(); //刷新"display"页 <br>               </script>  
             }  
    ?>  
    

" method="post">  
    Message:  
      
    
  
    =====================================================
    注:我们使得在"form"页提交发言时,马上刷新"display"页面,这可以达到对发言人来说马上发言马上
    显示,更体现实时性。
     
 

以上就介绍了php最小化数据传输:在客户端存储数据,包括了客户端存储数据方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

Related labels:
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