Home  >  Article  >  php教程  >  phpTaoke精简淘宝客_精简url_自动清理缓存_支持伪静态

phpTaoke精简淘宝客_精简url_自动清理缓存_支持伪静态

PHP中文网
PHP中文网Original
2016-05-25 17:07:011163browse

phpTaoke精简淘宝客_精简url_自动清理缓存_支持伪静态 
现仅支持.htaccess伪静态,也可以根据此伪静态,编写其它伪静态规则 
暂无后台管理,只需要修改common.php 文件即可 
其中 
ALIMAMA_PID alimama api 很重要否则会给别人佣金啊 

WEB_PATH 目录 
以下是几种情况的写法 
非伪静态 temai目录下 
/temai/? 
非伪静态 根目录下 
/? 

伪静态 temai目录 
/temai/ 
伪静态根目录 

1.index.php

<?php
include(&#39;common.php&#39;);
include(&#39;function.php&#39;);
require_once(&#39;cache.inc.php&#39;);
$cache = new Cache(CACHE_PATH,CACHE_DATE); //省略参数即采用缺省设置, $cache = new Cache();
$cache->gz=WEB_GZ;//缓存文件压缩 默认为 0 关闭 1 gzcompress压缩 2 gzip输出 3 gzcompress压缩 和 gzip输出
$cache->auto_clear=AUTO_CLEAR;//自动清理过期缓存echo $weburl;exit;
$cache->load();
//正文
unset($_GET[&#39;spm&#39;]);
$urlname=array_keys($_GET);$urlname=explode("_html",$urlname[0]);
echo cacheurl($urlname[0]);
echo TONGJI;
//正文
$cache->write();
?>

2.common.php

<?php
header("content-Type: text/html; charset=gb2312");
define("DATA_PATH","data");//数据目录
define("CACHE_PATH","cache/");//缓存目录
define("WEB_PATH","/tao/?");//网站目录  更改此项需要更新 以上两个目录的缓存方法,http://域名/del.php
define("ALIMAMA_PID","mm_16329643_3221023_10596513");//alimama pid
define("CACHE_DATE",60*60*6);//缓存时间60*60*60下
define("AUTO_CLEAR",1);//自动清理过期缓存文件
define("WEB_GZ",1);//缓存文件压缩 默认为 0 关闭 1 gzcompress压缩 2 gzip输出 3 gzcompress压缩 和 gzip输出
 
define("MI_SEARCH","淘宝,w.cnzz.com");//手动替换操作 替换前的数组 用半角 , 分隔
define("MI_REPLACE","爱美丽,www.ibtf.net");//手动替换操作 替换后的数组   用半角 , 分隔
 
define("INDEX_URL","http://www.taobao.com/go/chn/tbk_channel/onsale.php?pid=".ALIMAMA_PID."&unid=&refpos=557_102559_0,a,null&clk1=92486b4faea5973bbfece33a3a2ca292&upsid=92486b4faea5973bbfece33a3a2ca292");//特卖频道
//define("INDEX_URL","http://www.tmall.com/?ali_trackid=2:".ALIMAMA_PID.":1364258757_3k5_343937185");//天猫商城频道
define("TONGJI",&#39;<p style="display:none"><script language="javascript" type="text/javascript" src="http://js.users.51.la/15249102.js"></script></p><p align="center"><a href="http://www.ibtf.net">爱美丽女装</a></p>&#39;);
?>

3.cache.inc.php

<?php
class Cache {
   /**
    * $dir : 缓存文件存放目录
    * $lifetime : 缓存文件有效期,单位为秒
    * $cacheid : 缓存文件路径,包含文件名
    * $ext : 缓存文件扩展名(可以不用),这里使用是为了查看文件
    * $gz : 文件压缩 默认为 0 关闭 1 gzcompress 2 gzip 输出 3 gzcompress 和 gzip
   */
   private $dir;
   private $lifetime;
   private $cacheid;
   private $ext;
   /**
    * 析构函数,检查缓存目录是否有效,默认赋值
   */
   function __construct($dir=&#39;cache/&#39;,$lifetime=1800) {
       if ($this->dir_isvalid($dir)) {
           $this->dir = $dir;
           $this->lifetime = $lifetime;
           $this->ext = &#39;.c&#39;;
           $this->gz = 1;
           $this->auto_clear =0; //是否开始自动清理过期缓存
           $this->cacheid = $this->getcacheid();
       }
   }
   /**
    * 检查缓存是否有效
   */
   private function isvalid() {
       if (!file_exists($this->cacheid)) return false;
       if (!(@$mtime = filemtime($this->cacheid))) return false;
       if (mktime() - $mtime > $this->lifetime) return false;
       return true;
   }
   /**
    * 写入缓存
   */
   public function write($content=&#39;&#39;) {     
       if(!$content){
       $content = ob_get_contents();
       ob_end_flush();}
        
       if($this->gz==1 || $this->gz==3){$content =function_exists(&#39;gzcompress&#39;)?gzcompress($content):$content;}//压缩
       if(function_exists(&#39;fopen&#39;)){
        $fp = fopen($this->cacheid, "w+");
 
        if (flock($fp, LOCK_EX)) { // 进行排它型锁定
            fwrite($fp, $content);
            flock($fp, LOCK_UN); // 释放锁定
        } else {
            $this->error(&#39;写入缓存失败!请检查目录权限,或此文件已被锁定!&#39;);
        }
        fclose($fp);
       }else{
           try {
           file_put_contents($this->cacheid,$content);
           }
           catch (Exception $e) {
               $this->error(&#39;写入缓存失败!请检查目录权限!&#39;);
           }
        }
   }
   /**
    * 加载缓存
    * exit() 载入缓存后终止原页面程序的执行,缓存无效则运行原页面程序生成缓存
    * ob_start() 开启浏览器缓存用于在页面结尾处取得页面内容
   */
   public function load() {
       if ($this->isvalid()) {
           echo "<span style=&#39;display:none;&#39;>This is Cache.</span> ";
           $content=file_get_contents($this->cacheid);
           if($this->gz==1 || $this->gz==3){$content=function_exists(gzuncompress)?@gzuncompress($content):$content;}//解压缩
            
           if($this->gz==2 || $this->gz==3){
               if(extension_loaded(&#39;zlib&#39;)){ob_start(&#39;ob_gzhandler&#39;);}else{ob_start();}
           }
           echo $content;
           ob_end_flush();
           if($this->auto_clear)$this->clean(&#39;&#39;,1);//自动清理缓存
           exit();
       }
       else if($this->gz==2 || $this->gz==3){
           if(extension_loaded(&#39;zlib&#39;)){ob_start(&#39;ob_gzhandler&#39;);}else{ob_start();}
       }else {
            ob_start();
       }
        
   }
   /**
    * 清除缓存
   */
   public function clean($path=&#39;&#39;,$time=&#39;0&#39;) {
       try {
           if(!$path)$path=$this->dir;
           //unlink($this->cacheid);
           $file= $this ->getfilepath($path,$time);
           if($file){
            foreach($file as $v){
                $this ->remove_directory($v,$time);
            }}
       }
       catch (Exception $e) {
           $this->error(&#39;清除缓存文件失败!请检查目录权限!&#39;);
       }
   }
   //获取目录下文件
    public function getfilepath($path,$time){
        $allfiles=scandir($path);
        foreach($allfiles as $key => $filename)
        {
            if(in_array($filename,array(&#39;.&#39;,&#39;..&#39;)))continue;
            $result[$filename] =$path. $filename;
            if($time && $this->auto_clear && !is_dir($result[$filename])){
                if (mktime() - filemtime($result[$filename]) <= $this->lifetime)unset($result[$filename]);
            }
        }
        return $result;
    }   
    //删除空目录及文件
    public function remove_directory($dir,$time) {
      foreach(glob($dir) as $fn) { 
            if(!$time)echo " removing $fn<br>\n";
            if (!is_writable($fn))@chmod($fn, 0777);
            if(is_dir($fn)){@rmdir($fn);}else{@unlink($fn);}
       } 
    }
    
   /**
    * 取得缓存文件路径
   */
   private function getcacheid() {
       return $this->dir.md5($this->geturl()).$this->ext;
   }
   /**
    * 检查目录是否存在或是否可创建
    */
   private function dir_isvalid($dir) {
       if (is_dir($dir)) return true;
       try {
           mkdir($dir,0777);
       }
       catch (Exception $e) {
             $this->error(&#39;所设定缓存目录不存在并且创建失败!请检查目录权限!&#39;);
             return false;            
       }
       return true;
   }
   /**
    * 取得当前页面完整url
   */
   private function geturl() {
       $url = &#39;&#39;;
       if (isset($_SERVER[&#39;REQUEST_URI&#39;])) {
           $url = $_SERVER[&#39;REQUEST_URI&#39;];
       }
       else {
           $url = $_SERVER[&#39;Php_SELF&#39;];
           $url .= empty($_SERVER[&#39;QUERY_STRING&#39;])?&#39;&#39;:&#39;?&#39;.$_SERVER[&#39;QUERY_STRING&#39;];
       }
       return $url;
   }
   /**
    * 输出错误信息
   */
   private function error($str) {
       echo &#39;<p style="color:red;">&#39;.$str.&#39;</p>&#39;;
   }
}
?>
<?php
/*
  1.此版本为Php5版本,如需要请自行参考修改(比较容易啦,不要那么懒嘛,呵呵!).
  2.此版本为utf-8编码,如果网站采用其它编码请自行转换,Windows系统用记事本打开另存为,选择相应编码即可(一般ANSI),Linux下请使用相应编辑软件或iconv命令行.
  3.拷贝粘贴的就不用管上面第2条了.
* 关于缓存的一点感想:
* 动态缓存和静态缓存的根本差别在于其是自动的,用户访问页面过程就是生成缓存、浏览缓存、更新缓存的过程,无需人工操作干预.
* 静态缓存指的就是生成静态页面,相关操作一般是在网站后台完成,需人工操作(也就是手动生成).
*/
 
/*
* 使用方法举例
------------------------------------Demo1-------------------------------------------
 
   require_once(&#39;cache.inc.php&#39;);
   $cachedir = &#39;./Cache/&#39;; //设定缓存目录
   $cache = new Cache($cachedir,10); //省略参数即采用缺省设置, $cache = new Cache($cachedir);
   if ($_GET[&#39;cacheact&#39;] != &#39;rewrite&#39;) //此处为一技巧,通过xx.Php?cacheact=rewrite更新缓存,以此类推,还可以设定一些其它操作
       $cache->load(); //装载缓存,缓存有效则不执行以下页面代码
   //页面代码开始
   echo date(&#39;H:i:s jS F&#39;);
   //页面代码结束
   $cache->write(); //首次运行或缓存过期,生成缓存
 
------------------------------------Demo2-------------------------------------------
 
   require_once(&#39;cache.inc.php&#39;);
   $cachedir = &#39;./Cache/&#39;; //设定缓存目录
   $cache = new Cache($cachedir,10); //省略参数即采用缺省设置, $cache = new Cache($cachedir);
   if ($_GET[&#39;cacheact&#39;] != &#39;rewrite&#39;) //此处为一技巧,通过xx.Php?cacheact=rewrite更新缓存,以此类推,还可以设定一些其它操作
       $cache->load(); //装载缓存,缓存有效则不执行以下页面代码
   //页面代码开始
   $content = date(&#39;H:i:s jS F&#39;);
   echo $content;
   //页面代码结束
   $cache->write($content); //首次运行或缓存过期,生成缓存
 
------------------------------------Demo3-------------------------------------------
 
   require_once(&#39;cache.inc.php&#39;);
   define(&#39;CACHEENABLE&#39;,true);
    
   if (CACHEENABLE) {
       $cachedir = &#39;./Cache/&#39;; //设定缓存目录
       $cache = new Cache($cachedir,10); //省略参数即采用缺省设置, $cache = new Cache($cachedir);
       if ($_GET[&#39;cacheact&#39;] != &#39;rewrite&#39;) //此处为一技巧,通过xx.Php?cacheact=rewrite更新缓存,以此类推,还可以设定一些其它操作
           $cache->load(); //装载缓存,缓存有效则不执行以下页面代码    
   }
   //页面代码开始
   $content = date(&#39;H:i:s jS F&#39;);
   echo $content;
   //页面代码结束
   if (CACHEENABLE)
       $cache->write($content); //首次运行或缓存过期,生成缓存
*/
?>
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn