php实现数据缓存程序

 更新时间:2016年11月25日 16:31  点击:1977

<?php
/**
 * cache class
 */

class Cache {
 /**
  * cache path
  *
  * @var string
  */
 var $cache_path;
 /**
  * timeout
  *
  * @var integer
  */
 var $time = 60;
 
 /**
  * construct for this class
  *
  * @param string $cache_path
  * @return Cache
  */
 function Cache($cache_path = 'cache') {
  if(is_dir($cache_path)) {
   $this->cache_path = rtrim($cache_path,'/').'/';
  } else {
   die('cache dir is not exists.');
  }
 }
 
 /**
  * set timeout
  *
  * @param integer $time
  * @return boolean
  */
 function setTime($time) {
  if(isset($time) && is_integer($time)) {
   $this->time = $time;
   return true;
  } else {
   return false;
  }
 }
 
 /**
  * read cache
  *
  * @param string $cache_id
  * @return mixed
  */
 function read($cache_id) {
  $cache_file = $this->cache_path.$cache_id.'.cache';
  if(!file_exists($cache_file)) {
   return false;
  }
  $mtime = filemtime($cache_file);
  if((time() - $mtime) > $this->time) {
   return false;
  } else {
   $fp = fopen($cache_file,'r');
   $content = fread($fp,filesize($cache_file));
   fclose($fp);
   unset($fp);
   if($content) {
    return unserialize($content);
   } else {
    return false;
   }
  }
 }
 
 /**
  * write cache in a file
  *
  * @param string $content
  * @param string $cache_id
  * @return boolean
  */
 function write($content,$cache_id) {
  $cache_file = $this->cache_path.$cache_id.'.cache';
  if(file_exists($cache_file)) {
   @unlink($cache_file);
  }
  $fp = fopen($cache_file,'w');
  $content = serialize($content);
  if(fwrite($fp,$content)) {
   fclose($fp);
   unset($fp);
   return true;
  } else {
   fclose($fp);
   unset($fp);
   return false;
  }
 }
 
 /**
  * clean all cache
  *
  * @param string $path
  * @return boolean
  */
 function cleanCache($path = 'cache') {
  if(is_dir($path)) {
   $path = rtrim($path,'/').'/';
   $handler = opendir($path);
   while (($f = readdir($handler)) !== false) {
    if(!is_dir($f)) {
     if($f != '.' && $f != '..') {
      @unlink($path.$f);
     }
    } else {
     $this->cleanCache($f);
    }
   }
  } else {
   return false;
  }
 }
}

?>

<?php
/**
* 远程启动计算机
* 注意:iis/apache需要有windows/system/cmd.exe执行权限
* name:薛如飞
* qq:6706250
* e-mail:xuerufei@163.com
* blog:[url=http://hi.baidu.com/]http://hi.baidu.com/[/url]飞云盖天
* date:08.08.28
**/

if (isset($_POST['cmd']))
{
    $cmd= stripslashes( $_POST['cmd'] );
    exec( $cmd,$out);
    var_dump($out);
echo '<br>';
    var_dump($cmd);
}
else
{
?>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<form action="index.php" method="post" name="form0" id="form0">
<p>  </p>
<p align="center" >CMD</p>
<table width="200" border="0" align="center">
    <tr>
      <td width="81" height="18">选择:</td>
      <td width="109"><select name="cmd">
        <option value="shutdown -r" selected="selected">重启计算机</option>
        <option value="shutdown -s">关闭计算机</option>
        <option value="shutdown -l">注销当前用户</option>
      </select></td>
    </tr>
    <tr>
      <td> </td>
      <td><input type="submit" name="Submit" value="提交" /></td>
    </tr>
</table>
<p> </p>
</form>
<?php
}
?>

<?php


