php网站适合优化_php开发大型网站如何优化的方案详解
1.memcached3KZ香格里拉注冊(cè)-香格里拉注冊(cè)登錄|網(wǎng)站分類目錄
memcached 是一個(gè)高效的分布式的內(nèi)存對(duì)象緩存系統(tǒng) ,他可以支持把各種php的數(shù)據(jù)(array,對(duì)象,基本數(shù)據(jù)類型)放入到它管理的內(nèi)存中.注:需通過(guò)腳本定時(shí)清除緩存,防止緩存過(guò)大影響網(wǎng)站性能3KZ香格里拉注冊(cè)-香格里拉注冊(cè)登錄|網(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.頁(yè)面靜態(tài)化技術(shù)3KZ香格里拉注冊(cè)-香格里拉注冊(cè)登錄|網(wǎng)站分類目錄
a.真靜態(tài)化3KZ香格里拉注冊(cè)-香格里拉注冊(cè)登錄|網(wǎng)站分類目錄
1.創(chuàng)建模板文件template.html3KZ香格里拉注冊(cè)-香格里拉注冊(cè)登錄|網(wǎng)站分類目錄
2.通過(guò)模板文件,創(chuàng)建靜態(tài)頁(yè)面的 php文件 xx.php3KZ香格里拉注冊(cè)-香格里拉注冊(cè)登錄|網(wǎng)站分類目錄
3. 用戶訪問(wèn)生成的靜態(tài)頁(yè)面 xx.html3KZ香格里拉注冊(cè)-香格里拉注冊(cè)登錄|網(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;
}
//處理添加、修改、刪除請(qǐng)求
//1.接收一下oper
$oper=$_REQUEST['oper'];
if($oper=="add"){
//接收title,content
$title=$_POST['title'];
$content=$_POST['content'];
//1.把數(shù)據(jù)放入到mysql, 同時(shí)創(chuàng)建一個(gè)html
//添加到數(shù)據(jù)庫(kù) 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號(hào)
$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ù)庫(kù)并成功創(chuàng)建html文件返回列表";
}
mysql_close($conn);
}
?>
show_news.php
//接受id
$id=@$_GET['id'];
//看看如何使用html靜態(tài)頁(yè)面
//思路,看看html頁(yè)面是否有,如果有,直接訪問(wèn),沒(méi)有就創(chuàng)建
//構(gòu)建一個(gè)文件名.
$html_filename="news_id".$id.".html";
echo file_get_contents($html_filename);
//filemtime()=>獲取文件的最后修改時(shí)間
//filemtime($html_filename)+30>time() 表示靜態(tài)文件,
//if(file_exists($html_filename)&& filemtime($html_filename)+30>time()){
//
直接訪問(wèn)html頁(yè)面(把html頁(yè)面的內(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);
開(kāi)啟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 "沒(méi)有結(jié)果";
//}
//
//$html_content=ob_get_contents();
//$my_hader="
";把ob->$html_filename (必要時(shí),需要考慮路徑)
//file_put_contents($html_filename,$my_hader.$html_content);
//
//mysql_free_result($res);
//mysql_close($conn);
?>
[!--empirenews.page--]
b.偽靜態(tài)化3KZ香格里拉注冊(cè)-香格里拉注冊(cè)登錄|網(wǎng)站分類目錄
環(huán)境配置:#LoadModule rewrite_module modules/mod_rewrite.so 在httpd.conf去掉改項(xiàng)#,并項(xiàng)目目錄下配置.htaccess文件3KZ香格里拉注冊(cè)-香格里拉注冊(cè)登錄|網(wǎng)站分類目錄
.htaccess
#寫你的rewrite規(guī)則
RewriteEngine On
#news-id(d+).html$ 是規(guī)則 news.php?id=$1 是轉(zhuǎn)發(fā)的頁(yè)面
#正則 子表達(dá)式 捕獲 反向引用
# "news-id33.html"
# 可以配置多個(gè)規(guī)則,匹配的順序是從上到下
RewriteRule news-id(d+).html$ news.php?id=$1
RewriteRule news-id(d+).html$ error.php
①真靜態(tài)訪問(wèn)效率高,利于seo.可以減少對(duì)數(shù)據(jù)庫(kù)的操作。但是會(huì)占用大量的磁盤.3KZ香格里拉注冊(cè)-香格里拉注冊(cè)登錄|網(wǎng)站分類目錄
②偽靜態(tài)一、可以方便的實(shí)現(xiàn)對(duì)搜索引擎的優(yōu)化,二、占空間比較小。三、通過(guò)生成不同view-id2.hmtl 可以實(shí)現(xiàn)內(nèi)容的變化.四有效的防止了注入攻擊3KZ香格里拉注冊(cè)-香格里拉注冊(cè)登錄|網(wǎng)站分類目錄
注:但是兩者在啟用頁(yè)面緩存時(shí)(ob_start)需要注意一個(gè)問(wèn)題,不要需要經(jīng)常修改的html文件放入頁(yè)面緩存大網(wǎng)站如何優(yōu)化,否則會(huì)造成頁(yè)面無(wú)法刷新得到最新結(jié)果,頁(yè)面緩存一般存放經(jīng)常被查詢的html且不會(huì)被更新3KZ香格里拉注冊(cè)-香格里拉注冊(cè)登錄|網(wǎng)站分類目錄
c.mysql優(yōu)化技巧3KZ香格里拉注冊(cè)-香格里拉注冊(cè)登錄|網(wǎng)站分類目錄
配置慢查詢?nèi)罩?#xff1a;3KZ香格里拉注冊(cè)-香格里拉注冊(cè)登錄|網(wǎng)站分類目錄
在my.ini最下面配置3KZ香格里拉注冊(cè)-香格里拉注冊(cè)登錄|網(wǎng)站分類目錄
log-slow-queries = e:/wamp/logs/mysql_slow_query.log
long_query_time=2
通過(guò) show status/variables like '%query%'' 查看是否配置成功(即slow_query_log=ON)3KZ香格里拉注冊(cè)-香格里拉注冊(cè)登錄|網(wǎng)站分類目錄
分析慢查詢?nèi)罩?KZ香格里拉注冊(cè)-香格里拉注冊(cè)登錄|網(wǎng)站分類目錄
通過(guò)select sleep(4);測(cè)試3KZ香格里拉注冊(cè)-香格里拉注冊(cè)登錄|網(wǎng)站分類目錄
通過(guò)explain 慢sql語(yǔ)句或mysqldumpslow 慢查詢?nèi)罩?KZ香格里拉注冊(cè)-香格里拉注冊(cè)登錄|網(wǎng)站分類目錄
查詢sql語(yǔ)句狀態(tài)3KZ香格里拉注冊(cè)-香格里拉注冊(cè)登錄|網(wǎng)站分類目錄
set profilling=on;
show profiles;
show profile for query id;
1. 使用order by null 禁用排序(默認(rèn)為filesort)3KZ香格里拉注冊(cè)-香格里拉注冊(cè)登錄|網(wǎng)站分類目錄
比如 select * from dept group by ename order by null3KZ香格里拉注冊(cè)-香格里拉注冊(cè)登錄|網(wǎng)站分類目錄
2. 在精度要求高的應(yīng)用中大網(wǎng)站如何優(yōu)化,建議使用定點(diǎn)數(shù)(decimal)來(lái)存儲(chǔ)數(shù)值,以保證結(jié)果的準(zhǔn)確性3KZ香格里拉注冊(cè)-香格里拉注冊(cè)登錄|網(wǎng)站分類目錄
3.表的水平劃分/垂直分割3KZ香格里拉注冊(cè)-香格里拉注冊(cè)登錄|網(wǎng)站分類目錄
總結(jié)
以上是生活随笔為你收集整理的php网站适合优化_php开发大型网站如何优化的方案详解的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 夏季防暑降温13个方法 防温降暑的方法?
- 下一篇: 江西信丰县有没有黄坂乡?