php之將使用者資訊寫入資料庫

來源:互聯網
上載者:User

標籤:host   http   ack   iss   rac   fonts   插入資料   handle   include   

session進階應用程式將使用者資訊寫入到資料庫中

首先建立資料庫表

在實驗資料庫sqldb中建立session表,用於儲存資料



在根資料夾下建立須要用到的檔案(重點是session,class.php這個類檔案。包括列一些方法)


在session.class.php中主要用到的是session_set_save_handler()這種方法。藉助PDO進行資料操作。用類編寫寫入資料庫表中,

類中定義了一些靜態方法,其屬性也要為靜態,這樣session的資料就直接寫入資料庫中,而不是儲存在本地目錄中

首先建立一個Session類。類中首先定義一些私人靜態屬性。定義了ip。存留時間和時間

<?php//定義session類    class Session{    private static $handler=null;    private static $ip=null;    private static $lifetime=null;    private static $time=null;

private static function init($handler){    self::$handler=$handler; //代表PDO的連結    //ip先推斷不為空白    self::$ip=!empty($_SERVER["REMOTE_ADDR"])? $_SERVER["REMOTE_ADDR"] : ‘unkown‘;    //從設定檔取出存留時間    self::$lifetime=ini_get(‘session.gc_maxlifetime‘);    self::$time=time();    }

接下來就是定義開啟session的方法

//定義開啟session的方法static function start(PDO $pdo){    self::init($pdo);  //初始化私人方法    session_set_save_handler(    array(__CLASS__,"open"),    array(__CLASS__,"close"),    array(__CLASS__,"read"),    array(__CLASS__,"write"),    array(__CLASS__,"destroy"),    array(__CLASS__,"gc")    );    session_start();}


在開啟session中有open, close, read, write, destory, gc 的方法。以下主要是定義出這些方法 

open() 和 close()  方法

public static function open($path, $name){    return true;}public static function close(){    return true;}

在定義這些方法時,最重要的是write() 和 read() 方法,由於這是直接從資料庫讀出或寫入,採用PDO資料庫預先處理方式

read():先進行PDO預先處理。然後在擷取的一條記錄中,要推斷ip是否為資料庫中的ip,取出的資料是否已經到期,都不是就成功讀出

    public static function read($PHPSESSID){    $sql="select PHPSESSID,update_time,client_ip,data from session where PHPSESSID= ?"; //用?參數    //PDO預先處理    $stmt=self::$handler->prepare($sql);    $stmt->execute(array($PHPSESSID));    //擷取一條記錄    if(!$result=$stmt->fetch(PDO::FETCH_ASSOC)){    return ‘‘;    }    //推斷當前訪問ip是否為資料庫存在的ip    if(self::$ip != $result["client_ip"]){    self::destroy($PHPSESSID);  //銷毀使用者    return ‘‘;    }    //推斷是不是到期的    if(($result["update_time"] + self::$lifetime) < self::$time){    self::destroy($PHPSESSID);    return ‘‘;    }    return $result[‘data‘];  //成功讀出    }


write():相同寫入的方法也比較重要,須要推斷傳進來的資料是否為空白,假設為空白就不進行插入

    public static function write($PHPSESSID, $data){    $sql="select PHPSESSID,update_time,client_ip,data from session where PHPSESSID= ?

"; $stmt=self::$handler->prepare($sql); $stmt->execute(array($PHPSESSID)); if($result=$stmt->fetch(PDO::FETCH_ASSOC)){ //延遲30更新 if($result[‘data‘] != $data || self::$time > ($result[‘update_time‘]+30)){ //更新資料語句 $sql="uptate session set update_time=?, data=?

where PHPSESSID=?"; $stm=self::$handler->prepare($sql); $stm->execute(array(self::$time, $data, $PHPSESSID)); } }else{ //推斷傳進來的資料是否為空白。空時不插入 if(!empty($data)){ $sql="insert into session(PHPSESSID,update_time,client_ip,data) values(?

,?,?,?

)"; //插入值用?

參數 $sth=self::$handler->prepare($sql); $sth->execute(array($PHPSESSID,self::$time,self::$ip,$data)); //必須用數組 } } return true; }


接下來就是資料的銷毀

相同  destory()  和  gc()

destory():資料刪除

gc():記憶體回收

    public static function destroy($PHPSESSID){    $sql="delete from session where PHPSESSID=?

"; $stmt=self::$handler->prepare($sql); $stmt->execute(array($PHPSESSID)); return true; } private static function gc($lifetime){ $sql="delete from session where update_time < ?

"; $stmt=self::$handler->prepare($sql); $stmt->execute(array(self::$time-$lifetime)); return true; } }


最後就拋出一個異常並調用session類

    try{    $pdo=new PDO("mysql:host=localhost;dbname=sqldb","root","heyifeng19930924");    }catch(PDOException $e){    echo $e->getMessage();    }    //調用session類    Session::start($pdo);


在測試檔案裡。寫法和session進階使用方法(即上一篇部落格的測試檔案)一樣

僅僅是在包括檔案裡包括這個類檔案

即:include"session.class.php";



測試結果,假設插入資料成功,查詢表格資訊,在資料庫中顯示:


即傳遞列PHPSESSID的值


刪除撤銷後。查詢表格顯示


即撤銷了PHPSESSID的值













php之將使用者資訊寫入資料庫

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.