search
Homephp教程php手册PHP网站基础优化方法小结

PHP网站基础优化方法小结

Jun 13, 2016 pm 12:26 PM
gzipphpcodeoptimizationusejoin inexistBasemethodwebsitepagetop

1、使用GZip

  在每一个PHP页面顶部加入以下代码:

php ob_start("ob_gzhandler");?>

  使用该代码后服务器会压缩所有需要传送到客户端的代码,并在浏览器中进行解压,从而使网站运行速度加快。这一功能还可以节省网站空间的流量。

2、不要滥用Javascript和Ajax

  只在需要的时候才使用Javascript和Ajax,千万不要滥用它们。有些网站使用了太多不必要的Ajax动画,或使用Ajax来加载无用的部分。这样一来Javascript文件就会变得很大,而实际上却有很多其他方案来实现这些功能。

3、图片、头文件和HTTP请求

  这一点是本文最关键的部分。网页所引用的图片、外部文件以及CSS样式文件越多,网页就加载得越慢。花些时间把图片文件和其他外部文件缩小一些吧,使它们能更快地被加载。此外,每一次加载图片和外部文件时都会产生一个HTTP请求,这一定会拖延加载时间。你可以使用下面的方法来压缩网页文件、JS文件以及CSS样式文件:

网页文件:使用GZip,请见第一条
JS文件:http://www.fmarcia.info/jsmin/test.html 这个网站可以有效地减小JS文件的大小
CSS文件:http://www.cssdrive.com/index.php/main/csscompressor/ 这个网站可以减小CSS文件的大小

4、限制MySQL查询次数

  对数据库的每一次请求都会使网页的加载速度减缓一些。网络开发者允许很难控制这一点,但是在一些细节的地方是可以进行优化的。比如,在选择数据库记录时,不要使用以下代码:

SELECT * FROM database

而是使用:

SELECT id, name, date, author, etc, blah, blah FROM database

  这会消耗更少的查询时间并减少服务器的负载。

5、.php扩展名

  有些人认为将JS文件保存为filename.js.php以及将CSS文件保存为stylesheet.css.php会让加载的时间减少些,但我并没有察觉到这其中的差别。当然,如果你网站的速度变得很慢,你可以试试这个方法。当然,你需要在每个PHP文件中用include()去载入这些文件。

6、http://www.websiteoptimization.com/

  到这个网站去检测一下你自己网站,它会给你一些如何进行网站优化的建议。我每次建立新网页时都会用上这个功能。

Statement
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

DVWA

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

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.