SESSION Management Library-PHP source code

Source: Internet
Author: User
Tags glob
SESSION Management Library SESSION management library can be stored in databases, memcached, files, and stored by directory

Source code

 Hoojar studio ** module: wcore/session. php * Brief description: library dedicated to SESSION processing * author: liuqi-> * version: $ Id: session. php 1 2012-11-20 05: 55: 12Z Administrator $ * copyright: PHP Chinese Network **/class wcore_session {/*** SESSION array ** @ var array */public $ data = array (); /*** in which material the SESSION is stored ** @ var string storage method * dbmax: 65535 the SESSION content is stored in the database table * mdbmax: 255 session content is stored in the database memory table * memmax: unlimit session content is stored in the Memcache cache * filemax: unlimit session content is stored in the file * dirmax: Unlimit session content is stored in files in different directories */private $ _ type = 'file '; /*** path of the SESSION file stored when the storage mode is file or dir ** @ var string SESSION file storage path */private $ _ path = '/tmp '; /*** module object for data connection ** @ var wcore_mysql */private $ _ db = null; /*** data table to be operated when the SESSION is stored in the database ** @ var string database table name (common table [session_wcore] and memory table [session_mem]) */private $ _ opt = 'session _ wcore';/*** module object connecting MEM ** @ var wcore_mem */private $ _ mem = null; /*** SESSION life, default The value is 30 minutes in seconds ** @ var integer */private $ _ life_time = 1800; /*** SESSION prefix ** @ var string */private $ _ prefix = 'Ws '; /*** ip address ** @ var integer */private $ _ ip = ''; /*** initialize SESSION ** @ param string $ type the storage mode of the SESSION * @ param integer $ ltime the duration of the SESSION is measured in minutes * @ param string $ path path * @ param string $ prefix SESSION file prefix * @ param boolean $ start whether to enable SESSION processing immediately */public function _ construct ($ type = 'File ', $ Ltime = 30, $ path = '', $ prefix = 'Ws ', $ start = true) {$ this-> _ prefix = $ prefix; $ this-> _ type = strtolower ($ type); if ($ this-> _ type = 'file' | $ this-> _ type = 'dir ') {$ this-> _ path = ($ path & file_exists ($ path ))? $ Path: get_1__var ('session. save_path '); wcore_fso: make_dir ($ this-> _ path ); // process the SESSION storage path} $ this-> _ life_time = ($ ltime & is_numeric ($ ltime ))? $ Ltime * 60: get_cfg_var ('session. gc_maxlifetime '); $ this-> _ ip = wcore_utils: get_ip (); session_set_save_handler (array (& $ this, 'open'), array (& $ this, 'close'), array (& $ this, 'read'), array (& $ this, 'write'), array (& $ this, 'deststroy '), array (& $ this, 'gc '); register_shutdown_function ('session _ write_close');/*** whether to enable session processing immediately */if ($ start) {ini_set ('session. use_cookies ', 'on'); ini_set ('session. use_trans _ Sid ', 'off'); session_set_cookie_params (0,'/'); session_start () ;}$ this-> data = & $ _ SESSION ;} /*** open SESSION ** @ param string $ path SESSION storage path * @ param string $ name SESSION name * @ return boolean */public function open ($ path, $ name) {if ($ this-> _ type = 'DB' | $ this-> _ type = 'mdb ') // process SESSION {$ this-> _ db = wcore_object: mdb (); if ($ this-> _ type = 'mdb ') in the database mode ') {$ this-> _ opt = 'session _ mem ';} else if ($ this-> _ ty Pe = 'mem ') // process SESSION {$ this-> _ mem = wcore_object: mem () in Memcache buffer mode (); $ this-> _ mem-> expire = $ this-> _ life_time/60;} $ this-> gc (0); // delete invalid SESSIONreturn true ;} /*** close SESSION *** @ return boolean */public function close () {return true ;} /*** get the SESSION number ** @ return string */public function get_id () {return session_id ();} /*** read SESSION content ** @ param string $ sid unique SESSION id * @ return string SESSION value */public Function read ($ sid) {/*** process SESSION in database mode */if ($ this-> _ type = 'DB' | $ this-> _ type = 'mdb ') {$ res = $ this-> _ db-> fetch_row ("SELECT sData FROM {$ this-> _ opt} WHERE sId = '{$ sid }';"); return ($ res )? $ Res ['sdata']: '';}/*** process the SESSION in Memcache buffer mode */if ($ this-> _ type = 'mem ') {return $ this-> _ mem-> get ('session ', $ sid );} /*** process the SESSION as a file system */if ($ this-> _ type = 'dir ') {$ sfile = "{$ this-> _ path}/{$ sid [0]}/{$ this-> _ prefix}-{$ sid }";} else {$ sfile = "{$ this-> _ path}/{$ this-> _ prefix}-{$ sid}" ;}if (! File_exists ($ sfile) {return '';} return (string) file_get_contents ($ sfile );} /*** write SESSION content ** @ param string $ sid unique identifier of the SESSION * @ param string $ sdata SESSION content * @ return boolean */public function write ($ sid, $ sdata) {/*** if the SESSION data is null, clear the previous data */if (empty ($ sdata) {$ this-> destroy ($ sid ); return false ;} /*** process SESSION in database mode */if ($ this-> _ type = 'DB' | $ this-> _ type = 'mdb ') {$ expires = time () + $ this-> _ life_time; // SESSION validity period $ SQL = "REPLACE INTO {$ this-> _ opt} (sId, sData, sIp, sExpires) VALUES ('{$ sid }', '{$ sdata}', '{$ this-> _ ip}', {$ expires}) "; $ this-> _ db-> query ($ SQL ); return ($ this-> _ db-> affected_rows ()> 0 )? True: false;}/*** process the SESSION in Memcache buffer mode */if ($ this-> _ type = 'mem ') {$ expires = $ this-> _ life_time/60; // return $ this-> _ mem-> set ('session ', $ sid, $ sdata, $ expires);}/*** process the SESSION as a file system */if ($ this-> _ type = 'dir ') {$ sfile = "{$ this-> _ path}/{$ sid [0]}"; wcore_fso: make_dir ($ sfile ); // process the SESSION storage path $ sfile = "{$ sfile}/{$ this-> _ prefix}-{$ sid }";} else {$ sfile = "{$ this-> _ path}/{$ this-> _ Prefix}-{$ sid} ";} return file_put_contents ($ sfile, $ sdata );} /*** clear SESSION ** @ param string $ sid unique SESSION identifier * @ return boolean clear success return true otherwise false */public function destroy ($ sid = '') {if (empty ($ sid) {$ sid = $ this-> get_id ();} /*** process SESSION in database mode */if ($ this-> _ type = 'DB' | $ this-> _ type = 'mdb ') {$ this-> _ db-> query ("delete from {$ this-> _ opt} WHERE sId = '{$ sid }'"); return ($ this-> _ db-> affected_rows ()> 0 )? True: false;}/*** process the SESSION in Memcache buffer mode */if ($ this-> _ type = 'mem ') {return $ this-> _ mem-> del ('session ', $ sid );} /*** process the SESSION as a file system */if ($ this-> _ type = 'dir ') {$ sfile = "{$ this-> _ path}/{$ sid [0]}/{$ this-> _ prefix}-{$ sid }";} else {$ sfile = "{$ this-> _ path}/{$ this-> _ prefix}-{$ sid}";} return! Empty ($ sfile )? @ Unlink ($ sfile): true;}/*** regularly clears expired sessions ** @ param integer $ max_life_time * @ return boolean */public function gc ($ max_life_time) {if ($ this-> _ type = 'DB' | $ this-> _ type = 'mdb ') // process SESSION {$ this-> _ db-> query ("delete from {$ this-> _ opt} WHERE sExpires <". time ();} else if ($ this-> _ type = 'file') // process SESSION {self :: kill_sfile ($ this-> _ path);} else if ($ this-> _ type = 'dir ')// SESSION {$ dir = 'abcdefghijklmnopqrstuvwxy'; $ len = strlen ($ dir); for ($ I = 0; $ I <$ len; ++ $ I) {self: kill_sfile ("{$ this-> _ path}/{$ dir [$ I]}") ;}} return true ;} /*** delete the session file ** @ param string $ dir the directory where the session file is located * @ param boolean $ no_check whether to determine whether to expire * @ return boolean */private function kill_sfile ($ dir, $ no_check = false) {if ($ no_check) // directly delete the SESSION file without expiration judgment {foreach (glob ("{$ dir}/{$ this-> _ pref Ix}-* ") as $ filename) {@ unlink ($ filename);} return true ;} foreach (glob ("{$ dir}/{$ this-> _ prefix}-*") as $ filename) {if (filemtime ($ filename) + $ this-> _ life_time <time () {@ unlink ($ filename) ;}} return true ;} /*** clear all sessions ** @ return boolean */public function cleanup () {switch ($ this-> _ type) {case 'mem ': return $ this-> _ mem-> flush (); case 'DB': case 'mdb ': return $ this-> _ db-> truncate ($ this-> _ opt); case 'file ': Return self: kill_sfile ($ this-> _ path, true); case 'dir': $ dir = 'abcdefghijklmnopqrstuvwxy'; $ len = strlen ($ dir ); for ($ I = 0; $ I <$ len; ++ $ I) {wcore_fso: rm_dir ($ dir [$ I]);} default: return true ;}}?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.