search
HomeBackend DevelopmentPHP TutorialDetailed introduction to PHP application speed_PHP tutorial

Detailed introduction to PHP application speed_PHP tutorial

Jul 20, 2016 am 10:57 AM
phpgenerallyintroduceadvantageapplicationcaseusspeed upyesmostKnowdetailedhigh speed

We all know that fast speed is the biggest advantage of PHP. In general, PHP is always fast enough to support dynamic generation of Web content, and many times you can't even find a faster method than it.

However, when you have to face huge traffic, high-load applications, limited bandwidth, and various other factors that cause performance bottlenecks, you may ask yourself if there is anything you can do. Make your website run better. Perhaps as long as you add a very inconspicuous free module, your PHP application performance and Web server response speed will be significantly improved.

This article discusses how to further improve the performance of PHP applications and give users a better browsing experience. This article explains various technologies to improve PHP application performance in three aspects (code optimization, caching, and content compression), and introduces well-known products in various fields.

Code Optimization

First, let’s take a look at code optimization. Note that the code optimization here does not mean making the code more beautiful, because this is probably already known and there is no need to discuss it further; in addition, if you have already considered the speed issue, it is likely that you have already made changes to the PHP source code. Made some optimizations.

However, some tools can automatically help us complete these complicated tasks, such as Zend Optimizer. Zend Optimizer is available for free from Zend Technologies, but you must agree to its licensing agreement and note that it is not distributed under the GPL. Zend Optimizer obtains the intermediate code generated by Zend Engine runtime compilation and optimizes it, so that the intermediate code has faster execution efficiency.

The installation method of Zend Optimizer is very simple. You only need to download the precompiled version provided for the platform you are using, add the following two lines of code to php.ini, and then restart the web server:

<ol class="dp-xml">
<li class="alt"><span><span class="attribute">zend_optimizer.optimization_level</span><span>=</span><span class="attribute-value">15</span><span> </span></span></li>
<li>
<span class="attribute">zend_extension</span><span>=</span><span class="attribute-value">"/path/to/ZendOptimizer.so"</span><span> </span>
</li>
<li class="alt">
<span class="attribute">zend_loader.enable</span><span>=</span><span class="attribute-value">Off</span><span> </span>
</li>
</ol>

The additional third line of code here is optional. Disabling zend_loader seems to make the Zend Optimizer a bit faster, so it's worth adding this line to php.ini. Note: You can disable zend_loader only if you are not using the Zend Encoder Runtime.

Caching

If you want to make your huge PHP application have better performance, using caching is also a good way. There are many caching solutions available, including: Zend Cache, APC, and Afterburner Cache.

All these products are "caching modules". When a request for a .php file first occurs, they save the PHP intermediate code in the web server's memory, and then respond to subsequent requests with the "compiled" version. This method can really improve the performance of the application, because it reduces disk access to a minimum (the code has been read and parsed), and the code runs directly in memory, making the server respond to requests much faster.

Of course, the caching module will also monitor changes in PHP source files and re-cache the page if necessary, thus preventing the user from getting pages that are still generated by outdated PHP code. Because caching modules can significantly reduce the load on the server and improve the response efficiency of PHP applications, they are very suitable for websites with heavy loads.

How to choose these caching products

Zend Cache is a commercial software from Zend Technologies, and Zend Technologies is the one mentioned earlier that provides us with PHP engine and free Zend Optimizer company. Zend Cache is indeed well-deserved! For large PHP pages, you can feel that the speed will increase after the first run, and the server will have more available resources. Unfortunately this product is not free, but in some cases it can still be a good value.

Afterburner Cache is a free caching module from Bware Technologies. This product is currently in Beta version. Afterburner Cache looks similar to Zend Cache, but it doesn't improve performance as much as Zend Cache (yet), and it doesn't work with Zend Optimizer.

