付潭

淡泊高远,方圆之道。 助人扶危,处世之本。 修身养性,长乐之方。 好学勤勉,成就之功。

导航

PHP手写cms 缓存Cache

新闻添加页,news-add.php

if($_POST)
{
    $title = $_POST['title'];
    $admin_userid = $_COOKIE['admin_userid'];
    $content = $_POST['content'] ; 
    $choose_category = $_POST['choose_category'];
    //引入能将中文转化为拼音的函数
    include("pinyin.php");
    $en_title = Pinyin($title,1);                   //第二个参数随意设置即为utf8编码           
    //连接数据库
    include("config.php");
    //将缓存写入news-cache文件夹中,由于在新闻修改页面需要用到这里的路径,所以必须将现在生成的年/月日文件夹保存在数据库中
    $path = 'news-cache/'.date('Y').'/'.date('md').'/';
   
    $sql = "INSERT INTO `news` (`title`,`en_title`,`content`,`addtime`,`userid`,`categoryid`,`path`) VALUES ('$title','$en_title','$content','$addtime','$admin_userid','$choose_category','$path')";
    $result = mysql_query($sql);
    if(!$result)
    {
        die(mysql_error());
    }
    $product_id = mysql_insert_id();
   
        //判断是否有年/月日路径
    $path = '../zhongyi/news-cache/'.date('Y').'/';
    if(!file_exists($path))
    {
        mkdir($path);
    }
    $path .= date('md').'/';
     if(!file_exists($path))
    {
        mkdir($path);
    }
    //路径创建好了之后,以文件插入的id来命名每篇新闻内容写入的缓存文件
 /* 第一种方法:适合生成一些变量较少的缓存页面。缺点是需要生成多个文件。一个变量一个文件
    $handle = fopen($path.$en_title.'-'.$product_id.'_con.html','wb');
    fwrite($handle,'<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />'.chr(13).chr(10).$content);
    fclose($handle);
   
    $handle = fopen($path.$en_title.'-'.$product_id.'_tit.html','wb');
    fwrite($handle,'<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />'.chr(13).chr(10).$title);
    fclose($handle);
 */
 /*第二种方法:在缓存文件中写入变量。模拟php格式写入。
   缺点是变量存贮内容较多时,占的内存大,输出慢,*/
    //$handle = fopen($path.$en_title.'-'.$product_id.'.html','wb');  //.html 或者 .php的后缀都行
    //'$content=' ,不能用双引号,因为这里是引用一串字符串
    //而存贮内容的变量$content在缓存文件中写成赋值格式时是需要要引号的。使用单引号时注意要加转义符号,'\'
    //因为编辑器会自动给内容或者标题中出现的单引号添加转义符号,因此不用担心界定符出错。
  
    //chr() 函数从指定的 ASCII 值返回字符。chr(13).chr(10)代表换行
    /*fwrite($handle,'<?php'.chr(13).chr(10).'$content=\''.$content.'\';'.chr(13).chr(10).'$title=\''.$title.'\';'.chr(13).chr(10).'?>');*/
    //fclose($handle);
    //如果编辑器无法自动转义,需要启用函数判断:
    /*
        为了使自己的程序不管服务器是什么设置都能正常执行。可以在程序开始用get_magic_quotes_runtime检测该设置的状态决定是否要手工处理,或者在开始(或不需要自动转义的时候)用set_magic_quotes_runtime(0)关掉该设置。magic_quotes_gpc设置是否自动为GPC(get,post,cookie)传来的数据中的\'\"\\加上反斜线。可以用get_magic_quotes_gpc()检测系统设置。如果没有打开这项设置(返回0,打开时返回1),可以使用addslashes()函数添加,它的功能就是给数据库查询语句等的需要在某些字符前加上了反斜线。这些字符是单引号(\')、双引号(")、反斜线(\\)与 NUL(NULL 字符)。一般用法如下;
        if(!get_magic_quotes_gpc())
        {
            addslashes($prot);
            $cont = $prot;
        }else
        {
            $cont = $prot;
        }
    addslashes() 函数在指定的预定义字符前添加反斜杠。
    这些预定义字符是:单引号 (') , 双引号 (") ,反斜杠 (\) , NULL
    <?php
    $str = "Who's John Adams?";
    echo $str . " This is not safe in a database query.<br />";
    echo addslashes($str) . " This is safe in a database query.";
    ?>
    输出: 
    Who's John Adams? This is not safe in a database query.
    Who\'s John Adams? This is safe in a database query.
    stripslashes()与addslashes()的作用相反,消除转义字符。
    */
//第三中生成缓存的方法:
/*
    函数:
    file_get_contents() 函数把整个文件读入一个字符串中。
    file_get_contents(path,include_path,context,start,max_length)
    path 必需。规定要读取的文件的网址或路径。 include_path 可选。如果也想在 include_path 中搜寻文件的话,可以将该参数设为 "1"。 context 可选。规定文件句柄的环境。context 是一套可以修改流的行为的选项。若使用 null,则忽略。start 可选。规定在文件中开始读取的位置。该参数是 PHP 5.1 新加的。max_length 可选。规定读取的字节数。该参数是 PHP 5.1 新加的。
    注意:file() 函数把整个文件读入一个数组中。
与 file_get_contents() 类似,不同的是 file() 将文件作为一个数组返回。数组中的每个单元都是文件中相应的一行,包括换行符在内。如果失败,则返回 false。
   
*/
    //由于模板的样式路径与文件所在的地址发生变化,因此,我们要在同样的路径深处复制一个新的模板
    //注意:file_get_contents()中,所填写的地址必须是网址而不是文件的路径,因为直接读文件会将PHP代码显示出来,安全性不高。而读取网址的代码,解释过的PHP代码将不会显示出来。
    //利用常量$_SERVER['HTTP_HOST']来取代域名
    $str = file_get_contents('http://'.$_SERVER['HTTP_HOST'].'/zhongyi/news-cache/0/0/news_content_temple.php?id='.$product_id);
    //将新闻页作为模板,加上新增新闻的id组成路径,抓取文件变成字符串赋值给变量$str
    /*$handle = fopen($path.$en_title.'-'.$product_id.'.html','wb');
    fwrite($handle,$str);
    fclose($handle);*/
    //file_put_contents()函数是fopen()、fwrite()、fclose()的综合
    file_put_contents($path.$en_title.'-'.$product_id.'.html',$str);

新闻修改页,edit_news.php

 

if($_POST)              //如果表单有数据提交
{  
    //接收表单提交的数据
    $id=$_POST['id'];
    $title=$_POST['title'];
    $userid=$_COOKIE['admin_userid'];
    $content=$_POST['content'];
    include("pinyin.php");
    $en_title = Pinyin($title,1);//第二个参数随意设置即为utf8编码
    $addtime=$_POST['time'];  
    $sql = "UPDATE `news` set `title`='{$title}',`userid`='{$userid}',`content`='{$content}',`addtime`='{$addtime}',`en_title`='{$en_title}' WHERE `id`='{$id}' ";
    $result=mysql_query($sql);
    if(!$result)
    {
        die(mysql_error());
    }
    
    //将缓存写入news-cache文件夹中
    //这里的路径为新建文件时,存在数据库中的$path,利用隐藏域写入提交的新闻表单中。
    $path = '../zhongyi/';
    $path .= $_POST['path'];
   
    //写缓存cache
    //$str = file_get_contents('http://'.$_SERVER['HTTP_HOST'].'/zhongyi/news-cache/0/0/news_content_temple.php?id='.$id);
    //将新闻页作为模板,加上新增新闻的id组成路径,抓取文件变成字符串赋值给变量$str
    /*$handle = fopen($path.$en_title.'-'.$id.'.html','wb');
    fwrite($handle,$str);
    fclose($handle);*/
    file_put_contents($path.$en_title.'-'.$id.'.html',file_get_contents('http://'.$_SERVER['HTTP_HOST'].'/zhongyi/news-cache/0/0/news_content_temple.php?id='.$id));
    $now_page = $_GET['now_page'];
    header("location:index.php?id=$now_page");

 

}else                   //如果表单没有数据提交
{
    if(PHP_VERSION >= '5.1')                        //只有版本大于5.1才能使用date_default_timezone_set()函数
    {
        @ date_default_timezone_set('Asia/Shanghai');
        $addtime = date("Y-m-d H:i:s");
    }else
    {
        $addtime = date("Y-m-d H:i:s",time() + 3600 * 8);            //中国,东8区,time()函数显示的是格林威治时间,因此要加上8个小时
    }
    $news_id = $_GET['id'];
    $sql = "SELECT * FROM `news` WHERE `id` = '{$news_id}'";     
    $result = mysql_query($sql);
    if(!$result)
    {
        die(mysql_error());
    }
    $row = mysql_fetch_assoc($result);

//缓存删除
    $deletcachefile = '../zhongyi/'.$row['path'].$row['en_title'].'-'.$row['id'].'.html';
    if(file_exists($deletcachefile))
    {
        $result=unlink ($deletcachefile);    //unlink() 函数删除文件。若成功,则返回 true,失败则返回 false。
        echo $result;
    }
}

3.新闻列表页,news-list.php

 <li><a href=<?php echo 'http://'.$_SERVER['HTTP_HOST'].'/zhongyi/'.$row['path'].$row['en_title'].'-'.$row['id'].'.html' ?> title="<?php echo
$row['title'] ?>" target="_blank"><?php echo
cut_str($row['title'], 80) ?></a><span class="date">(<?php echo
cut_str($row['addtime'], 10, $dot = '') ?>)</span></li>
       <?php endwhile; ?>

posted on 2012-12-19 17:17  付潭  阅读(550)  评论(0编辑  收藏  举报