- /**
- * Session MySQL Memory table
- @Usage: Use some other storage method (MySQL or memcache) instead of PHP Sessoin
- @author: Lein
- @Version: 1.2
- */
- Session_Start ();
- if (!isset ($_session[' test ')) {
- $_session[' test ']= "123_lein_". Date ("y-m-d h:i:s");
- }
- Class session{
- Session data
- Private $data;
- Engine,mysql or Memcache
- Private $engine;
- PHP Session expire Time
- Private $sessionexpiredTime;
- Current user ' s session cookie value
- Private $sessionID;
- Session Coolie Name
- Private $sessionCookieName;
- Public Function session ($engineBase =null, $engineName = ' mysql ', $storage _name= ' php_session ') {
- try{
- $this->sessionexpiredtime = intval (Ini_get ("Session.cache_expire") *10;//default is 180 minutes, too long, changed to 30 minutes
- }catch (Exception $Exception) {
- $this->sessionexpiredtime = 1200;
- }
- try{
- $this->sessioncookiename = Ini_get ("Session.name");
- }catch (Exception $Exception) {
- $this->sessioncookiename = ' PHPSESSID ';
- }
- if (!isset ($_cookie[$this->sessioncookiename])) {
- @session_start ();
- $this->sessionid=session_id ();
- }else{
- $this->sessionid=$_cookie[$this->sessioncookiename];
- }
- $className = $engineName. " Sessionengine ";
- $this->engine = new $className (
- Array
- ' Storage_name ' = $storage _name,//mysql table name or MEMCAHCE key which stores data;
- ' Expire_time ' = $this->sessionexpiredtime,
- ' Data_too_long_instead_value ' = ' {__data is *$* to long__} '
- ),
- $this->sessionid,
- & $engineBase
- );
- $this->init ();
- $this->loadfromsession ();
- $this->engine->refresh ();
- $this->engine->cleanup ();
- }
- Private Function init ()
- {
- $this->data = $this->engine->get ();
- if (Empty ($this->data)) {
- @session_start ();
- if (!empty ($_session)) {
- $this->data = $_session;
- $this->engine->create (False, $this->data);
- }
- Else
- {
- $this->engine->create (False, "");
- }
- }
- }
- Public Function loadfromsession ($flagStartSession = False) {
- $flag =false;
- if ($flagStartSession) {
- @session_start ();
- }
- if ($_session&&is_array ($_session)) {
- foreach ($_session as $k = = $v) {
- if (!isset ($this->data[$k])) {
- $this->data[$k] = $v;
- $flag =true;
- }
- }
- }
- if ($flag) {
- $this->engine->set (False, $this->data);
- }
- }
- Private Function __get ($NM)
- {
- if (Isset ($this->data[$nm])) {
- $r = $this->data[$nm];
- return $r;
- }
- Else
- {
- return NULL;
- }
- }
- Private Function __set ($NM, $val)
- {
- $this->data[$nm] = $val;
- $this->engine->set (False, $this->data);
- }
- Private Function __isset ($NM)
- {
- return Isset ($this->data[$nm]);
- }
- Private Function __unset ($NM)
- {
- unset ($this->data[$nm]);
- $this->engine->set (False, $this->data);
- }
- function __destruct () {
- $this->data = NULL;
- $this->engine->close ();
- $this->engine = NULL;
- }
- }
- Interface Sessionengine
- {
- /*
- * Set Varibles
- * @param $arr Array,array (varible name=>varible value,...)
- */
- Public Function setvariable ($arr);
- /*
- * Get Session value
- * @param $key string
- */
- Public function Get ($key = "");
- /*
- * Set Session value
- * @param $key string
- * @param $value string
- */
- Public function set ($key = "", $value = "");
- /*
- * Set Session value
- * @param $key string
- * @param $value string
- */
- Public Function Create ($key = "", $value = "");
- /*
- * Update the session ' s invalid time
- * @param $key string
- */
- Public Function Refresh ($key = "");
- /*
- * Close MySQL or memcache connection
- */
- Public function close ();
- /*
- * Delete Expired Sessions
- */
- Public function Cleanup ();
- }
- Final class Mysqlsessionengine implements sessionengine{
- Private $id = "";
- Private $storage _name= ' php_session ';
- Private $storage _name_slow= ' Php_session_slow ';
- Private $data _too_long_instead_value = ' {__data is ~ to long__} ',//if data is longer than $max _session_data_length and you is using MySQL 4 or below,insert this value into Memery table instead.
- Private $expire _time=1200;
- Private $max _session_data_length = 2048;
- Private $conn;
- Private $mysql _version;
- Public Function Mysqlsessionengine ($arr =array (), $key = "", &$_conn) {
- $this->setvariable ($arr);
- $this->id = $key;
- if (Empty ($this->id) | | strlen ($this->id)!=32) {
- throw new Exception (__file__. " ". __line__.": Session ' s cookie name can ' t be empty and it must has just charactors! ");
- }
- $this->conn = $_conn;
- if (! $this->conn| |! Is_resource ($this->conn)) {
- throw new Exception (__file__. " ". __line__.": Need a MySQL connection! ");
- }
- $this->mysql_version = $this->getone ("Select Floor (Version ())");
- if ($this->mysql_version<5) {
- $this->max_session_data_length = 255;
- }
- }
- Public Function SetVariable ($arr) {
- if (!empty ($arr) &&is_array ($arr)) {
- foreach ($arr as $k = = $v) {
- $this $k = $v;
- if ($k = = ' Storage_name ') {
- $this->storage_name_slow = $v. ' _slow ';
- }
- }
- }
- }
- Public function Get ($key = "") {
- if ($key = = "") $key = $this->id;
- $return = $this->getone (' Select value from ' $this->storage_name. ' Where id= '. $key. ');
- if ($return = = $this->data_too_long_instead_value)
- {
- $return = $this->getone (' Select value from ' $this->storage_name_slow. ' Where id= '. $key. ');
- }
- if (! $return)
- {
- $mysqlError = mysql_error ($this->conn);
- if (Strpos ($mysqlError, "doesn ' t exist")!==false)
- {
- $this->inittable ();
- }
- $return = Array ();
- }
- Else
- {
- $return = Unserialize ($return);
- }
- return $return;
- }
- Public function Close () {
- @mysql_close ($this->conn);
- }
- Public Function Cleanup () {
- if ($this->mysql_version>4) {
- $sql = ' Delete from '. $this->storage_name. ' Where Date_add (' time ', INTERVAL '. $this->expire_time. ' SECOND)
- }else{
- $sql = ' Delete from ' $this->storage_name_slow. ' WHERE ' time ' + '. $this->expire_time. '
- if ($_session[' username ']== "Leinchu") {
- Echo $sql;
- }
- $this->execute ($sql);
- $sql = ' Delete from ' $this->storage_name. ' WHERE ' time ' + '. $this->expire_time. '
- if ($_session[' username ']== "Leinchu") {
- Echo $sql;
- }
- }
- $this->execute ($sql);
- }
- Public Function Refresh ($key = "") {
- if ($this->mysql_version>4) {
- $sql = ' Update '. $this->storage_name. ' Set ' time ' =current_timestamp () where id= "'. $key. '";
- }else{
- $sql = ' Update '. $this->storage_name. ' Set ' time ' =unix_timestamp () where id= "'. $key. '";
- }
- $return = $this->execute ($sql);
- if (! $return) {
- $this->inittable ();
- $return = $this->execute ($sql, true);
- }
- return $return;
- }
- Public Function Create ($key = "", $value = "") {
- if ($key = = "") $key = $this->id;
- if ($value! = "") $value = mysql_real_escape_string (serialize ($value), $this->conn);
- if (strlen ($value) > $this->max_session_data_length)
- {
- if ($this->mysql_version>4) {
- throw new Exception (__file__. " ". __line__.": Session data is a long than max allow Length (". $this->max_session_data_length.")! ");
- }
- }
- if ($this->mysql_version>4) {
- $sql = ' replace into ' $this->storage_name. ' Set value=/'. $value. ' /', id= "'. $key. '", ' Time ' =current_timestamp () ';
- }else{
- $sql = ' replace into ' $this->storage_name. ' Set value=/'. $value. ' /', id= "'. $key. '", ' Time ' =unix_timestamp () ';
- }
- $return = $this->execute ($sql);
- if (! $return) {
- $this->inittable ();
- $return = $this->execute ($sql, true);
- }
- return $return;
- }
- Public function set ($key = "", $value = "") {
- if ($key = = "") $key = $this->id;
- if ($value! = "") $value = mysql_real_escape_string (serialize ($value), $this->conn);
- $sql = ' Update '. $this->storage_name. ' Set value=/'. $value. ' /' Where id= ' '. $key. ';
- if (strlen ($value) > $this->max_session_data_length)
- {
- if ($this->mysql_version>4) {
- throw new Exception (__file__. " ". __line__.": Session data is a long than max allow Length (". $this->max_session_data_length.")! ");
- }
- $sql = ' replace into ' $this->storage_name_slow. ' Set value=/'. $value. ' /', id= "'. $key. '", ' Time ' =unix_timestamp () ';
- $this->execute ($sql, true);
- $sql = ' Update '. $this->storage_name. ' Set value=/'. $this->data_too_long_instead_value. ' /' Where id= ' '. $key. ';
- }
- $return = $this->execute ($sql);
- if (! $return) {
- $this->inittable ();
- $return = $this->execute ($sql, true);
- }
- return $return;
- }
- Private Function inittable () {
- if ($this->mysql_version>4) {
- $sql = "
- CREATE TABLE if not exists ' ". $this->storage_name." ` (
- ' id ' char (+) not NULL default ' ERR ',
- ' Value ' VARBINARY (". $this->max_session_data_length.") Null
- ' Time ' timestamp not NULL for default current_timestamp on update Current_timestamp,
- PRIMARY KEY (' id '),
- KEY ' time ' (' time ')
- ) Engine=memory;
- ";
- }else{
- $sqlSlow = "
- CREATE TABLE if not exists ' ". $this->storage_name." _slow ' (
- ' id ' char (+) not NULL default ' ERR ',
- ' Value ' text NULL,
- ' Time ' int (ten) NOT null default ' 0 ',
- PRIMARY KEY (' id '),
- KEY ' time ' (' time ')
- ) Engine=myisam;
- ";
- $this->execute ($sqlSlow, true);
- $sql = "
- CREATE TABLE if not exists ' ". $this->storage_name." ` (
- ' id ' char (+) not NULL default ' ERR ',
- ' Value ' VARCHAR (255) NULL,
- ' Time ' int (ten) NOT null default ' 0 ',
- PRIMARY KEY (' id '),
- KEY ' time ' (' time ')
- ) Engine=memory;
- ";
- }
- return $this->execute ($sql, true);
- }
- Private function Execute ($sql, $die =false)
- {
- if ($die)
- {
- mysql_query ($sql, $this->conn) or Die ("EXE SQL error:
". Mysql_error ()." ". $sql.");
- }
- Else
- {
- mysql_query ($sql, $this->conn);
- if (Mysql_error ()) {
- return false;
- }else{
- return true;
- }
- }
- }
- Private Function GetOne ($sql, $die =false) {
- $rs = $this->query ($sql, $die);
- if ($rs && ($one = mysql_fetch_row ($rs))) {
- return $one [0];
- }else{
- return false;
- }
- }
- Private function Query ($sql, $die =false) {
- if ($die)
- $rs = mysql_query ($sql, $this->conn) or Die ("Query SQL error:
". Mysql_error ()." ". $sql.");
- Else
- $rs = mysql_query ($sql, $this->conn);
- return $rs;
- }
- }
- $lnk = mysql_connect (' localhost ', ' root ', ' 123456 ')
- Or Die (' Not connected: '. mysql_error ());
- Make Foo the current db
- mysql_select_db (' Test ', $lnk) or Die (' can/' t use foo: '. mysql_error ());
- $S = new session ($LNK);
- if (! $S->last) {
- $S->last = time ();
- }
- echo "First visit at". $S->last. "
";
- if (! $S->lastv) {
- $S->lastv = 0;
- }
- $S->lastv++;
- echo "lastv=". $S->lastv. "
";
- echo "test=". $S->test. "
";
- if (Isset ($_get[' Max ')) {
- $S->boom = str_repeat ("OK", 255);
- }
- if (isset ($_get[' boom ')) {
- $S->boom = $_get[' boom ');
- }
- echo "boom=". $S->boom. "
";
- ?>
Copy Code |