| 订阅 | 在线投稿
分享
 
 
当前位置: 王朝网络 >> mysql >> you hua shi yong mysql cun chu session

you hua shi yong mysql cun chu session

2008-12-22 08:09:25 编辑來源:互联网 评论
 
 
 
本文为【优化使用mysql存储session】的拼音翻译版
  zhi qian xie guo liang pian wen zhang 《 zi ding yi SESSION( er )—— shu ju ku bao cun 》 he 《 wo wei shen me bu shi yong session》
   dan hou lai fa xian dou you wen ti 。 qian zhe chu li zai shi ji zhong ji hu mei shen me yong chu , er qie session hui shou huan de zi ji ling wai chu li 。 hou zhe pin fan de cao zuo shu ju ku , da lai le hen da de xing neng wen ti 。
   zhe liang tian zai xi kao lv xia , da zhi gei chu yi ge fang an , dan huan mei you ju ti xiang xi de ce shi 。
  1、session chu li he tong ji jie he qi lai 。 tong shi you ke ye dou you ji lu 。
  2、 wan quan shi yong shu ju ku he cookie lai mo ni session de gong neng 。
  3、 yong hu de dui session de cao zuo dou jin liang bao zheng zai yi tiao sql yu ju wan cheng 。 bu yong dao session de shi hou , jue dui bu duo yi tiao cha xun 。
  4、 wei le xiao lv qi jian ,session de hui shou mei you ji cheng jin lai , dan ti gong le jie kou , ke yi tiao yong shi xian 。
  zan shi gei chu dai ma , bu ju ti jie shi 。
  sql
  CREATE TABLE `*****_session` (
  `sid` char(32) NOT NULL,
  `uid` int(10) NOT NULL,
  `username` char(32) NOT NULL,
  `usertype` tinyint(1) NOT NULL,
  `activetime` int(10) NOT NULL,
  `expiry` int(10) NOT NULL,
  `ip` char(15) NOT NULL,
  `url` char(80) NOT NULL,
  `value` char(255) NOT NULL,
  PRIMARY KEY (`sid`)
  ) ENGINE=MEMORY DEFAULT CHARSET=utf8;
  php dai ma
  <?
  class session{
  
   private $_sessionPrex= '';//session de qian zhui
  
   private $_time = '';// dang qian shi jian
  
   private $_model = null;// shu ju ku cao zuo mo xing
  
   private $_expiry = 1200;//session you xiao shi jian
  
   private $_domain = '';//session de zuo yong yu
  
   protected $isNew = 0;// pan ding cao zuo dong zuo 0 geng xin 1 zeng jia
  
   protected $session = array();// dui ying de yi tiao session ji lu
  
   public function __construct($options){
   $this->_setOptions($options);
   if(empty($this->_time))$this->_time = time();
   $this->session['activetime'] = $this->_time;
   }
  
   public function start(){
   $this->_getSid();
   }
  
   public function set($key,$value){
   if(in_array($key,array('uid','username','usertype','url','expiry'))){
   if($key == 'expiry'){
   $this->_setCookie($this->_sessionPrex.'_sid',$this->session['sid'],$value);
   $this->_setCookie($this->_sessionPrex.'_uid',$this->session['uid'],$value);
   }
   $this->session[$key] = $value;
   }else{
   $other = $this->session['value'];
   $other[$key] = $value;
   $this->session['value'] = $other;
   }
   }
  
   public function get($key){
   if(in_array($key,array('uid','username','usertype','url','expiry'))){
   return $this->session[$key];
   }else{
   if(isset($this->session['value'][$key])){
   return $this->session['value'][$key];
   }
   return null;
   }
   }
  
   public function gc($file,$time = 1200){
   $lasttime = file_get_contents($file);
   if($lasttime + $time<$this->_time){
   file_put_contents($file,$this->_time);
   return $this->_model->delete('activetime+expiry<'.$this->_time);
   }
   }
  
   public function destroy(){
   $this->session['uid'] = 0;
   $this->session['username'] = '';
   $this->session['usertype'] = -1;
   $this->session['expiry'] = $this->_expiry;
   $this->session['value'] = array();
   $this->_setCookie($this->_sessionPrex.'_sid',$this->session['sid'],$this->_expiry);
   $this->_setCookie($this->_sessionPrex.'_uid',$this->session['uid'],$this->_expiry);
   }
  
   public function __destruct(){
   $this->_save();
   }
  
   private function _save(){
   $dbSession = $this->session;
   $dbSession['value'] = serialize($dbSession['value']);
   if(strlen($dbSession['value'])>255)$this->_error('session->value is too long!');
   if($this->isNew == 1){
   // zeng jia
   $this->_model->insert($dbSession);
   }else{
   // geng xin
   $sid = $dbSession['sid'];
   $this->_model->update(array_slice($dbSession,1),'sid=\''.$sid.'\'');
   }
   }
  
   private function _getSession($sid){
   $dbSession = $this->_model->detail('sid = \''.$sid.'\'');
   if(!$dbSession)return false;
   $dbSession['value'] = unserialize($dbSession['value']);
   $this->session = array_merge($dbSession,$this->session);
   return true;
   }
  
   private function _getSid(){
   $sid = strip_tags($_COOKIE[$this->_sessionPrex.'_sid']);
   if(strlen($sid)==32){
   if($this->_getSession($sid)){
   return true;
   }
   }else{
   $sid = md5(time().mt_rand(1000,10000));
   $this->_setCookie($this->_sessionPrex.'_sid',$sid);
   }
   $this->_setCookie($this->_sessionPrex.'_uid',0);
   $this->session = array(
   'uid' => 0,
   'username' => '',
   'usertype' => -1,
   'activetime' => $this->_time,
   'ip' => $this->_getip(),
   'url' => strip_tags($_SERVER['REQUEST_URI']),
   'expiry' =>$this->_expiry,
   'value' => array()
   );
   $this->isNew = 1;
   $this->session['sid'] = $sid;
   }
  
   private function _setCookie($name,$value,$expiry=0){
   if(empty($expiry))$expiry = $this->_expiry;
   if(empty($this->_domain)){
   setcookie($name,$value,$this->_time + $expiry,'/');
   }else{
   setcookie($name,$value,$this->_time + $expiry,'/',$this->_domain);
   }
   }
  
   private function _getip(){
   return getip();
   }
  
   private function _setOptions($options){
   foreach ($options as $key=>$value){
   if(in_array($key,array('sessionPrex','time','model','expiry','domain'))){
   $key = '_'.$key;
   $this->$key = $value;
   }
   }
   }
  
   private function _error($msg){
   throw new Phpbean_Exception($msg);
   }
  }
  ?>
  ( zhu yi , gai dai ma bu neng zhi jie shi yong , ben wen zhu yao shi ti gong yi zhong si lu )【原文】【汉音对照
 
 
 
 
 
 
 
 
 
