PHP Minimized data transfer: Store the information on the client

Source: Internet
Author: User
Tags sessions
Exporting programs to other languages is one of the things programmers love, on the web we have
Two different programming environments: client (browser) and server side, according to the definition of HTTP protocol,
We can write a service-side program that outputs other languages in the client, and we select
Terminal language, JavaScript as the client output. In this question we will show you how to use
The program stores the data on the client, and in a chat room, news system, or other
The implementation of the application on the service side and the client (browser) to achieve the minimum data transfer.

The following support is required:
PhP4
Javascript
Frames

Main ideas:

We've been trying to develop a chat room based on HTTP protocol (HTTP chat rooms) in PHP,
Although the HTTP protocol is not a good protocol for chatting, it can be unaffected by firewalls or proxies.
PHP is fully capable of doing this without using Java APPLETS, and there are two main problems with chat rooms:
First, because IE does not support server PUSH technology, we only use the client pull technology (both
Client Auto-Refresh), the second problem is deeper: because the idea is refreshed on the client,
All messages must be delivered at the end of the service, which means a large amount of data transfer, which is also the main
For reasons, this article attempts to resolve the issue:
Using frame technology (frames), you can refresh the specified page without reloading other pages, which
To reduce the amount of service/client (c/s) data transfer. Our model is based on this scenario.
"Master" file: Defining the framework Structure
"Loader" page: Import data
Display page: Displaying the data
In this scenario, the "Loder" box is automatically refreshed once per "x" seconds--our idea is to store the data in "master"
file, so that the "Loder" page simply requests data that the client does not have on the server, we use timestamp (timestamp)
Remember each message to decide which messages must be passed on to the client that do not have to be transmitted. We use PHP4. 0 Session Management (sessions)
Stores the timestamp of the last update of the client (timestamp) to make the timestamp visible to both the server and the client. When the "loader" file
Receive data from "Master" file (Note: "Master" file is large, but it is only delivered once), refresh the Display page ("Diaplay")
The display page simply calls the JavaScript function named "Displaymsgs ()" In the "master" file and displays the message. The function dynamically displays
The data stored in the "master" file, the following is a rough flowchart:
1. The browser requests the "master" page (frame), the "master" page is transferred from the server side to the client (browser), and then "master"
The file generation framework, and the "loader" and "display" pages are tuned to the client.
2. On the server side, the "loader" file will parse: If the client does not define a "timestamp" session variable, the "Loder" file will
Get all the data from the server and generate JavaScript code to store the data in 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 show the data
5. Every "x" seconds back to step 2

We can think of the following:
========================================================
"Master" file: Very large, defines the DISPLAYMSGS () function and stores the data and initial values.
"Loader" File: small, retrieving data from the server, generating JavaScript code
"Display" File: Very small, called "diaplaymsgs ()" Function of "master" file
=========================================================
Note: the "master" file is only delivered once
"Loder" and "display" files are refreshed every "x" seconds
"Loder" may be very large when first transmitted, but it will be very small in the future.
The "Diaplay" file has not changed

If you are not sure about the above ideas, we will create a chat room to explain the method, this chat room is just for a simple demonstration
So it may not be very useful, but you can use the idea to build more complex chat rooms, remembering that this idea is not only used with chat rooms. :)

First, you use the MySQL database form:
============================
CREATE TABLE Testeable (
Timestamp datetime,
Message text
);
============================
The "master" file is as follows:
================================================






==================================================
Note: the "form" file is a statement box that provides the user input to the speaker box.

"Display" File contents:
=====================

====================
Is the "display" file very small? :)

"Loader" File:
====================

Session_Start (); Use Sessions in this!

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

$dab =mysql_connect ("localhost", "User", "PassWord"); Open 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 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"); Registering the "timestamp" variable

Echo '




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

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

Session_Start ();

if (!isset ($timestamp)) {
$timestamp = 0;
}

Displays the form, producing a "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 the 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");

?>

}
?>

=====================================================
Note: We enable the "display" page to be immediately refreshed when submitting a statement on the "form" page, which can be reached immediately after the speaker
Display, more real-time.


The above describes the PHP minimal data transfer: In the client to store data, including the client to store data content, I hope the PHP tutorial interested in a friend helpful.

  • 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.