How to improve web page loading speed in php

不言
Release: 2023-04-02 13:44:02
Original
2176 people have browsed it

This article mainly introduces how to improve the loading speed of web pages in PHP. It has certain reference value. Now I share it with you. Friends in need can refer to it

1. Reduce page requests:

From the perspective of WEB operation principles, IIS requests are stateless. Connections and shutdowns are always ongoing on the server side. If server requests can be reduced, the total time will be reduced.

When I downloaded the picture of the login page of 163 Mailbox before, I found that they only used one picture to complete all the pictures on the entire page. At that time, I was puzzled. This was different from the websites I usually build. However, by doing this to reduce the number of browser requests, using httpwatch you will find that the total request time is greatly reduced. With the same idea, when making css styles and javascript codes, you should also put them in one file as much as possible to reduce WEB requests.

2. Page compression technology allows us to slim down JS and HTML codes.

Jquery is the most lightweight class library of JS. Its original class library is 242KB, but after compression, it is only 91.6KB. Common compression tools include Google Closure Compiler, YUI Compressor, JsPacker, and gzip. I usually use gzip because it has the highest compression rate. After compressing with jdk, it will remove some spaces and replace our long variable names with short letters.

3. Separation of style, behavior and structure.

When visiting the page for the first time, the user can download the CSS and JS code. Although this will slow down the loading speed of the web page for the first time, subsequent pages will be much faster. When users visit other pages of this website, other pages also reference this CSS style and JS code. The browser finds that it has been downloaded and does not need to download it next time. This achieves the effect of one download and subsequent speed.

4. Use stored procedures

Before running the stored procedure, the database has analyzed its grammar and syntax, and provided an optimized execution plan. This compiled process can greatly improve the performance of SQL statements. Since most of the work of executing the SQL statement has been completed, the stored procedure can be executed very quickly. This precompiled SQL statement can be executed directly and saves a large part of the query time.

5. Some details:

Put the CSS style at the head. If there is a lot of JS, put the js code at the end. In this way, the DOM structure of the page can be loaded first as much as possible, and then the user experience can be improved. If the JS is too large and the page is not fully loaded, how can there be any user experience?

6. Proper use of AJAX

Nowadays Ajax seems to be a bit mythical. It seems that as long as the web page has Ajax, then there will be no efficiency problem. This is actually a kind of misunderstanding. Poor use of Ajax will not make your web page more efficient, but will reduce it. Ajax is indeed a good thing, but please don't myth it too much. Also consider the above guidelines when using Ajax.

7. Try to use the page style code generated by the style tool as little as possible, and write the style in the style file yourself. Try to write as generically as possible.

8. Page caching

Caching technology is used in large portal websites. It is a technology that trades space for time.

Caching is mainly to improve the reading speed of data. Because there is a traffic bottleneck between the server and the application client, when reading large-capacity data, using the cache to directly serve the client can reduce the data interaction between the client and the server, thereby greatly improving the access speed of secondary pages. efficiency of the program.

9. Configure the entity tag of the website:

This Etag is not for users, but for browser caching. Etag is a mechanism for the server to tell the browser cache whether the content in the cache has changed. Through Etag, the browser can know whether the content in the current cache is the latest and whether it needs to be downloaded from the server again.

10. Use CDN technology.

When we download things online, we often see "southern servers" and "northern servers" that use CDN technology to improve download speed and stability.

The full name of CDN is Content Delivery Network, which is content distribution network. The basic idea is to avoid bottlenecks and links on the Internet that may affect data transmission speed and stability as much as possible, so that content transmission can be faster and more stable. By placing node servers throughout the network to form a layer of intelligent virtual network based on the existing Internet, the CDN system can real-time analyze the network traffic and the connection and load status of each node, as well as the distance to the user and response time. and other comprehensive information to redirect the user's request to the service node closest to the user. Its purpose is to enable users to obtain the required content nearby, solve the congestion situation of the Internet network, and improve the response speed of users' access to the website (from Baidu Encyclopedia).

11. Use Gzip to compress documents:

GZIP encoding is a method used to improve web page performance. It can not only reduce storage space, but also reduce the time required for transmission. After using Gzip to compress the files in the website, the loading speed of the web page can be significantly increased, and the bandwidth consumption of the website can also be reduced.

12. The style sheet file is placed externally and called:

Put the JavaScript and CSS style sheet files that need to be used in the page in another folder, so that these called files will be cached when opening other pages of the website, thus speeding up the loading speed of the website pages.

13. Reduce weight for JavaScript files:

This method is different from the gzip compression function. There are many ways to reduce the size of JavaScript documents, such as removing unnecessary spaces and other tags. Reducing script files can significantly improve the access speed of web pages.

14. Use the height and width attributes of images

The height and width attributes of each image allow the browser to know the length and width of the image before loading the image, and reserve the specified The length and width will be displayed after the image is loaded. Without these two attributes, the browser needs to process the page layout style again after successfully reading the image, which undoubtedly slows down the loading speed of the web page. Therefore, it is best to use the height and width attributes when the image size is fixed.

15. CSS file compression and slimming

p CSS is the current fluent web page layout method. p defines the elements and CSS controls the display effect. So we often write CSS to one or more externally linked CSS files, and the CSS file code also has many lines. We can use some CSS compression tools to remove unnecessary redundant content in CSS files, such as repeated definitions of styles, spaces, etc. to slim down. You can try using the CleanCSS tool to compress your CSS files.

16. Integrate CSS and JS files to reduce the number of HTTP requests

Nowadays, web pages have multiple images, CSS external file links, and Javascript external script links. So when visiting a web page, the browser needs to request these files from the server multiple times. There will be a considerable time difference between request and loading. Especially for some web pages with multiple small pictures and icon buttons, the browser needs to request as many of these small files as there are pictures. Requesting too many of these small picture files will significantly affect the loading speed of the web page. Therefore, we should try our best to combine small pictures into a large PNG picture, and then display the icon through coordinates. Requesting one large image at once is much faster than requesting small images multiple times. Likewise, it's best to combine CSS and Javascript into a single file as much as possible to help speed up web page loading.

17. Add a slash (/) after the directory address

If a visitor clicks to access such a directory address: http://www.3haojob.com, open the index in this directory .html document. When the server receives the request, it takes some time to analyze whether it is a file or a directory. But if we add a slash (/) at the end, the server will know that you are accessing a directory address, and then just load the default document index.html or index.php directly. In this way, the server does not have to spend time analyzing this address, which also plays a certain role in speeding up the process. Labor dispatch

The above is the entire content of this article. I hope it will be helpful to everyone’s study. For more related content, please pay attention to the PHP Chinese website!

Related recommendations:

How to implement simulated multiple inheritance in php

The above is the detailed content of How to improve web page loading speed in php. For more information, please follow other related articles on the PHP Chinese website!

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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!