Minimize data transfer - store data on the client_PHP tutorial
Outputting programs into other languages is one of the things programmers love. On the WEB we have
two different programming environments: the client (browser) and the server side. According to the definition of the HTTP protocol,
we You can write server-side programs that output other languages on the client. We chose server-side language and javascript as the client-side output. In this question we will demonstrate to you how to use
this solution to store data on the client, and reach the server and client in applications such as chat rooms, news systems or other applications you want to
implement ( browser) minimal data transfer.
Requires the following support:
PHP4
JavaScript
Frames
Main idea:
We have been trying to develop a chat room based on HTTP protocol using PHP (HTTP CHAT ROOM),
Although the HTTP protocol is not a good protocol for chat, it can not be affected by firewalls or proxies.
PHP can completely implement this function without using JAVA APPLETS, for chat rooms There are two main problems:
First, because IE does not support SERVER PUSH technology, so we can only use CLIENT PULL technology (that is,
the client automatically refreshes). The second problem is deeper: because the The idea is to refresh on the client, so the server must transmit all messages each time, which means a large amount of data transmission. This is also the main reason for chat room delay. This article attempts to solve this problem. :
Using frame technology (frames) you can refresh specified pages without having to reload other pages, which can
reduce the amount of service/client (C/S) data transmission. Our model is based on this scheme.
"master" file: define the framework structure
"loader" page: import data
"display" page: display data
In this scheme, the "loder" box is automatically refreshed every "x" seconds ——Our 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)
to mark Each message determines which messages must be sent to the client and which do not need to be transmitted. We use PHP4.0's session management (session)
to store the client's last updated timestamp (last timestamp) so that the timestamp is visible to both the server and the client. When the "loader" file
receives data from the "master" file (note: the "master" file is large, but it is only transmitted once), it refreshes the display page ("diaplay")
and "display" The page simply calls a javascript function named "displaymsgs()" in the "master" file to display the message. This function dynamically displays
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 the "master"
file generates the frame, and the "loader" and "display" pages are transferred to 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 store the data in the "master" file. 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 storage data and initial values.
"loader" file: small, retrieves data from the server and generates javascript code
"display" file: very small, calls the "diaplaymsgs()" function of the "master" file
==== ================================================== ===
Note: The "master" file is only transferred once
"loder" and "display" files are refreshed every "x" seconds
"loder" may be very large the first time it is transferred , but it will be very small in the future
The "diaplay" file will remain unchanged
If you are still not clear about the above ideas, below we will create a chat room to explain the method in detail. This chat room 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 this idea is not just for chat rooms. :)
First, please use the mysql database form:
============================
create table testeable (
timestamp datetime,
message text
);
=========================== =
The "master" file is as follows:
===================================== ===========
<script> <br> lines=new Array(); <br> function displaymsgs() { <br> for(i=0;i<lines.length ;i++) { <br> display.document.write(lines[i]); <br> display.document.write('<BR>'); <br> script> <br> <frameset cols="1" rows="20,60,20" border="0"> 🎜> ================================================== <br> Note: The "form" file is a speech box that provides users with input speech boxes.<br><br> "display" file content: <br> ===================== <br> <script> <br> top.displaymsgs( );:) <br><br> "loader" file: <br> ==================== <br> <?php <br><br> session_start (); // Use Sessions here! <br><br> if(!isset($timestamp)) { <br> //If "timestamp" is not defined, define it and set it to 0 <br> $timestamp= 0; <br> // Find information that the client does not have <br> $query="select * from testeable where timestamp>'$timestamp'"; <br> $result=mysql_query($query,$dab); <br> $ msgs = array (); <br> <br> // In this cycle, we store the latest news/data, and set "TimestAmp" to the current maximum value <br> <br> While ($ resql_fetch_array ($ Result )) { <br> $msgs[]=$res["message"]; <br> if($res["timestamp"]>$timestamp) { <br> $timestamp=$res["timestamp"] ; <br> 🎜> // here 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> "); ?>"; <br> ; <br> top.display.location.reload(); <br> </script>
Refresh every 4 seconds -->
====================================
"form" page:
====================
)) {
$timestamp=0;
}
// 显示表单,产生"timestamp"变量.
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);
// 得到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> <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>
}
?>
=====================================================
注:我们使得在"form"页提交发言时,马上刷新"display"页面,这可以达到对发言人来说马上发言马上
显示,更体现实时性。
As you can see, this chat room is very simple, this is the Minimum Customer/Service (C/S) data transfer skill, using this technology you can achieve the minimum data transfer, Again, remind you: this technology is not meant for chat rooms!
================================================== ========
The above was tested on win98+apache1.3+php4.03 platform!
If you have any suggestions or questions, please contact Feiyang Community (http://feiyschool.51.net)
Or send an email to feiyhy@sina.com
Please give me your advice, thank you!
================================
English original author: Luis Argerich, Alejandro Mitrou
English The URL of the original text: http://www.phpbuilder.com
[The copyright of this article is jointly owned by the author and Aosuo.com. If you need to reprint, please indicate the author and source]

PHP is used to build dynamic websites, and its core functions include: 1. Generate dynamic content and generate web pages in real time by connecting with the database; 2. Process user interaction and form submissions, verify inputs and respond to operations; 3. Manage sessions and user authentication to provide a personalized experience; 4. Optimize performance and follow best practices to improve website efficiency and security.

PHP uses MySQLi and PDO extensions to interact in database operations and server-side logic processing, and processes server-side logic through functions such as session management. 1) Use MySQLi or PDO to connect to the database and execute SQL queries. 2) Handle HTTP requests and user status through session management and other functions. 3) Use transactions to ensure the atomicity of database operations. 4) Prevent SQL injection, use exception handling and closing connections for debugging. 5) Optimize performance through indexing and cache, write highly readable code and perform error handling.

Using preprocessing statements and PDO in PHP can effectively prevent SQL injection attacks. 1) Use PDO to connect to the database and set the error mode. 2) Create preprocessing statements through the prepare method and pass data using placeholders and execute methods. 3) Process query results and ensure the security and performance of the code.

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP makes it easy to create interactive web content. 1) Dynamically generate content by embedding HTML and display it in real time based on user input or database data. 2) Process form submission and generate dynamic output to ensure that htmlspecialchars is used to prevent XSS. 3) Use MySQL to create a user registration system, and use password_hash and preprocessing statements to enhance security. Mastering these techniques will improve the efficiency of web development.

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Zend Studio 13.0.1
Powerful PHP integrated development environment

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.