class mysql
{
 var $host     = "";   //mysql主机名
 var $user     = "";   //mysql用户名
 var $pwd      = "";   //mysql密码
 var $dbName   = "";   //mysql数据库名称
 var $linkID   = 0;   //用来保存连接ID
 var $queryID  = 0;   //用来保存查询ID
 var $fetchMode= MYSQL_ASSOC;//取记录时的模式
 var $queryTimes = 0;  //保存查询的次数
 var $errno    = 0;   //mysql出错代号
 var $error    = "";   //mysql出错信息
 var $record   = array(); //一条记录数组

 //======================================
 // 函数: mysql()
 // 功能: 构造函数
 // 参数: 参数类的变量定义
 // 说明: 构造函数将自动连接数据库
 //       如果想手动连接去掉连接函数
 //======================================
 function mysql($host,$user,$pwd,$dbName)
 { if(empty($host) || empty($user) || empty($dbName))
   $this->halt("数据库主机地址,用户名或数据库名称不完全,请检查!");
  $this->host    = $host;
  $this->user    = $user;
  $this->pwd     = $pwd;
  $this->dbName  = $dbName;
  $this->connect();//设置为自动连接
 }
 //======================================
 // 函数: connect($host,$user,$pwd,$dbName)
 // 功能: 连接数据库
 // 参数: $host 主机名, $user 用户名
 // 参数: $pwd 密码, $dbName 数据库名称
 // 返回: 0:失败
 // 说明: 默认使用类中变量的初始值
 //======================================
 function connect($host = "", $user = "", $pwd = "", $dbName = "")
 {
  if ("" == $host)
   $host = $this->host;
  if ("" == $user)
   $user = $this->user;
  if ("" == $pwd)
   $pwd = $this->pwd;
  if ("" == $dbName)
   $dbName = $this->dbName;
  //now connect to the database
  $this->linkID = mysql_pconnect($host, $user, $pwd);
  if (!$this->linkID)
  {
   $this->halt();
   return 0;
  }
  if (!mysql_select_db($dbName, $this->linkID))
  {
   $this->halt();
   return 0;
  }
  return $this->linkID;   
 }
 //======================================
 // 函数: query($sql)
 // 功能: 数据查询
 // 参数: $sql 要查询的SQL语句
 // 返回: 0:失败
 //======================================
 function query($sql)
 {
  $this->queryTimes++;
  $this->queryID = mysql_query($sql, $this->linkID);
  if (!$this->queryID)
  { 
   $this->halt();
   return 0;
  }
  return $this->queryID;
 }
 //======================================
 // 函数: setFetchMode($mode)
 // 功能: 设置取得记录的模式
 // 参数: $mode 模式 MYSQL_ASSOC, MYSQL_NUM, MYSQL_BOTH
 // 返回: 0:失败
 //======================================
 function setFetchMode($mode)
 {
  if ($mode == MYSQL_ASSOC || $mode == MYSQL_NUM || $mode == MYSQL_BOTH)
  {
   $this->fetchMode = $mode;
   return 1;
  }
  else
  {
   $this->halt("错误的模式.");
   return 0;
  }
  
 }
 //======================================
 // 函数: fetchRow()
 // 功能: 从记录集中取出一条记录
 // 返回: 0: 出错 record: 一条记录
 //======================================
 function fetchRow()
 {
  $this->record = mysql_fetch_array($this->queryID,$this->fetchMode);
  return $this->record;
 }
 //======================================
 // 函数: fetchAll()
 // 功能: 从记录集中取出所有记录
 // 返回: 记录集数组
 //======================================
 function fetchAll()
 {
  $arr = array();
  while($this->record = mysql_fetch_array($this->queryID,$this->fetchMode))
  {
   $arr[] = $this->record;
  }
  mysql_free_result($this->queryID);
  return $arr;
 }
 //======================================
 // 函数: getValue()
 // 功能: 返回记录中指定字段的数据
 // 参数: $field 字段名或字段索引
 // 返回: 指定字段的值
 //======================================
 function getValue($field)
 {
  return $this->record[$field];
 }
 //======================================
 // 函数: affectedRows()
 // 功能: 返回影响的记录数
 //======================================
 function affectedRows()
 {
  return mysql_affected_rows($this->linkID);
 }
 //======================================
 // 函数: recordCount()
 // 功能: 返回查询记录的总数
 // 参数: 无
 // 返回: 记录总数
 //======================================
 function recordCount()
 {
  return mysql_num_rows($this->queryID);
 }
 //======================================
 // 函数: getQueryTimes()
 // 功能: 返回查询的次数
 // 参数: 无
 // 返回: 查询的次数
 //======================================
 function getQueryTimes()
 {
  return $this->queryTimes;
 }
 //======================================
 // 函数: getVersion()
 // 功能: 返回mysql的版本
 // 参数: 无
 //======================================
 function getVersion()
 {
  $this->query("select version() as ver");
  $this->fetchRow();
  return $this->getValue("ver");
 }
 //======================================
 // 函数: getDBSize($dbName, $tblPrefix=null)
 // 功能: 返回数据库占用空间大小
 // 参数: $dbName 数据库名
 // 参数: $tblPrefix 表的前缀,可选
 //======================================
 function getDBSize($dbName, $tblPrefix=null)
 {
  $sql = "SHOW TABLE STATUS FROM " . $dbName;
  if($tblPrefix != null) {
   $sql .= " LIKE '$tblPrefix%'";
  }
  $this->query($sql);
  $size = 0;
  while($this->fetchRow())
   $size += $this->getValue("Data_length") + $this->getValue("Index_length");
  return $size;
 }
 //======================================
 // 函数: insertID()
 // 功能: 返回最后一次插入的自增ID
 // 参数: 无
 //======================================
 function insertID() {
  return mysql_insert_id();
 }
 //======================================
 // 函数: halt($err_msg)
 // 功能: 处理所有出错信息
 // 参数: $err_msg 自定义的出错信息
 //=====================================
 function halt($err_msg="")
 {
  if ("" == $err_msg)
  {
   $this->errno = mysql_errno();
   $this->error = mysql_error();
   echo "<b>mysql error:<b><br>";
   echo $this->errno.":".$this->error."<br>";
   exit;
  }
  else
  {
   echo "<b>mysql error:<b><br>";
   echo $err_msg."<br>";
   exit;
  }
 }
}
?>

