PHP封裝的數(shù)據(jù)庫保存session功能類
來源:易賢網(wǎng) 閱讀:843 次 日期:2016-08-16 14:14:01
溫馨提示:易賢網(wǎng)小編為您整理了“PHP封裝的數(shù)據(jù)庫保存session功能類”,方便廣大網(wǎng)友查閱!

本文實(shí)例講述了PHP封裝的數(shù)據(jù)庫保存session功能類。分享給大家供大家參考,具體如下:

PHP用數(shù)據(jù)庫保存session類:

<?php

class SafeSessionHandler implements SessionHandlerInterface {

  public $save_path;

  public $session_name;

  public $table;

  public function __construct() {

    $this->table = new Table("safe_session");

  }

  private function session_id_parse($session_id) {

    $time = hexdec(substr($session_id, 0, 8));

    $skey = substr($session_id, 8);

    return array($time, $skey);

  }

  public function close() {

    loginfo("close: ");

    return true;

  }

  public function create_sid() {

    loginfo("create_sid: ");

    $time = time();

    $skey = "";

    $char = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";

    for ($i=0; $i<52; $i++) {

      $skey .= $char{mt_rand(0, 61)};

    }

    $session = array(

      "time" => $time,

      "skey" => $skey,

      "sval" => "",

    );

    $this->table->insert($session);

    return dechex($time) . $skey;

  }

  public function destroy($session_id) {

    loginfo("destroy: %s", $session_id);

    list($time, $skey) = $this->session_id_parse($session_id);

    $this->table->where("time = ?", $time)->where("skey = ?", $skey)->delete();

    return true;

  }

  public function gc($maxlifetime) {

    loginfo("gc: %s", $maxlifetime);

    $this->table->where("time < ?", time() - 86400 * 30)->delete();

    return true;

  }

  public function open($save_path, $session_name) {

    loginfo("open: %s, %s", $save_path, $session_name);

    $this->save_path = $save_path;

    $this->session_name = $session_name;

    return true;

  }

  public function read($session_id) {

    loginfo("read: %s", $session_id);

    list($time, $skey) = $this->session_id_parse($session_id);

    $row = $this->table->where("time = ?", $time)->where("skey = ?", $skey)->select()->fetch();

    if (empty($row)) {

      return "";

    }

    return $row["sval"];

  }

  public function write($session_id, $session_data) {

    loginfo("write: %s, %s", $session_id, $session_data);

    $session = array("sval" => $session_data,);

    list($time, $skey) = $this->session_id_parse($session_id);

    $this->table->where("time = ?", $time)->where("skey = ?", $skey)->update($session);

    return true;

  }

}

希望本文所述對大家PHP程序設(shè)計(jì)有所幫助。

更多信息請查看網(wǎng)絡(luò)編程
易賢網(wǎng)手機(jī)網(wǎng)站地址:PHP封裝的數(shù)據(jù)庫保存session功能類
由于各方面情況的不斷調(diào)整與變化,易賢網(wǎng)提供的所有考試信息和咨詢回復(fù)僅供參考,敬請考生以權(quán)威部門公布的正式信息和咨詢?yōu)闇?zhǔn)!

2025國考·省考課程試聽報(bào)名

  • 報(bào)班類型
  • 姓名
  • 手機(jī)號
  • 驗(yàn)證碼
關(guān)于我們 | 聯(lián)系我們 | 人才招聘 | 網(wǎng)站聲明 | 網(wǎng)站幫助 | 非正式的簡要咨詢 | 簡要咨詢須知 | 新媒體/短視頻平臺 | 手機(jī)站點(diǎn) | 投訴建議
工業(yè)和信息化部備案號:滇ICP備2023014141號-1 云南省教育廳備案號:云教ICP備0901021 滇公網(wǎng)安備53010202001879號 人力資源服務(wù)許可證:(云)人服證字(2023)第0102001523號
云南網(wǎng)警備案專用圖標(biāo)
聯(lián)系電話:0871-65099533/13759567129 獲取招聘考試信息及咨詢關(guān)注公眾號:hfpxwx
咨詢QQ:1093837350(9:00—18:00)版權(quán)所有:易賢網(wǎng)
云南網(wǎng)警報(bào)警專用圖標(biāo)