Class CustomSession { Private static $ db_host = "localhost "; Private static $ db_user = "root "; Private static $ db_password = ""; Private static $ database = "session "; Private $ conn; Public static function getInstance (){ Static $ instance = null; If ($ instance = null ){ $ Instance = new CustomSession (); } Return $ instance; } Public function _ construct (){ Session_set_save_handler ( Array ($ this, "open "), Array ($ this, "close "), Array ($ this, "read "), Array ($ this, "write "), Array ($ this, "destroy "), Array ($ this, "gc ") ); } Public function _ destruct (){ Session_write_close (); } Public function open (){ $ This-> conn = mysql_connect (CustomSession: $ db_host, CustomSession: $ db_user, CustomSession: $ db_password ); Mysql_select_db (CustomSession: $ database, $ this-> conn ); } Public function close (){ Mysql_close ($ this-> conn ); } Public function read ($ id ){ $ Escaped_id = mysql_escape_string ($ id ); $ Res = $ this-> query ("select * from 'session 'where 'sessionid' =' $ escaped_id '"); If ($ row = mysql_fetch_assoc ($ res )){ $ This-> query ("update 'session 'set' timetamp '= UTC_TIMESTAMP () where 'sessionid' =' $ escaped_id '"); Return $ row ['data']; } Return ""; } Public function write ($ id, $ data ){ $ Query = "replace into 'session' ('sessionid', 'data', 'IP', 'timestamp') values ('% s',' % s ', '% s', UNIX_TIMESTAMP (UTC_TIMESTAMP ()))"; $ This-> query (sprintf ($ query, mysql_escape_string ($ id), mysql_escape_string ($ data), $ _ SERVER ["REMOTE_ADDR"]); // www.111cn.net } Public function destroy ($ id ){ $ Escaped_id = mysql_escape_string ($ id ); $ Res = $ this-> query ("delete from 'session 'where 'id' =' $ escaped_id '"); Return (mysql_affected_rows ($ res) = 1 ); } Public function gc ($ lifetime ){ $ This-> query ("delete from 'session 'where UNIX_TIMESTAMP (UTC_TIMESTAMP ()-'timestamp'> $ lifetime "); } Public function query ($ query ){ $ Res = mysql_query ($ query, $ this-> conn ); Return $ res; } } ?> |