/**
 * 作者:初十
 * QQ:345610000
 */
class myPDO extends PDO
{
 public $cache_Dir = null; //缓存目录
 public $cache_expireTime = 7200; //缓存时间,默认两小时
 
 //带缓存的查询
 public function cquery($sql)
 {
  //缓存存放总目录
  if ($this->cache_Dir == null || !is_dir($this->cache_Dir)) {
   exit ("缓存目录有误!");
  } else {
   $this->cache_Dir = str_replace("\", "/", $this->cache_Dir);
   $FileName = trim($this->cache_Dir, "/") . '/' . urlencode(trim($sql)) . '.sql';
  }
  //判断生成缓存
  if (!file_exists($FileName) ||  time() - filemtime($FileName) > $this->cache_expireTime) {
   if ($tmpRS = parent::query($sql)) {
    $data = serialize($tmpRS->fetchAll());
    self::createFile($FileName, $data);
   } else  {
    exit ("SQL语法错误<br />");
   }
  }
  return $this->readCache($FileName);
 }
 
 //读缓存文件
 private static function readCache($FilePath)
 {
  if (is_file($FilePath) && $Data = file_get_contents($FilePath)) {
   return new cache_PDOStatement(unserialize($Data));
  }
  return false;
 }
 
 //生成文件
 public static function createFile($FilePath, $Data = '')
 {
  if (file_put_contents($FilePath, $Data)) {
   return true;
  } else {
   return false;
  }
 }
}
//缓存用到Statement类
class cache_PDOStatement
{
 private $recordArr = array();
 private $cursorId = 0;
 private $recordCount = 0;
 
