


Share the method of caching the PHP website to speed up the opening speed_PHP tutorial
Notes:
1. The compressed files are cached on the server. Accessing them again reduces re-compression time and CPU usage.
2. By setting the client file cache time to reduce the number of re-requests, it can be reduced by more than 85%.
3. Because the image is already in a compressed format, only the client cache time is set and no compression is performed.
Usage:
1. The server must support gzip and Rewrite functions.
2. Add the following code in the line below "RewriteBase /" in the .htacess file, as shown in the picture
RewriteRule (.*.css$|.*.js$|.*.jpg$|.*.gif $|.*.png$) gzip.php?$1 [L]
3. Upload gzip.php to the root directory
4. Create a cache folder in the root directory to ensure it is readable and writable.
/**
* @author Seraphim
* @copyright 2012
*/
//
function sendheader($last_modified , $p_type, $content_length = 0)
{
// Set the client cache validity time
header("Expires: " . gmdate("D, d M Y H:i:s", time() + 15360000) . "GMT");
header("Cache-Control: max-age=315360000");
header("Pragma: ");
//Set the last modification time
header("Last-Modified: " . $last_modified);
//Set file type information
header($p_type);
header("Content-Length: " . $content_length);
}
define('ABSPATH', dirname(__file__) . '/');
$cache = true;
$cachedir = 'cache/'; //The directory where the gz file is stored, ensure it is writable
if (empty($_SERVER['QUERY_STRING']))
exit();
$gzip = strstr($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip');
if (empty ($gzip))
$cache = false;
$key = array_shift(explode('?', $_SERVER['QUERY_STRING']));
$key = str_replace('../' , '', $key);
$filename = ABSPATH . $key;
$symbol = '_';
$rel_path = str_replace(ABSPATH, '', dirname($filename));
$namespace = str_replace('/', $symbol, $rel_path);
$cache_filename = ABSPATH . $cachedir . $namespace . $symbol . basename($filename) .
'.gz'; / /Generate gz file path
$ext = array_pop(explode('.', $filename)); //Judge file type information based on suffix
$type = "Content-type: text/html"; // Default file type
switch ($ext)
{
case 'css':
$type = "Content-type: text/css";
break;
case ' js':
$type = "Content-type: text/javascript";
break;
case 'gif':
$cache = false;
$type = "Content-type : image/gif";
break;
case 'jpg':
$cache = false;
$type = "Content-type: image/jpeg";
break;
case 'png':
$cache = false;
$type = "Content-type: image/png";
break;
default:
exit();
}
if ($cache)
{
if (file_exists($cache_filename))
{ // If gz file exists
$mtime = filemtime($cache_filename);
$ gmt_mtime = gmdate('D, d M Y H:i:s', $mtime) . ' GMT';
if ((isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) && array_shift(explode(';', $_SERVER ['HTTP_IF_MODIFIED_SINCE'])) ==
$gmt_mtime))
{
// Consistent with the file modification date in the browser cache, returns 304
header("HTTP/1.1 304 Not Modified ");
// Send client header
header("Content-Encoding :gzip");
sendheader($gmt_mtime, $type);
}
else
{
// Read gz file output
$content = file_get_contents($cache_filename);
// Send client header
sendheader($gmt_mtime, $type, strlen($content));
header("Content-Encoding: gzip");
// Send data
echo $content;
}
}
else
if (file_exists($filename))
{ // There is no corresponding gz file
$mtime = mktime();
$gmt_mtime = gmdate('D, d M Y H:i:s', $mtime) . ' GMT';
// Read the file
$content = file_get_contents($filename);
// Remove the blank part
// $content = ltrim($content);
// Compress the file content
$content = gzencode($content, 9, $gzip ? FORCE_GZIP : FORCE_DEFLATE);
//Send client header
sendheader($gmt_mtime, $type, strlen($content));
header("Content-Encoding: gzip");
// Send data
echo $content;
// Write file
file_put_contents($cache_filename, $content);
}
else
{
header("HTTP/1.0 404 Not Found");
}
}
else
{ // Process output in Gzip mode. The principle is basically the same as above
if (file_exists($filename))
{
$mtime = filemtime($filename);
$gmt_mtime = gmdate('D, d M Y H:i:s', $ mtime) . ' GMT';
if ((isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) && array_shift(explode(';', $_SERVER['HTTP_IF_MODIFIED_SINCE'])) ==
$gmt_mtime))
{
// Consistent with the file modification date in the browser cache, returns 304
header("HTTP/1.1 304 Not Modified");
// Send client header
sendheader ($gmt_mtime, $type, strlen($content));
header("Content-Encoding :gzip");
}
else
{
// Read file output
$content = file_get_contents($filename);
// Send client header
sendheader($gmt_mtime, $type, strlen($content));
// Send data
echo $ content;
}
}
else
{
header("HTTP/1.0 404 Not Found");
}
}
?>

PHP remains a powerful and widely used tool in modern programming, especially in the field of web development. 1) PHP is easy to use and seamlessly integrated with databases, and is the first choice for many developers. 2) It supports dynamic content generation and object-oriented programming, suitable for quickly creating and maintaining websites. 3) PHP's performance can be improved by caching and optimizing database queries, and its extensive community and rich ecosystem make it still important in today's technology stack.

In PHP, weak references are implemented through the WeakReference class and will not prevent the garbage collector from reclaiming objects. Weak references are suitable for scenarios such as caching systems and event listeners. It should be noted that it cannot guarantee the survival of objects and that garbage collection may be delayed.

The \_\_invoke method allows objects to be called like functions. 1. Define the \_\_invoke method so that the object can be called. 2. When using the $obj(...) syntax, PHP will execute the \_\_invoke method. 3. Suitable for scenarios such as logging and calculator, improving code flexibility and readability.

Fibers was introduced in PHP8.1, improving concurrent processing capabilities. 1) Fibers is a lightweight concurrency model similar to coroutines. 2) They allow developers to manually control the execution flow of tasks and are suitable for handling I/O-intensive tasks. 3) Using Fibers can write more efficient and responsive code.

The PHP community provides rich resources and support to help developers grow. 1) Resources include official documentation, tutorials, blogs and open source projects such as Laravel and Symfony. 2) Support can be obtained through StackOverflow, Reddit and Slack channels. 3) Development trends can be learned by following RFC. 4) Integration into the community can be achieved through active participation, contribution to code and learning sharing.

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 is not dying, but constantly adapting and evolving. 1) PHP has undergone multiple version iterations since 1994 to adapt to new technology trends. 2) It is currently widely used in e-commerce, content management systems and other fields. 3) PHP8 introduces JIT compiler and other functions to improve performance and modernization. 4) Use OPcache and follow PSR-12 standards to optimize performance and code quality.

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.


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

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

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.

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.

Atom editor mac version download
The most popular open source editor

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