日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > php >内容正文

php

php网站适合优化_php开发大型网站如何优化的方案详解

發(fā)布時間:2024/9/27 php 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 php网站适合优化_php开发大型网站如何优化的方案详解 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

1.memcached3KZ香格里拉注冊-香格里拉注冊登錄|網(wǎng)站分類目錄

memcached 是一個高效的分布式的內(nèi)存對象緩存系統(tǒng) ,他可以支持把各種php的數(shù)據(jù)(array,對象,基本數(shù)據(jù)類型)放入到它管理的內(nèi)存中.注:需通過腳本定時清除緩存,防止緩存過大影響網(wǎng)站性能3KZ香格里拉注冊-香格里拉注冊登錄|網(wǎng)站分類目錄

示例代碼:

conn.php

$link=mysql_connect("localhost","root",null);

mysql_select_db("bbs",$link);

mysql_query("set names utf8");

?>

memcache_getid.php

include_once 'conn.php';

$id=$_GET['id'];

$memcache = new memcache;

$memcache->connect('127.0.0.1', 11211) or die ("連接失敗");

//$memcache->flush(); 清除緩存

if($info=$memcache->get($id))

{

echo $info;

exit;

}

else

{

$result=mysql_query("select * from user where id=$id");

if($result)

{

$arr=mysql_fetch_array($result);

echo "need mysql query";

$memcache->add($id,$arr['id'],MEMCACHE_COMPRESSED,60*60*24);

}

}

?>

2.頁面靜態(tài)化技術(shù)3KZ香格里拉注冊-香格里拉注冊登錄|網(wǎng)站分類目錄

a.真靜態(tài)化3KZ香格里拉注冊-香格里拉注冊登錄|網(wǎng)站分類目錄

1.創(chuàng)建模板文件template.html3KZ香格里拉注冊-香格里拉注冊登錄|網(wǎng)站分類目錄

2.通過模板文件,創(chuàng)建靜態(tài)頁面的 php文件 xx.php3KZ香格里拉注冊-香格里拉注冊登錄|網(wǎng)站分類目錄

3. 用戶訪問生成的靜態(tài)頁面 xx.html3KZ香格里拉注冊-香格里拉注冊登錄|網(wǎng)站分類目錄

newsAction.php

header("content-type:text/html;charset=utf-8");

function replace($row,$title,$content){

//含義是 用 $title的內(nèi)容替換 $row中的 %title%

$row=str_replace("%title%",$title,$row);

$row=str_replace("%content%",$content,$row);

return $row;

}

//處理添加、修改、刪除請求

//1.接收一下oper

$oper=$_REQUEST['oper'];

if($oper=="add"){

//接收title,content

$title=$_POST['title'];

$content=$_POST['content'];

//1.把數(shù)據(jù)放入到mysql, 同時創(chuàng)建一個html

//添加到數(shù)據(jù)庫 SqlHelper.class.php

$conn=mysql_connect("localhost","root","root");

if(!$conn){

die("連接失敗");

}

//構(gòu)建html_filename

//$file=

mysql_select_db("spdb1",$conn);

$sql="insert into news (title,content) values('$title','$content')";

if(mysql_query($sql,$conn)){

//獲取剛剛插入數(shù)據(jù)的id號

$id=mysql_insert_id();

$html_filename="news_id".$id.".html";

//echo "文件名=".$html_filename;

//創(chuàng)建html文件

$fp_tmp=fopen("template.tpl","r");

$fp_html_file=fopen($html_filename,"w");

//思路->tmp->html 逐行讀取template.tpl文件,然后逐行替換

while(!feof($fp_tmp)){

//讀取一行.

$row=fgets($fp_tmp);

//替換(小函數(shù))

$new_row=replace($row,$title,$content);

//把替換后的一行寫入到html文件

fwrite($fp_html_file,$new_row);

}

//關(guān)閉文件流

fclose($fp_tmp);

fclose($fp_html_file);

echo "添加到數(shù)據(jù)庫并成功創(chuàng)建html文件返回列表";

}

mysql_close($conn);

}

?>

show_news.php

//接受id

$id=@$_GET['id'];

//看看如何使用html靜態(tài)頁面

//思路,看看html頁面是否有,如果有,直接訪問,沒有就創(chuàng)建

//構(gòu)建一個文件名.

$html_filename="news_id".$id.".html";

echo file_get_contents($html_filename);

//filemtime()=>獲取文件的最后修改時間

//filemtime($html_filename)+30>time() 表示靜態(tài)文件,

//if(file_exists($html_filename)&& filemtime($html_filename)+30>time()){

//

直接訪問html頁面(把html頁面的內(nèi)容 echo 瀏覽器)

//echo file_get_contents($html_filename);

//exit;

//}

//

//$conn=mysql_connect("localhost","root","root");

//

//if(!$conn){

//die("連接失敗");

//}

