#5960: custom session handler
--------------------------+-------------------------------------------------
    Reporter:  ahuino1    |          Type:  Bug    
      Status:  new        |      Priority:  Medium 
   Milestone:  1.2.x.x    |     Component:  General
     Version:  1.2 Final  |      Severity:  Normal 
    Keywords:             |   Php_version:  n/a    
Cake_version:             |  
--------------------------+-------------------------------------------------
 To define a custom session handler.
 I write code by follow:

 {{{
 Configure::write('Session.save', 'my_session');
 }}}

 In my config dir,I write a file my_session.php

 {{{
 <?php
 class MySession {
         var $security = null;
         var $path = '/';
         var $host       =       null;
         var $time;
         var $cookieLifeTime;
         var $sessionTime;
     /**
      * 构造函数
      */
     function __construct() {
         //保存当前时间
         $this->time = time();

         $this->host = env('HTTP_HOST');

                 if (strpos($this->host, ':') !== false) {
                         $this->host = substr($this->host, 0,
 strpos($this->host, ':'));
                 }

                 if (!class_exists('Security')) {
                         App::import('Core', 'Security');
                 }

                 $this->sessionTime = $this->time +
 (Security::inactiveMins() * Configure::read('Session.timeout'));
                 $this->security = Configure::read('Security.level');



         $iniSet = function_exists('ini_set');

                 if ($iniSet && env('HTTPS')) {
                         ini_set('session.cookie_secure', 1);
                 }
                 switch ($this->security) {
                         case 'high':
                                 $this->cookieLifeTime = 0;
                                 if ($iniSet) {
                                         ini_set('session.referer_check',
 $this->host);
                                 }
                         break;
                         case 'medium':
                                 $this->cookieLifeTime = 7 * 86400;
                                 if ($iniSet) {
                                         ini_set('session.referer_check',
 $this->host);
                                 }
                         break;
                         case 'low':
                         default:
                                 $this->cookieLifeTime = 788940000;
                         break;
                 }

                 if (!isset($_SESSION)) {
                         if ($iniSet) {
                                 ini_set('session.use_trans_sid', 0);
                                 ini_set('url_rewriter.tags', '');
                                 ini_set('session.save_handler', 'user');
                                 ini_set('session.serialize_handler',
 'php');
                                 ini_set('session.use_cookies', 1);
                                 ini_set('session.name',
 Configure::read('Session.cookie'));
                                 ini_set('session.cookie_lifetime',
 $this->cookieLifeTime);
                                 ini_set('session.cookie_path',
 $this->path);
                                 ini_set('session.auto_start', 1);
                         }
                 }
         session_set_save_handler(
             array('MySession', 'sessionOpen'),
             array('MySession', 'sessionClose'),
             array('MySession', 'sessionRead'),
             array('MySession', 'sessionWrite'),
             array('MySession', 'sessionDestroy'),
             array('MySession', 'sessionGc')
         );

         if (!class_exists('ConnectionManager')) {
                         App::import('Core', 'ConnectionManager');
                 }
     }


     function __destruct() {
                 if (function_exists('session_write_close')) {
                         session_write_close();
                 }
         }

     /**
      * 打开 session
      * @return boolean
      */
     function sessionOpen() {
         return true;
     }

     /**
      * 关闭 session
      *
      * @return boolean
      */
     function sessionClose() {
         $probability = mt_rand(1, 150);
                 if ($probability <= 3) {
                         MySession::sessionGc();
                 }
                 return true;
     }

     /**
      * 读取指定 id 的 session 数据
      * @return string
      */
     function sessionRead($key) {
         $db =&
 ConnectionManager::getDataSource(Configure::read('Session.database'));
                 $table =
 $db->fullTableName(Configure::read('Session.table'), false);

                 $row = $db->query("SELECT " . $db->name($table.'.data') .
 " FROM " . $db->name($table) . " WHERE " . $db->name($table.'.id') . " = "
 . $db->value($key), false);

                 if ($row && !isset($row[0][$table]) && isset($row[0][0]))
 {
                         $table = 0;
                 }

                 if ($row && $row[0][$table]['data']) {
                         return $row[0][$table]['data'];
                 } else {
                         return false;
                 }
     }

     /**
      * 写入指定 id 的 session 数据
      * @return boolean
      */
     function sessionWrite($key, $value) {
                 $db =&
 ConnectionManager::getDataSource(Configure::read('Session.database'));
                 $table =
 $db->fullTableName(Configure::read('Session.table'));
                 switch (Configure::read('Security.level')) {
                         case 'high':
                                 $factor = 10;
                         break;
                         case 'medium':
                                 $factor = 100;
                         break;
                         case 'low':
                                 $factor = 300;
                         break;
                         default:
                                 $factor = 10;
                         break;
                 }
                 $expires = time() +  Configure::read('Session.timeout') *
 $factor;
                 $row = $db->query("SELECT COUNT(id) AS count FROM " .
 $db->name($table) . " WHERE "
                                                                  .
 $db->name('id') . " = "
                                                                  .
 $db->value($key), false);

                 if ($row[0][0]['count'] > 0) {
                         $db->execute("UPDATE " . $db->name($table) . " SET
 "
                                  . $db->name('data') . " = " .
 $db->value($value) . ", "
                                  . $db->name('expires') . " = " .
 $db->value($expires) . ", "
                                  . $db->name('useragent') . " = " .
 $db->value($_SERVER['HTTP_USER_AGENT'])
                                  . " WHERE " . $db->name('id') . " = " .
 $db->value($key));
                 } else {
                         $db->execute("INSERT INTO " . $db->name($table) .
 " ("
                                                                 .
 $db->name('data') . ","
                                                                 .
 $db->name('expires') . ","
                                                                 .
 $db->name('id') . ","
                                                                 .
 $db->name('useragent')
                                                                 . ")
 VALUES ("
                                                                 .
 $db->value($value) . ", "
                                                                 .
 $db->value($expires) . ", "
                                                                 .
 $db->value($key) . ", "
                                                                 .
 $db->value($_SERVER['HTTP_USER_AGENT'])
                                                                 . ")"
                                         );
                 }
                 return true;
     }

     /**
      * 销毁指定 id 的 session
      * @return boolean
      */
     function sessionDestroy($key) {
         $db =&
 ConnectionManager::getDataSource(Configure::read('Session.database'));
                 $table =
 $db->fullTableName(Configure::read('Session.table'));
                 $db->execute("DELETE FROM " . $db->name($table) . " WHERE
 " . $db->name($table.'.id') . " = " . $db->value($key));
                 return true;
     }

     /**
      * 清理过期的 session 数据
      * @return boolean
      */
     function sessionGc($expires = null) {
         $db =&
 ConnectionManager::getDataSource(Configure::read('Session.database'));
                 $table =
 $db->fullTableName(Configure::read('Session.table'));
                 $db->execute("DELETE FROM " . $db->name($table) . " WHERE
 " . $db->name($table.'.expires') . " < ". $db->value(time()));
                 return true;
     }
 }
 new MySession();
 }}}

 The result the application show the errors:

 {{{
 Warning (2): mysql_real_escape_string(): 53 is not a valid MySQL-Link
 resource [CORE\cake\libs\model\datasources\dbo\dbo_mysql.php, line 504]
 mysql_real_escape_string - [internal], line ??
 DboMysql::value() - CORE\cake\libs\model\datasources\dbo\dbo_mysql.php,
 line 504
 Warning (2): mysql_query(): 53 is not a valid MySQL-Link resource
 [CORE\cake\libs\model\datasources\dbo\dbo_mysql.php, line 407]
 Warning (2): mysql_errno(): 53 is not a valid MySQL-Link resource
 [CORE\cake\libs\model\datasources\dbo\dbo_mysql.php, line 515]
 }}}

 How can I do,thank you

-- 
Ticket URL: <https://trac.cakephp.org/ticket/5960>
CakePHP : The Rapid Development Framework for PHP <https://trac.cakephp.org/>
Cake is a rapid development framework for PHP which uses commonly known design 
patterns like ActiveRecord, Association Data Mapping, Front Controller and MVC. 
Our primary goal is to provide a structured framework that enables PHP users at 
all levels to rapidly develop robust web applications, without any loss to 
flexibility.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"tickets cakephp" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/tickets-cakephp?hl=en
-~----------~----~----~----~------~----~------~--~---

  • [CakePHP : The Rapid Dev... CakePHP : The Rapid Development Framework for PHP

Reply via email to