欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

PHP 使用MySQL管理Session的回调函数详解

程序员文章站 2023-10-17 17:45:11
复制代码 代码如下:
复制代码 代码如下:

<?php
 class mysession extends dbsql {

  /**
   * __construct()
   */
  public function __construct() {
   parent::__construct ();

  }

  /**
   * open()
   *
   * @param <string> $ssavepath
   * @param <string>$ssessionnames
   *
   * @return <boolean> true
   */
  public function open($ssavepath, $ssessionname) {
   return true;
  }

  /**
   * close()
   */
  public function close() {
   return true;
  }

  /**
   * read()
   *
   * @param <string> $ssessionid
   * @return <string> session data or null on error...
   */
  public function read($ssessionid) {
   $ssessionid = addslashes(trim($ssessionid));
   $ssql = "select * from tb_session where f_sessionid = '{$ssessionid}'";
   //echo $ssql;
   if(false != ($result = $this->select($ssql))) {
    //var_dump($result[0]['f_sessiondata']);
    return $result[0]['f_sessiondata'];
   } else {
    return '';
   }
  }

  /**
   * write()
   *
   * @param <string> $sessionid
   * @param <string> $ssessiondata
   *
   * @return <boolean> true or false
   */
  public function write($ssessionid, $ssessiondata) {
   $arrsession = array();
   $arrsession['f_sessionid'] = addslashes(trim($ssessionid));
   $arrsession['f_sessiondata'] = addslashes($ssessiondata);
   $arrsession['f_sessioncreated'] = date("y-m-d h:i:s");
   $arrsession['f_sessionupdated'] = date("y-m-d h:i:s", time() + 1440);

   // 新session及更新的session
   if (false != ($result = $this->insertdata('tb_session', $arrsession))) {
    return true;
   } else {
    $arrupdatesession = array();
    $arrupdatesession['f_sessiondata'] = addslashes($ssessiondata);
    $arrupdatesession['f_sessionupdated'] = date("y-m-d h:i:s", time() + 1440);
    if (false != ($result = $this->updatedata('tb_session', "'".addslashes($ssessionid)."'", 'f_sessionid', $arrupdatesession))) {
     return true;
    } else {
     return false;
    }
   }
  }

  /**
   * destroy()
   *
   * @param <string> $ssessionid
   * @return <boolean> true
   */
  public function destroy($ssessionid) {
   $ssessionid = addslashes(trim($ssessionid));
   $this->deldata($ssessionid, 'f_sessionid', 'tb_session');
   return true;
  }

  /**
   * gc
   *
   * @param <int> $nmaxlifetime seconds
   *
   * @return <boolean> true
   */
  public function gc($nmaxlifetime) {
   $dtexpiredtime = date("y-m-d h:i:s", time() - $nmaxlifetime);

   $ssql = "delete from tb_session where f_sessionupdated < {$dtexpiredtime}";
   //echo $ssql;
   $this->delete($ssql);

   return true;
  }
  /**
   * __desctruct()
   *
   */
  function __destruct() {
   // ensure session data is written out before classes are destroyed
      // (see http://bugs.php.net/bug.php?id=33772 for details)
         @session_write_close();
     } // __destruct
 }
?>