Welcome Guest, Not a member yet? Register   Sign In
Beta - but working - code for a new and extensive Session Library.
#1

[eluser]starbbs[/eluser]
Some time ago i tried to convert cakes session lib to ci. It's not done, but most of it works. You can replace this lib with the original session library.Maybe it will inspire someone to get ideas or to finish it ? It has a LOT of options and it;s working excellent

Code:
<?php
define('MIB_SESSION_SAVE', 'php');
define('MIB_SECURITY', 'medium');
define('MIB_SESSION_TIMEOUT', '10');
define('MIB_SESSION_STRING', 'DYhG93b0qyJfIxfs2guVoUubWwvniR2G0FgaC9mi');
define('LOG_ERROR', 2);

class CI_Session{
var $maxTime;
var $sessionTime = false;
var $time = false;
var $lastError = null;
var $path = false;
var $error = false;
var $valid = false;
var $security = null;
var $lasterror = null;

function CI_Session($start = true){
  if($start === true){
   $this->CI = & get_instance();
   $this->__active = $start;

   $this->tablename = 'ci_sessions';
   $this->host = $_SERVER['HTTP_HOST'];
   $this->ip_address = $_SERVER['REMOTE_ADDR'] ;
   $this->path = $this->CI->config->item('sess_cookie_path');

   if (empty($this->path)){
$this->path = '/';
   }
   if (strpos($this->host, ':') !== false){
$this->host = substr($this->host, 0, strpos($this->host, ':'));
   }
   if (mibGetParam($_SERVER, 'HTTP_USER_AGENT') != null){
$this->userAgent = mibGetParam($_SERVER, 'HTTP_USER_AGENT');
   }else{
$this->userAgent = 'Empty';
   }
   $this->time = $this->get_correct_time();
   $this->sessionlength = $this->CI->config->item('sess_expiration');
   $this->sessionTime = $this->time + ($this->inactiveMins() * MIB_SESSION_TIMEOUT);
   $this->security = MIB_SECURITY;

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

   $this->__initSession();

   session_cache_limiter ("must-revalidate");
   session_start();
   header ('P3P: CP="NOI ADM DEV PSAi COM NAV OUR OTRo STP IND DEM"');

   $this->__checkValid();

   $start = false;
  }
  // parent::__construct();
}

function get_correct_time(){
  /**
   * Set the "now" time
   *
   * It can either set to GMT or time(). The pref
   * is set in the config file.  If the developer
   * is doing any sort of time localization they
   * might want to set the session time to GMT so
   * they can offset the "last_activity" and
   * "last_visit" times based on each user's locale.
   */
  if (strtolower($this->CI->config->item('time_reference')) == 'gmt'){
   $now = time();
   $this->now = mktime(gmdate("H", $now), gmdate("i", $now), gmdate("s", $now), gmdate("m", $now), gmdate("d", $now), gmdate("Y", $now));

   if (strlen($this->now) < 10){
$this->now = time();
log_message('error', 'The session class could not set a proper GMT timestamp so the local time() value was used.');
   }
  }else{
   $this->now = time();
  }
}

function inactiveMins(){
  // $security =& Security::getInstance();
  switch (MIB_SECURITY){
   case 'high':
return 10;
break;
   case 'medium':
return 100;
break;
   case 'low':
   default :
return 300;
break;
  }
}

function __initSession(){
  switch(MIB_SECURITY){
   case 'high':
$this->cookieLifeTime = 0;
if (function_exists('ini_set')){
  ini_set('session.referer_check', $this->host);
}
break;
   case 'medium':
$this->cookieLifeTime = 7 * 86400;
break;
   case 'low':
   default:
$this->cookieLifeTime = 788940000;
break;
  }

  if (!isset($_SESSION)){
   if (function_exists('ini_set')){
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', $this->CI->config->item('sess_cookie_name'));
ini_set('session.cookie_lifetime', $this->cookieLifeTime);
ini_set('session.cookie_path', $this->path);
ini_set('session.gc_probability', 5);
ini_set('session.auto_start', 0);
   }
   session_set_save_handler(array($this, '__open'),
array($this, '__close'),
array($this, '__read'),
array($this, '__write'),
array($this, '__destroy'),
array($this, '__gc'));
  }
}

function isValid(){
  return $this->valid;
}

function readSessionVar($name = null){
  if (is_null($name)){
   return $this->returnSessionVars();
  }
  if ($this->checkSessionVar($name)){
   $result = eval("return " . $this->__sessionVarNames($name) . ";");
   return $result;
  }
  $this->__setError(2, "$name doesn't exist");
  $return = null;
  return $return;
}
/**
  * Writes value to given session variable name.
  * @param mixed $name
  * @param string $value
  * @return void
  */
function writeSessionVar($name, $value){
  $expression = $this->__sessionVarNames($name);
  $expression .= " = \$value;";
  eval ($expression);
}
// continue next page


Messages In This Thread
Beta - but working - code for a new and extensive Session Library. - by El Forum - 07-19-2007, 12:06 PM



Theme © iAndrew 2016 - Forum software by © MyBB