Php minimal data transmission: store data on the client

Source: Internet
Author: User
Tags php print
: This article describes how to minimize data transmission in php: store data on the client. For more information about PHP tutorials, see. Exporting programs to other languages is one of the favorites of programmers. on the WEB, we have
Two different programming environments: the client (browser) and the server, according to the HTTP protocol definition,
We can write the program on the client to output the server programs in other languages.
The client language and javascript are output as the client. In this question, we will show you how to use
This solution stores data on the client and
The minimum data transmission between the server and the client (browser) is achieved.

The following requirements are required:
Php4
Javascript
Frames

Main ideas:

We have been trying to use PHP to develop an HTTP-based chat room ),
Although HTTP is not a good protocol for chat, it can be unaffected by firewalls or proxies,
PHP can fully implement this function without using java applets. There are two main problems in the chat room:
First, because IE does not support server push technology, we only use client pull technology (
Automatically refresh the client), the second problem goes deeper: because the idea is to refresh the client, the server
The service end must send all the messages each time, which means a large amount of data transmission, which is also the main
This article attempts to solve the problem:
With Frame Technology (frames), you can refresh a specified page without re-loading other pages.
To reduce the data transmission volume of the service/client (C/S. Our model is based on this solution.
"Master" File: defines the framework structure
"Loader" page: import data
"Display" page: displays data
In this solution, the "loder" box is automatically refreshed every "x" seconds -- our idea is to store data in the "master"
In the file, as long as the "loder" page requests the data that the client does not have from the server, we can use the timestamp)
Each message is recorded to determine which messages must be sent to the client without transmission. We use PHP4. 0 sessions)
Store the last updated timestamp of the client so that the timestamp can be seen on the server and client. When the "loader" file
When the data is received from the "master" file (note: the "master" file is large, but it is transmitted only once), the display page ("diaplay") is refreshed ")
The "display" page simply calls the javascript function "displaymsgs ()" in the "master" file to display messages. Dynamic display of this function
The data stored in the "master" file is as follows:
1. The browser requests the "master" page (framework), the "master" page is sent from the server to the client (browser), and then "master"
File generation framework, and tune 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
Obtain all the data from the server, generate javascript code to store the data to the "master" file, and save the "timestamp" variable
Session variable.
3. The "loder" page generates javascript code to refresh the "display" page.
4. Refresh the request so that the "display" page calls the "diaplaymsgs ()" javascript function to display data
5. Return to step 2 every "x" seconds

We can consider the following:
========================================================== ======================
"Master" File: very large. it defines the displaymsgs () function and stores data and initial values.
"Loader" File: small, fetch data from the server, and generate javascript code
"Display" File: very small. call the "diaplaymsgs ()" function of the "master" file.
========================================================== ========================
Note: the "master" file is transmitted only once.
"Loder" and "display" files are refreshed every "x" seconds.
"Loder" may be very large during the first transmission, but it will be very small in the future.
The "diaplay" file remains unchanged

If you are still not clear about the above ideas, we will create a chat room to illustrate this method. this chat room is just for simple demonstration.
So it may not be very useful, but you can use this idea to create more complex chat rooms. remember that this idea is not just about chatting rooms. :)

First, use the MySQL database form:
======================================
Create table testeable (
Timestamp datetime,
Message text
);
======================================
The "master" file is as follows:
========================================================== ==========
Script
Lines = new Array ();
Function displaymsgs (){
For (I = 0; I Display.doc ument. write (lines [I]);
Display.doc ument. write ('
');
}
}
Script





========================================================== ============
Note: the "form" file is a speech box. you can enter a speech box.

"Display" file content:
==================================
Script
Top. displaymsgs ();
Script
================================
Is the "display" file very small? :)

"Loader" file:
================================

Session_start (); // Use Sessions here!

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

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

// Search for information not available on the client
$ Query = "select * from testeable where timestamp> '$ timestamp '";
$ Result = mysql_query ($ query, $ dab );
$ Msgs = array ();

// In this loop, we store the latest message/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 ';

// In this loop, we generate javascript code
// Store the latest data obtained from the server to the "master" page (note: use "top" to point to the top window (master)

For ($ I = 0; $ I <$ count ($ msgs); $ I ++ ){
?>
Top. lines [top. lines. length] =" ";
}

// Now we will generate the "javascript" code to refresh the "display" page

?>
Top. display. location. reload ();
Script




========================================================

"Form" page:
================================

Session_start ();

If (! Isset ($ timestamp )){
$ Timestamp = 0;
}

// Display the form to generate the "timestamp" variable.
If (isset ($ msg )){
$ Dab = mysql_connect ("localhost", "root", "seldon ");
Mysql_select_db ("testbase", $ dab );
$ Query = "insert into testeable (timestamp, message) values (now (), '$ msg ')";
Mysql_query ($ query, $ dab );
// Get all messages after timestamp
$ Query = "select * from testeable where timestamp> '$ TT '";
$ Result = mysql_query ($ query, $ dab );
$ Msgs = array (); $ I = 0; $ timestamp = 0;
While ($ res = mysql_fetch_array ($ result )){
$ Msgs [] = $ res ["message"];
If ($ res ["timestamp"]> $ timestamp ){
$ Tt = $ res ["timestamp"];
}
}
Session_register ("timestamp ");

?>
Script
For ($ I = 0; $ I <$ count ($ msgs); $ I ++ ){
?>
Top. lines [top. lines. length] =" ";
}
?>
Top. display. location. reload (); // refresh the "display" page
Script
}
?>

========================================================== ==================
Note: We will immediately refresh the "display" page when submitting a speech on the "form" page, which can be used as soon as the speaker speaks.
Display, more real-time.


The above introduces the minimal data transmission in php: it stores data on the client, including the content on the client to store data, and hopes to help friends who are interested in the PHP Tutorial.

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.