日版宠物情人插曲《Winding Road》歌词

日版宠物情人2017的插曲,很带节奏感,日语的,女生唱的。 最后听见是在第8集的时候女主手割伤了,然后男主用嘴帮她吸了一下,插曲就出来了。 歌手:Def...

兄弟共妻,我成了他们夜里的美食

老钟家的两个儿子很特别,就是跟其他的人不太一样,魔一般的执着。兄弟俩都到了要结婚的年龄了,不管自家老爹怎么磨破嘴皮子,兄弟俩说不娶就不娶,老父母为兄弟两操碎了心...

如何磨出破洞牛仔裤?牛仔裤怎么剪破洞?

把牛仔裤磨出有线的破洞 1、具体工具就是磨脚石,下面垫一个硬物,然后用磨脚石一直磨一直磨,到把那块磨薄了,用手撕开就好了。出来的洞啊很自然的。需要猫须的话调几...

我就是扫描下图得到了敬业福和爱国福

先来看下敬业福和爱国福 今年春节,支付宝再次推出了“五福红包”活动,表示要“把欠大家的敬业福都还给大家”。 今天该活动正式启动,和去年一样,需要收集“五福”...

冰箱异味产生的原因和臭味去除的方法

有时候我们打开冰箱就会闻到一股异味,冰箱里的这种异味是因为一些物质发出的气味的混合体,闻起来让人恶心。 产生这些异味的主要原因有以下几点。 1、很多人有这种习...

 
 