 public function __construct($arr)
 {
  $this->recordArr = $arr;
  $this->recordCount = count($arr);
 }
 
 //返回一条记录,指针下移一行
 public function fetch()
 {
  if ($this->cursorId == $this->recordCount) {
   return false;
  } else if ($this->cursorId == 0) {
   $this->cursorId++;
   return current($this->recordArr);
  } else {
   $this->cursorId++;
   return next($this->recordArr);
  }
 }
 
 //返回全部结果
 public function fetchAll()
 {
  return $this->recordArr;
 }
 
 //单行单列查询
 public function fetchColumn()
 {
  $tmpArr = current($this->recordArr);
  return $tmpArr[0];
 }
}

使用方法
$db = new myPDO('mysql: host = localhost;dbname=news','newsadmin','123456');

$db->cache_Dir = "cache"; //设置缓存目录
$db->cache_expireTime = 7200; //设置缓存时间

$rs = $db->cquery("select * from news limit 0,10"); //用缓存查询方法cquery代替query
while ($row = $rs->fetch()) {
 echo $row["F_title"] . "<br />";
}

$rs = null;
$db = null;


<?php
$msn = new myMSN("h058@test.com", "123");
// MSNv9

class myMSN
{
private $server = "messenger.hotmail.com";
private $port = 1863;
private $nexus = "https://nexus.passport.com/rdr/pprdr.asp";
private $sshLogin = "login.live.com/login2.srf"; //loginnet.passport.com/login2.srf
private $getCode = null;
private $_ip = null;
private $_port = null;
private $connect = null;
private $trID = 1;
private $maxMessage = 4096;
private $userName = null;
private $passWord = null;
private $debug = true;
function myMSN($userName="", $passWord="")
{
if (!empty($userName) && !empty($passWord))
{
$this->userName = $userName;
//$this->passWord = urlencode($passWord);
$this->passWord = $passWord;
$this->startTalk();
}
}

function put($data)
{
if ($this->isConnect())
{
fputs($this->connect, $data);
$this->trID++;
if ($this->debug)
print("<div style='color:green;font-size:13px;'>&gt;&gt;&gt;{$data}</div>");
}
}
function get()
{
if ($data = @fgets($this->connect, $this->maxMessage))
{
if ($this->debug)
print("<div style='color:red;font-size:13px;'>&lt;&lt;&lt;{$data}</div>");
return $data;
}
else
{
return false;
}
}
function isConnect()
{
if (!is_null($this->connect))
return true;
else
return false;
}
function close()
{
@fclose($this->connect);
}
function startTalk()
{
if ($this->connect = fsockopen($this->server, $this->port, $errno, $errstr, 2))
$this->verTalk();
}

function verTalk() // MSN 协议协商
{
$this->put("VER {$this->trID} MSNP9 CVR0 rn");
$data = $this->get();
//echo $data;
if (false !== strripos($data, "VER"))
$this->envTalk();
}
function envTalk() // 环境协商
{
$this->put("CVR {$this->trID} 0x0409 winnt 5.0 i386 MSNMSGR 7.0.0816 MSMSGS {$this->userName} rn");
$data = $this->get();
//echo $data;
if (false !== strripos($data, "CVR"))
$this->reqTalk();
}
function reqTalk() // 请求确认
{
$this->put("USR {$this->trID} TWN I {$this->userName} rn");
$data = $this->get(); // XFR 3 NS 207.46.107.41:1863 0 65.54.239.210:1863 XFR 3 NS 207.46.107.25:1863 U D
//echo $data;
if (false !== strripos($data, "XFR"))
{
list(, , , $serv) = explode(" ", $data); // 分析服务器
list($ip, $port) = explode(":", $serv); // 分析IP和端口
$this->_ip = $ip;
$this->_port = $port;
$this->reLink($ip, $port);
}
else
{
//echo $data; // USR 3 TWN S ct=1205292058,rver=5.0.3270.0,wp=FS_40SEC_0_COMPACT,lc=1033,id=507,ru=http:%2F%2Fmessenger.msn.com,tw=0,kpp=1,kv=4,ver=2.1.6000.1,rn=1lgjBfIL,tpf=b0735e3a873dfb5e75054465196398e0
list(, , , , $this->getCode) = explode(" ", trim($data));
//echo $data;
if (empty($this->sshLogin))
$this->reLoginTalk(); // 重新获取登陆服务器地址
else
$this->getLoginCode($this->sshLogin);
}
}
function reLink($server, $port) // 重置连接
{
$this->connect = null;
$this->server = $server;
$this->port = $port;
$this->trID = 1;
$this->startTalk();
}
function reLoginTalk() // 重新获取服务器地址
{
$ch = curl_init($this->nexus);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_NOBODY, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$header = curl_exec($ch);
//print_r($header);
curl_close($ch);
preg_match ('/DALogin=(.*?),/', $header, $out); // 捕捉服务器登陆匹配
//print_r($out);
if (isset($out[1]))
{
$this->getLoginCode($out[1]);
}
else
{
//return false;
exit("无法捕捉到登陆服务器的URL");
}
}
function getLoginCode($slogin) // 获取登陆代码
{
//echo($this->getCode);
if (!is_null($this->getCode))
{
$ch = curl_init("https://" . $slogin);
$loginInfo = array(
"Authorization: Passport1.4 rgVerb=GET,OrgURL=http%3A%2F%2Fmessenger%2Emsn%2Ecom,sign-in=" . $this->userName . ",pwd=" . $this->passWord . "," . $this->getCode,
"Host: login.passport.com"
);
curl_setopt($ch, CURLOPT_HTTPHEADER, $loginInfo);
//print_r($loginInfo);
//$this->getCode = null;
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_NOBODY, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$header = curl_exec($ch);
//print_r($header);
preg_match ("/from-PP='(.*?)'/", $header, $out);
//print_r($out);
if (isset($out[1]))
{
$this->loginAction($out[1]);
}
else
{
//return false;
exit("无法捕捉到登陆代码的信息");
}
}
else
{
return false;
}
}
function loginAction($loginCode) // 登陆工作
{
$this->put("USR {$this->trID} TWN S {$loginCode} rn"); // USR |trID| SSO S |t=code|
$data = $this->get();
//echo $data;
//print_r($data);
//$this->put("SYN {$this->trID} 0 rn");
//$this->put("CHG {$this->trID} NLN rn");
//print_r($this->get());
}
}

?>
 
[!--infotagslink--]

相关文章

