溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

如何提高PHP版網(wǎng)站的打開速度

發(fā)布時(shí)間:2020-12-10 14:24:59 來源:億速云 閱讀:174 作者:Leah 欄目:開發(fā)技術(shù)

如何提高PHP版網(wǎng)站的打開速度?相信很多沒有經(jīng)驗(yàn)的人對(duì)此束手無策,為此本文總結(jié)了問題出現(xiàn)的原因和解決方法,通過這篇文章希望你能解決這個(gè)問題。

                                                           說明:
1,在服務(wù)器緩存了壓縮過的文件,再次訪問減少再壓縮時(shí)間,降低CPU占用率。
2,通過設(shè)置客戶端文件緩存時(shí)間,降低再次請(qǐng)求次數(shù),可降低85%以上。
3,圖片因?yàn)橐呀?jīng)是壓縮格式,只是設(shè)置客戶端緩存時(shí)間,不做壓縮處理。

使用方法:
1,服務(wù)器必須支持gzip,Rewrite功能。
2,在.htacess文件的“RewriteBase /”下面一行添加下面的代碼,見圖
RewriteRule (.*.css$|.*.js$|.*.jpg$|.*.gif$|.*.png$) gzip.php?$1 [L]
3,上傳gzip.php到根目錄
4,在根目錄建cache文件夾,保證可讀寫。

如何提高PHP版網(wǎng)站的打開速度

復(fù)制代碼 代碼如下:

<?php
/**
* @author Seraphim
* @copyright 2012
*/
// <!-- 公共的返回header的子程序 -->
function sendheader($last_modified, $p_type, $content_length = 0)
{
// 設(shè)置客戶端緩存有效時(shí)間
header("Expires: " . gmdate("D, d M Y H:i:s", time() + 15360000) . "GMT");
header("Cache-Control: max-age=315360000");
header("Pragma: ");
// 設(shè)置最后修改時(shí)間
header("Last-Modified: " . $last_modified);
// 設(shè)置文件類型信息
header($p_type);
header("Content-Length: " . $content_length);
}
define('ABSPATH', dirname(__file__) . '/');
$cache = true;
$cachedir = 'cache/'; //存放gz文件的目錄,確保可寫
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'; //生成gz文件路徑
$ext = array_pop(explode('.', $filename)); //根據(jù)后綴判斷文件類型信息
$type = "Content-type: text/html"; //默認(rèn)的文件類型
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))
{ // 假如存在gz文件
$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))
{
// 與瀏覽器cache中的文件修改日期一致,返回304
header("HTTP/1.1 304 Not Modified");
// 發(fā)送客戶端header
header("Content-Encoding :gzip");
sendheader($gmt_mtime, $type);
}
else
{
// 讀取gz文件輸出
$content = file_get_contents($cache_filename);
// 發(fā)送客戶端header
sendheader($gmt_mtime, $type, strlen($content));
header("Content-Encoding: gzip");
// 發(fā)送數(shù)據(jù)
echo $content;
}
}
else
if (file_exists($filename))
{ // 沒有對(duì)應(yīng)的gz文件
$mtime = mktime();
$gmt_mtime = gmdate('D, d M Y H:i:s', $mtime) . ' GMT';
// 讀取文件
$content = file_get_contents($filename);
// 去掉空白的部分
// $content = ltrim($content);
// 壓縮文件內(nèi)容
$content = gzencode($content, 9, $gzip ? FORCE_GZIP : FORCE_DEFLATE);
// 發(fā)送客戶端header
sendheader($gmt_mtime, $type, strlen($content));
header("Content-Encoding: gzip");
// 發(fā)送數(shù)據(jù)
echo $content;
// 寫入文件
file_put_contents($cache_filename, $content);
}
else
{
header("HTTP/1.0 404 Not Found");
}
}
else
{ // 處理不使用Gzip模式下的輸出。原理基本同上
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))
{
// 與瀏覽器cache中的文件修改日期一致,返回304
header("HTTP/1.1 304 Not Modified");
// 發(fā)送客戶端header
sendheader($gmt_mtime, $type, strlen($content));
header("Content-Encoding :gzip");
}
else
{
// 讀取文件輸出
$content = file_get_contents($filename);
// 發(fā)送客戶端header
sendheader($gmt_mtime, $type, strlen($content));
// 發(fā)送數(shù)據(jù)
echo $content;
}
}
else
{
header("HTTP/1.0 404 Not Found");
}
}
?>

看完上述內(nèi)容,你們掌握如何提高PHP版網(wǎng)站的打開速度的方法了嗎?如果還想學(xué)到更多技能或想了解更多相關(guān)內(nèi)容,歡迎關(guān)注億速云行業(yè)資訊頻道,感謝各位的閱讀!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

php
AI