zhi qian xie guo liang pian wen zhang 《 zi ding yi SESSION( er )—— shu ju ku bao cun 》 he 《 wo wei shen me bu shi yong session》 dan hou lai fa xian dou you wen ti 。 qian zhe chu li zai shi ji zhong ji hu mei shen me yong chu , er qie session hui shou huan de zi ji ling wai chu li 。 hou zhe pin fan de cao zuo shu ju ku , da lai le hen da de xing neng wen ti 。 zhe liang tian zai xi kao lv xia , da zhi gei chu yi ge fang an , dan huan mei you ju ti xiang xi de ce shi 。 1、session chu li he tong ji jie he qi lai 。 tong shi you ke ye dou you ji lu 。 2、 wan quan shi yong shu ju ku he cookie lai mo ni session de gong neng 。 3、 yong hu de dui session de cao zuo dou jin liang bao zheng zai yi tiao sql yu ju wan cheng 。 bu yong dao session de shi hou , jue dui bu duo yi tiao cha xun 。 4、 wei le xiao lv qi jian ,session de hui shou mei you ji cheng jin lai , dan ti gong le jie kou , ke yi tiao yong shi xian 。 zan shi gei chu dai ma , bu ju ti jie shi 。 sql CREATE TABLE `*****_session` ( `sid` char(32) NOT NULL, `uid` int(10) NOT NULL, `username` char(32) NOT NULL, `usertype` tinyint(1) NOT NULL, `activetime` int(10) NOT NULL, `expiry` int(10) NOT NULL, `ip` char(15) NOT NULL, `url` char(80) NOT NULL, `value` char(255) NOT NULL, PRIMARY KEY (`sid`) ) ENGINE=MEMORY DEFAULT CHARSET=utf8; php dai ma <? class session{ private $_sessionPrex= '';//session de qian zhui private $_time = '';// dang qian shi jian private $_model = null;// shu ju ku cao zuo mo xing private $_expiry = 1200;//session you xiao shi jian private $_domain = '';//session de zuo yong yu protected $isNew = 0;// pan ding cao zuo dong zuo 0 geng xin 1 zeng jia protected $session = array();// dui ying de yi tiao session ji lu public function __construct($options){ $this->_setOptions($options); if(empty($this->_time))$this->_time = time(); $this->session['activetime'] = $this->_time; } public function start(){ $this->_getSid(); } public function set($key,$value){ if(in_array($key,array('uid','username','usertype','url','expiry'))){ if($key == 'expiry'){ $this->_setCookie($this->_sessionPrex.'_sid',$this->session['sid'],$value); $this->_setCookie($this->_sessionPrex.'_uid',$this->session['uid'],$value); } $this->session[$key] = $value; }else{ $other = $this->session['value']; $other[$key] = $value; $this->session['value'] = $other; } } public function get($key){ if(in_array($key,array('uid','username','usertype','url','expiry'))){ return $this->session[$key]; }else{ if(isset($this->session['value'][$key])){ return $this->session['value'][$key]; } return null; } } public function gc($file,$time = 1200){ $lasttime = file_get_contents($file); if($lasttime + $time<$this->_time){ file_put_contents($file,$this->_time); return $this->_model->delete('activetime+expiry<'.$this->_time); } } public function destroy(){ $this->session['uid'] = 0; $this->session['username'] = ''; $this->session['usertype'] = -1; $this->session['expiry'] = $this->_expiry; $this->session['value'] = array(); $this->_setCookie($this->_sessionPrex.'_sid',$this->session['sid'],$this->_expiry); $this->_setCookie($this->_sessionPrex.'_uid',$this->session['uid'],$this->_expiry); } public function __destruct(){ $this->_save(); } private function _save(){ $dbSession = $this->session; $dbSession['value'] = serialize($dbSession['value']); if(strlen($dbSession['value'])>255)$this->_error('session->value is too long!'); if($this->isNew == 1){ // zeng jia $this->_model->insert($dbSession); }else{ // geng xin $sid = $dbSession['sid']; $this->_model->update(array_slice($dbSession,1),'sid=\''.$sid.'\''); } } private function _getSession($sid){ $dbSession = $this->_model->detail('sid = \''.$sid.'\''); if(!$dbSession)return false; $dbSession['value'] = unserialize($dbSession['value']); $this->session = array_merge($dbSession,$this->session); return true; } private function _getSid(){ $sid = strip_tags($_COOKIE[$this->_sessionPrex.'_sid']); if(strlen($sid)==32){ if($this->_getSession($sid)){ return true; } }else{ $sid = md5(time().mt_rand(1000,10000)); $this->_setCookie($this->_sessionPrex.'_sid',$sid); } $this->_setCookie($this->_sessionPrex.'_uid',0); $this->session = array( 'uid' => 0, 'username' => '', 'usertype' => -1, 'activetime' => $this->_time, 'ip' => $this->_getip(), 'url' => strip_tags($_SERVER['REQUEST_URI']), 'expiry' =>$this->_expiry, 'value' => array() ); $this->isNew = 1; $this->session['sid'] = $sid; } private function _setCookie($name,$value,$expiry=0){ if(empty($expiry))$expiry = $this->_expiry; if(empty($this->_domain)){ setcookie($name,$value,$this->_time + $expiry,'/'); }else{ setcookie($name,$value,$this->_time + $expiry,'/',$this->_domain); } } private function _getip(){ return getip(); } private function _setOptions($options){ foreach ($options as $key=>$value){ if(in_array($key,array('sessionPrex','time','model','expiry','domain'))){ $key = '_'.$key; $this->$key = $value; } } } private function _error($msg){ throw new Phpbean_Exception($msg); } } ?> ( zhu yi , gai dai ma bu neng zhi jie shi yong , ben wen zhu yao shi ti gong yi zhong si lu )
󰈣󰈤
  免责声明:本文仅代表作者个人观点,与王朝网络无关。王朝网络登载此文出于传递更多信息之目的,并不意味着赞同其观点或证实其描述,其原创性以及文中陈述文字和内容未经本站证实,对本文以及其中全部或者部分内容、文字的真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
 
时尚秀气模特裴裴(8)
时尚秀气模特裴裴(7)
时尚秀气模特裴裴(6)
时尚秀气模特裴裴(5)
白墙黑瓦
广州东郊[原创]
八美至丹巴的东谷天然盆景
不一样的
 
>>返回首页<<
 
 
 为你推荐
 
 
 
 转载本文
 UBB代码 HTML代码
复制到剪贴板...
 
 热帖排行
 
 
 
 
 
©2005- 王朝网络 版权所有