APC is the abbreviation of Alternative PHP Cache, which is another free caching module from Community Connect. The product is already stable enough for formal use, and it seems to improve the speed of responding to requests to a great extent.

Content Compression

We have discussed several ways to improve the performance of PHP applications. Let’s take a look at another important factor that makes viewers feel that the website is too slow. : Download speed. If the PHP application is running on an internal intranet and each client connects to the server at 100 MB/s, then download speed should not be an issue. However, if the server also needs to serve slow modem users, it is worth considering content compression.

Most browsers support content compression with gzip according to IETF standards. This means that you can gzip the content and send it to the browser, which will decompress the data before displaying the page, and the entire process is completely transparent to the user. As for server-side content compression, there are many different methods available.

例如,来自Remote Communications的免费Apache模块mod_gzip就具有为支持这类内容编码的浏览器 压缩静态Web内容的能力。对于绝大多数静态Web内容,mod_gzip都非常有效。mod_gzip可以方便地编译到 Apache里面,也可以作为DSO使用。据Remote communications公司说,mod_gzip也能够压缩来自mod_php 、mod_perl等的动态内容。

我试了一次又一次,但看来还是不行。我看了许多关于mod_gzip的论坛和文章,看 来到了mod_gzip的下一个版本(可能是1.3.14.6f)这个问题有望得到解决。在此之前,我们可以在网站的静态 部分使用mod_gzip。

然而有时我们确实需要压缩动态内容,所以必须找找其他办法。有一种办法是使用class.gzip_encode.php ,这是一个可以用来压缩页面内容的PHP类,具体方法是在PHP脚本的开头和末尾调用该类的某些函数。如果要 在网站级实现这个方案,可以从php.ini文件的auto_prepend以及auto_append指令调用这些函数。

这种方法虽 然有效,但它无疑为高负载的网站带来了更多的开销。关于如何使用这个类的详细说明,请参见它的源代码。 它的源代码说明相当完善,作者告诉了你所有你必须知道的事情。

PHP 4.0.4有一个新的输出缓存句柄ob_gzhandler,它与前面的类相似,但用法不同。使用 ob_gzhandler时要在php.ini中加入的内容如下:

<ol class="dp-c"><li class="alt"><span><span>output_handler = ob_gzhandler ; </span></span></li></ol>

这行代码使得PHP激活输出缓存,并压缩它发送出去的所有内容。如果由于某种原因你不想在php.ini中加上这行代码,你还可以通过PHP源文件所在目录的.htaccess文件改变默认的服务器行为(不压缩),语法如下 :

<ol class="dp-c"><li class="alt"><span><span>php_value output_handler ob_gzhandler </span></span></li></ol>

或者是从PHP代码调用,如下所示:

<ol class="dp-c"><li class="alt"><span><span>ob_start(</span><span class="string">"ob_gzhandler"</span><span>); </span></span></li></ol>

采用输出缓存句柄的方法确实非常有效,而且不会给服务器带来什么特殊的负荷。但必须注意的是,Netscape Communicator对压缩图形的支持不佳,因此除非你能够保证所有用户都使用IE浏览器,否则你 应该禁止压缩JPEG和GIF图形。一般地,对于所有其他文件,这种压缩都有效,但建议你针对各种浏览器都分别 进行测试,特别是当你使用了特殊的插件或者数据查看器时这一点尤其重要。

使用前面介绍的各种技术,你能够显著地改善网站的性能表现,但应该注意的是:

PHP可能是、也可能不是性能瓶颈所在。务必仔细地观察每一个和应用性能有关的因素,比如数据库等。

单纯使用本文技术只能在一定限度之内提高Web服务器的性能。因此在归咎于PHP以及它的缓存之前,不妨看看是否应该升级服务器以及是否可以引入负载平衡技术(后者需要较大的投资)。

不要低估内容压缩的作用。虽然你在100 MB/s的LAN连接下看到Web应用响应非常迅速,但使用Modem 连接的用户不会,他们只会抱怨你那100 Kb的HTML页面实在过于庞大。