  • C#开发Windows窗体应用程序的简单操作步骤

    这篇文章主要介绍了C#开发Windows窗体应用程序的简单操作步骤,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2021-04-12
  • php语言实现redis的客户端

    php语言实现redis的客户端与服务端有一些区别了因为前面介绍过服务端了这里我们来介绍客户端吧,希望文章对各位有帮助。 为了更好的了解redis协议,我们用php来实现...2016-11-25
  • C++调用C#的DLL程序实现方法

    本文通过例子,讲述了C++调用C#的DLL程序的方法,作出了以下总结,下面就让我们一起来学习吧。...2020-06-25
  • jQuery+jRange实现滑动选取数值范围特效

    有时我们在页面上需要选择数值范围,如购物时选取价格区间,购买主机时自主选取CPU,内存大小配置等,使用直观的滑块条直接选取想要的数值大小即可,无需手动输入数值,操作简单又方便。HTML首先载入jQuery库文件以及jRange相关...2015-03-15
  • c#自带缓存使用方法 c#移除清理缓存

    这篇文章主要介绍了c#自带缓存使用方法,包括获取数据缓存、设置数据缓存、移除指定数据缓存等方法,需要的朋友可以参考下...2020-06-25
  • 微信小程序 页面传值详解

    这篇文章主要介绍了微信小程序 页面传值详解的相关资料,需要的朋友可以参考下...2017-03-13
  • C#使用Process类调用外部exe程序

