压缩你的PHP、JS、CSS等文本类型代码
众所周知,减小代码在传输中的大小,就能提高页面的传输速度,打开网页也就快 了,这是基于带宽瓶颈的理论,当然服务器速度受到其他很多因素的影响, 有PHP代码执行速度,数据库访问速度,磁盘读写IO速度等,无论怎样,带宽影响是非常大,而受中国电信抑或是中国网通还有其他的一些电信公司的制约,在 服务器网络带宽并不很充裕的条件下,压缩代码传输将会大大节省带宽!
本文讲述通过使用Gzip压缩方式自动压缩相应的代码,在访问者浏览器端会自动还原成代码以便浏览器执行,至于编写代码时的良好风格节省空间的方法不在本文之列。
GZIP 最早由Jean-loup Gailly和Mark Adler创建,用于UNIX系统的文件压缩。我们在 LINUX中经常会用到后缀为.gz的文件,它们就是GZIP格式的。现今已经成为Internet 上使用非常普遍的一种数据压缩格式,或者说一种文件格式。HTTP协议上的GZIP编码是一种用来改进WEB应用程序性能的技术。大流量的WEB站点常常 使用GZIP压缩技术来让用户感受更快的速度。
首先测试一下你的网站各个页面是否采用了Gzip压缩,这里有个测试工具
目前,有两种主流方法实现Gzip压缩。
一、mod_deflate模块
mod_deflate 模块提供了DEFLATE输出过滤器,允许服务器在将输出内容发送到客户端以前进行压缩(应该也是采用gzip数据格式压缩,deflate是一种压缩算 法)以节约带宽。Apache 1.3.x时代使用mod_gzip模块来实现,Apache2已经内置了mod_deflate模块。
这种方法要求安装了Apache的http服务器软件,并且加载了deflate模块。
使用方法:
在网页发布目录的.htaccess文件中加入:
- ifmodule mod_deflate.c>
- AddOutputFilter DEFLATE html htm xml php js css
- ifmodule>
这样就会使得Apache在发布html,xml,php,js,css这样的文件时会采用Gzip方式传输数据,这个方法简单,一劳永逸,但一般使用的虚拟主机并没有安装deflate模块,也不一定是Apache的,不过我们还有办法:
二、ob_gzhandler函数
主机要求:支持PHP,PHP版本在4.0.4以上,安装有zlib库
对于PHP文件传输中压缩,只需在原来的 PHP 文件首行加上
并且在结尾加上
复制时还原为正规的PHP格式。
对于js文件,则在原来js文件的首行加上
末尾同样加上
然后存为abc.js.PHP这样的文件,并且修改指向这个所有html,PHP等文件。
改
为
对于a.html文件就直接改成a.PHP访问好了,如果要保留原来的URL就 修改.htaccess 伪URL或者建立一个网页跳转吧,而其他的CSS等文本也就依此推算出该怎么改了。压缩效能说明:jQuery库源代码为50k左右,先写代码时优化,去除空格组合函数等使其减小至26k,再使用GZIP压缩至14k!
再附一张其他网站的压缩效能比较图,图中三种方式都是GZIP,只是执行的方法不同而已。

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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

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

Hot Topics

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.

WebdevelopmentreliesonHTML,CSS,andJavaScript:1)HTMLstructurescontent,2)CSSstylesit,and3)JavaScriptaddsinteractivity,formingthebasisofmodernwebexperiences.

The future of PHP will be achieved by adapting to new technology trends and introducing innovative features: 1) Adapting to cloud computing, containerization and microservice architectures, supporting Docker and Kubernetes; 2) introducing JIT compilers and enumeration types to improve performance and data processing efficiency; 3) Continuously optimize performance and promote best practices.

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

PHP remains important in modern web development, especially in content management and e-commerce platforms. 1) PHP has a rich ecosystem and strong framework support, such as Laravel and Symfony. 2) Performance optimization can be achieved through OPcache and Nginx. 3) PHP8.0 introduces JIT compiler to improve performance. 4) Cloud-native applications are deployed through Docker and Kubernetes to improve flexibility and scalability.

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.

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.