希望通过本文对于PHP的介绍,能够给你带来帮助。


www.bkjia.comtruehttp://www.bkjia.com/PHPjc/445740.htmlTechArticle我们都知道,速度快是 PHP 最大的优点。一般情况下PHP总是具有足够的速度支持Web内容动态生成,许多时候你甚至无法找出比它更快的方法...
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
How can you protect against Cross-Site Scripting (XSS) attacks related to sessions?How can you protect against Cross-Site Scripting (XSS) attacks related to sessions?Apr 23, 2025 am 12:16 AM

To protect the application from session-related XSS attacks, the following measures are required: 1. Set the HttpOnly and Secure flags to protect the session cookies. 2. Export codes for all user inputs. 3. Implement content security policy (CSP) to limit script sources. Through these policies, session-related XSS attacks can be effectively protected and user data can be ensured.

How can you optimize PHP session performance?How can you optimize PHP session performance?Apr 23, 2025 am 12:13 AM

Methods to optimize PHP session performance include: 1. Delay session start, 2. Use database to store sessions, 3. Compress session data, 4. Manage session life cycle, and 5. Implement session sharing. These strategies can significantly improve the efficiency of applications in high concurrency environments.

What is the session.gc_maxlifetime configuration setting?What is the session.gc_maxlifetime configuration setting?Apr 23, 2025 am 12:10 AM

Thesession.gc_maxlifetimesettinginPHPdeterminesthelifespanofsessiondata,setinseconds.1)It'sconfiguredinphp.iniorviaini_set().2)Abalanceisneededtoavoidperformanceissuesandunexpectedlogouts.3)PHP'sgarbagecollectionisprobabilistic,influencedbygc_probabi

How do you configure the session name in PHP?How do you configure the session name in PHP?Apr 23, 2025 am 12:08 AM

In PHP, you can use the session_name() function to configure the session name. The specific steps are as follows: 1. Use the session_name() function to set the session name, such as session_name("my_session"). 2. After setting the session name, call session_start() to start the session. Configuring session names can avoid session data conflicts between multiple applications and enhance security, but pay attention to the uniqueness, security, length and setting timing of session names.

How often should you regenerate session IDs?How often should you regenerate session IDs?Apr 23, 2025 am 12:03 AM

The session ID should be regenerated regularly at login, before sensitive operations, and every 30 minutes. 1. Regenerate the session ID when logging in to prevent session fixed attacks. 2. Regenerate before sensitive operations to improve safety. 3. Regular regeneration reduces long-term utilization risks, but the user experience needs to be weighed.

How do you set the session cookie parameters in PHP?How do you set the session cookie parameters in PHP?Apr 22, 2025 pm 05:33 PM

Setting session cookie parameters in PHP can be achieved through the session_set_cookie_params() function. 1) Use this function to set parameters, such as expiration time, path, domain name, security flag, etc.; 2) Call session_start() to make the parameters take effect; 3) Dynamically adjust parameters according to needs, such as user login status; 4) Pay attention to setting secure and httponly flags to improve security.

What is the main purpose of using sessions in PHP?What is the main purpose of using sessions in PHP?Apr 22, 2025 pm 05:25 PM

The main purpose of using sessions in PHP is to maintain the status of the user between different pages. 1) The session is started through the session_start() function, creating a unique session ID and storing it in the user cookie. 2) Session data is saved on the server, allowing data to be passed between different requests, such as login status and shopping cart content.

How can you share sessions across subdomains?How can you share sessions across subdomains?Apr 22, 2025 pm 05:21 PM

How to share a session between subdomains? Implemented by setting session cookies for common domain names. 1. Set the domain of the session cookie to .example.com on the server side. 2. Choose the appropriate session storage method, such as memory, database or distributed cache. 3. Pass the session ID through cookies, and the server retrieves and updates the session data based on the ID.

See all articles

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

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),

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version