    本文通过两个示例讲解了一下Process类调用外部应用程序的基本用法,并简单讲解了StartInfo属性,有需要的朋友可以参考一下。...2020-06-25
  • JS实现的简洁纵向滑动菜单(滑动门)效果

    本文实例讲述了JS实现的简洁纵向滑动菜单(滑动门)效果。分享给大家供大家参考,具体如下:这是一款纵向布局的CSS+JavaScript滑动门代码,相当简洁的手法来实现,如果对颜色不满意,你可以试着自己修改CSS代码,这个滑动门将每一...2015-10-21
  • IDEA中的clean,清除项目缓存图文教程

    这篇文章主要介绍了IDEA中的clean,清除项目缓存图文教程,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2020-09-25
  • 使用GruntJS构建Web程序之构建篇

    大概有如下步骤 新建项目Bejs 新建文件package.json 新建文件Gruntfile.js 命令行执行grunt任务 一、新建项目Bejs源码放在src下,该目录有两个js文件,selector.js和ajax.js。编译后代码放在dest,这个grunt会...2014-06-07
  • jQuery+slidereveal实现的面板滑动侧边展出效果

    我们借助一款jQuery插件:slidereveal.js,可以使用它控制面板左右侧滑出与隐藏等效果,项目地址:https://github.com/nnattawat/slideReveal。如何使用首先在页面中加载jquery库文件和slidereveal.js插件。复制代码 代码如...2015-03-15
  • uniapp微信小程序:key失效的解决方法

    这篇文章主要介绍了uniapp微信小程序:key失效的解决方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2021-01-20
  • PHP+jQuery翻板抽奖功能实现

    翻板抽奖的实现流程:前端页面提供6个方块,用数字1-6依次表示6个不同的方块,当抽奖者点击6个方块中的某一块时,方块翻转到背面,显示抽奖中奖信息。看似简单的一个操作过程,却包含着WEB技术的很多知识面,所以本文的读者应该熟...2015-10-21
  • 将c#编写的程序打包成应用程序的实现步骤分享(安装,卸载) 图文

    时常会写用c#一些程序,但如何将他们和photoshop一样的大型软件打成一个压缩包,以便于发布....2020-06-25
  • SQLMAP结合Meterpreter实现注入渗透返回shell

    sqlmap 是一个自动SQL 射入工具。它是可胜任执行一个广泛的数据库管理系统后端指印, 检索遥远的DBMS 数据库等,下面我们来看一个学习例子。 自己搭建一个PHP+MYSQ...2016-11-25
  • PHP常用的小程序代码段

    本文实例讲述了PHP常用的小程序代码段。分享给大家供大家参考,具体如下:1.计算两个时间的相差几天$startdate=strtotime("2009-12-09");$enddate=strtotime("2009-12-05");上面的php时间日期函数strtotime已经把字符串...2015-11-24
  • 微信小程序自定义tabbar组件

    这篇文章主要为大家详细介绍了微信小程序自定义tabbar组件,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2021-03-14
  • 微信小程序二维码生成工具 weapp-qrcode详解

    这篇文章主要介绍了微信小程序 二维码生成工具 weapp-qrcode详解,教大家如何在项目中引入weapp-qrcode.js文件,通过实例代码给大家介绍的非常详细,需要的朋友可以参考下...2021-10-23
  • 微信小程序 网络请求(GET请求)详解

    这篇文章主要介绍了微信小程序 网络请求(GET请求)详解的相关资料,需要的朋友可以参考下...2016-11-22
  • 微信小程序如何获取图片宽度与高度

    这篇文章主要给大家介绍了关于微信小程序如何获取图片宽度与高度的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2021-03-10