总结
豆包 AI 助手文章总结

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

黄舟
发布: 2017-07-27 10:12:47
原创
2992人浏览过

1.memcached

memcached 是一个高效的分布式的内存对象缓存系统 ,他可以支持把各种php的数据(array,对象,基本数据类型)放入到它管理的内存中.注:需通过脚本定时清除缓存,防止缓存过大影响网站性能

示例代码:
conn.php
<?php
	$link=mysql_connect("localhost","root",null);
	mysql_select_db("bbs",$link);
	mysql_query("set names utf8");
?>
memcache_getid.php
<?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.页面静态化技术

a.真静态化

1.创建模板文件template.html
2.通过模板文件,创建静态页面的 php文件  xx.php
3. 用户访问生成的静态页面  xx.html

立即学习PHP免费学习笔记(深入)”;

newsAction.php
<?php


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


    function replace($row,$title,$content){
            //含义是 用 $title的内容替换 $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.把数据放入到mysql, 同时创建一个html

            //添加到数据库 SqlHelper.class.php

            $conn=mysql_connect("localhost","root","root");
        
            if(!$conn){
                die("连接失败");
            }

            //构建html_filename
            //$file=

            mysql_select_db("spdb1",$conn);

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

            if(mysql_query($sql,$conn)){
                
                //获取刚刚插入数据的id号
                $id=mysql_insert_id();
                $html_filename="news_id".$id.".html";
                //echo "文件名=".$html_filename;

                //创建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);
                        //替换(小函数)
                        $new_row=replace($row,$title,$content);

                        //把替换后的一行写入到html文件
                        fwrite($fp_html_file,$new_row);
                }

                //关闭文件流
                fclose($fp_tmp);
                fclose($fp_html_file);

                echo "添加到数据库并成功创建html文件<a href='news_list.php'>返回列表</a>";

            }

            
            mysql_close($conn);

    }


?>
show_news.php
<?php


	//接受id
	$id=@$_GET['id'];

		//看看如何使用html静态页面
		//思路,看看html页面是否有,如果有,直接访问,没有就创建
		//构建一个文件名.
		$html_filename="news_id".$id.".html";

		echo file_get_contents($html_filename);
		//filemtime()=>获取文件的最后修改时间
		//filemtime($html_filename)+30>time() 表示静态文件,
//		if(file_exists($html_filename)&& filemtime($html_filename)+30>time()){
//			
//			//直接访问html页面(把html页面的内容 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 "<table  border='1px' bordercolor='green' cellspacing='0' width=400px height=200px>";
//			echo "<tr><td>新闻详细内容</td></tr>";
//			echo "<tr><td>{$row['title']}</td></tr>";
//			echo "<tr><td>{$row['content']}</td></tr>";
//			echo "</table>";
//		}else{
//			echo "没有结果";
//		}
//
//		$html_content=ob_get_contents();
//		$my_hader="<head><meta http-equiv='content-type' content='text/html;charset=utf-8'/></head>";
//		//把ob->$html_filename (必要时,需要考虑路径)
//		file_put_contents($html_filename,$my_hader.$html_content);
//
//		mysql_free_result($res);
//		mysql_close($conn);


?>
登录后复制


b.伪静态化

环境配置:#LoadModule rewrite_module modules/mod_rewrite.so 在httpd.conf去掉改项#,并项目目录下配置.htaccess文件

.htaccess
<IfModule rewrite_module>
#写你的rewrite规则
RewriteEngine On
#news-id(\d+)\.html$ 是规则  news.php?id=$1 是转发的页面
#正则 子表达式 捕获 反向引用
#  "news-id33.html"
# 可以配置多个规则,匹配的顺序是从上到下
RewriteRule  news-id(\d+)\.html$   news.php?id=$1
RewriteRule  news-id(\d+)\.html$     error.php
</IfModule>
登录后复制


①真静态访问效率高,利于seo.可以减少对数据库的操作。但是会占用大量的磁盘.
②伪静态一、可以方便的实现对搜索引擎的优化,二、占空间比较小。三、通过生成不同view-id2.hmtl 可以实现内容的变化.四有效的防止了注入攻击

注:但是两者在启用页面缓存时(ob_start)需要注意一个问题,不要需要经常修改的html文件放入页面缓存,否则会造成页面无法刷新得到最新结果,页面缓存一般存放经常被查询的html且不会被更新

c.mysql优化技巧

配置慢查询日志:

在my.ini最下面配置

log-slow-queries = e:/wamp/logs/mysql_slow_query.log 
long_query_time=2
登录后复制

通过 show status/variables like '%query%'' 查看是否配置成功(即slow_query_log=ON)

分析慢查询日志

通过select sleep(4);测试

通过explain 慢sql语句或mysqldumpslow 慢查询日志

查询sql语句状态

set profilling=on;
show profiles;
show profile for query id;
登录后复制

1. 使用order by null  禁用排序(默认为filesort)

比如 select * from dept group by ename order by null

2. 在精度要求高的应用中,建议使用定点数(decimal)来存储数值,以保证结果的准确性

3.表的水平划分/垂直分割


以上就是php开发大型网站如何优化的方案详解的详细内容,更多请关注php中文网其它相关文章!

PHP速学教程(入门到精通)
PHP速学教程(入门到精通)

PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
豆包 AI 助手文章总结
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号