//

//mysql_select_db("spdb1",$conn);

//

//

//$sql="select * from news where id=$id";

//$res=mysql_query($sql);

開啟ob緩存

//ob_start();

//if($row=mysql_fetch_assoc($res)){

//

//header("content-type:text/html;charset=utf-8");

//echo "

//echo "

新聞詳細(xì)內(nèi)容";

//echo "

{$row['title']}";

//echo "

{$row['content']}";

//echo "

";

//}else{

//echo "沒有結(jié)果";

//}

//

//$html_content=ob_get_contents();

//$my_hader="

";

把ob->$html_filename (必要時,需要考慮路徑)

//file_put_contents($html_filename,$my_hader.$html_content);

//

//mysql_free_result($res);

//mysql_close($conn);

?>

[!--empirenews.page--]

b.偽靜態(tài)化3KZ香格里拉注冊-香格里拉注冊登錄|網(wǎng)站分類目錄

環(huán)境配置:#LoadModule rewrite_module modules/mod_rewrite.so 在httpd.conf去掉改項#,并項目目錄下配置.htaccess文件3KZ香格里拉注冊-香格里拉注冊登錄|網(wǎng)站分類目錄

.htaccess

#寫你的rewrite規(guī)則

RewriteEngine On

#news-id(d+).html$ 是規(guī)則 news.php?id=$1 是轉(zhuǎn)發(fā)的頁面

#正則 子表達(dá)式 捕獲 反向引用

# "news-id33.html"

# 可以配置多個規(guī)則,匹配的順序是從上到下

RewriteRule news-id(d+).html$ news.php?id=$1

RewriteRule news-id(d+).html$ error.php

①真靜態(tài)訪問效率高,利于seo.可以減少對數(shù)據(jù)庫的操作。但是會占用大量的磁盤.3KZ香格里拉注冊-香格里拉注冊登錄|網(wǎng)站分類目錄

②偽靜態(tài)一、可以方便的實現(xiàn)對搜索引擎的優(yōu)化,二、占空間比較小。三、通過生成不同view-id2.hmtl 可以實現(xiàn)內(nèi)容的變化.四有效的防止了注入攻擊3KZ香格里拉注冊-香格里拉注冊登錄|網(wǎng)站分類目錄

注:但是兩者在啟用頁面緩存時(ob_start)需要注意一個問題,不要需要經(jīng)常修改的html文件放入頁面緩存大網(wǎng)站如何優(yōu)化,否則會造成頁面無法刷新得到最新結(jié)果,頁面緩存一般存放經(jīng)常被查詢的html且不會被更新3KZ香格里拉注冊-香格里拉注冊登錄|網(wǎng)站分類目錄

c.mysql優(yōu)化技巧3KZ香格里拉注冊-香格里拉注冊登錄|網(wǎng)站分類目錄

配置慢查詢?nèi)罩?#xff1a;3KZ香格里拉注冊-香格里拉注冊登錄|網(wǎng)站分類目錄

在my.ini最下面配置3KZ香格里拉注冊-香格里拉注冊登錄|網(wǎng)站分類目錄

log-slow-queries = e:/wamp/logs/mysql_slow_query.log

long_query_time=2

通過 show status/variables like '%query%'' 查看是否配置成功(即slow_query_log=ON)3KZ香格里拉注冊-香格里拉注冊登錄|網(wǎng)站分類目錄

分析慢查詢?nèi)罩?KZ香格里拉注冊-香格里拉注冊登錄|網(wǎng)站分類目錄

通過select sleep(4);測試3KZ香格里拉注冊-香格里拉注冊登錄|網(wǎng)站分類目錄

通過explain 慢sql語句或mysqldumpslow 慢查詢?nèi)罩?KZ香格里拉注冊-香格里拉注冊登錄|網(wǎng)站分類目錄

查詢sql語句狀態(tài)3KZ香格里拉注冊-香格里拉注冊登錄|網(wǎng)站分類目錄

set profilling=on;

show profiles;

show profile for query id;

1. 使用order by null 禁用排序(默認(rèn)為filesort)3KZ香格里拉注冊-香格里拉注冊登錄|網(wǎng)站分類目錄

比如 select * from dept group by ename order by null3KZ香格里拉注冊-香格里拉注冊登錄|網(wǎng)站分類目錄

2. 在精度要求高的應(yīng)用中大網(wǎng)站如何優(yōu)化,建議使用定點數(shù)(decimal)來存儲數(shù)值,以保證結(jié)果的準(zhǔn)確性3KZ香格里拉注冊-香格里拉注冊登錄|網(wǎng)站分類目錄

3.表的水平劃分/垂直分割3KZ香格里拉注冊-香格里拉注冊登錄|網(wǎng)站分類目錄

總結(jié)

以上是生活随笔為你收集整理的php网站适合优化_php开发大型网站如何优化的方案详解的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。