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

[eluser]starbbs[/eluser]
Code:
function renew(){
  $this->__regenerateId();
}

function __regenerateId(){
  $oldSessionId = session_id();
  $sessionpath = session_save_path();
  $cookiename = $this->CI->config->item('sess_cookie_name');

  if (empty($sessionpath)){
   $sessionpath = "/tmp";
  }

  if (isset($_COOKIE[session_name()])){
   setcookie($cookiename, '', time() - 42000, $this->path);
  }
  session_regenerate_id();
  $newSessid = session_id();
  $file = $sessionpath . '/' . "sess_$oldSessionId";
  @unlink ($file);
  @session_destroy ($oldSessionId);

  if (function_exists('session_write_close')){
   session_write_close();
  }
  $this->__initSession();
  session_id ($newSessid);
  session_start();
}

function __write($key, $value){
  log_message('debug', "__WRITE key $key - value $value");

  switch(MIB_SECURITY){
   case 'high':
    $factor = 10;
    break;
   case 'medium':
    $factor = 100;
    break;
   case 'low':
    $factor = 300;
    break;
   default:
    $factor = 10;
    break;
  }
  $CI = & get_instance();
  $expires = time() + MIB_SESSION_TIMEOUT * $factor;
  $results = $CI->db->query("SELECT COUNT(id) AS count FROM ci_sessions WHERE id = '" . $key . "'");
  $row_count = $results->num_rows();
  log_message('debug', "__WRITE No rows were found");

  if ($row_count > 1){
   $data1 = array('val' => $value,
    'expires' => $expires);

   $CI->db->where('id', $key);
   $CI->db->update('ci_sessions', $data1);
  }else{
   /**
    * $data2 = array( 'data' => $value,
    * 'expires'=> $expires,
    * 'id' => $key);
    *
    * $this->CI->db->insert($this->tablename, $data2);
    */
   $sql = "INSERT INTO ci_sessions (val,expires,id) VALUES ('$value','$expires','$key')";
   $CI->db->query($sql);
   // echo "INSERTED $sql found records:".$CI->db->affected_rows();
  }
  return true;
}

/**
  * Fetch a specific item form  the session array
  *
  * @access public
  * @param string $
  * @return string
  */
function userdata($item){
  return (! isset($this->userdata[$item])) ? false : $this->userdata[$item];
}
// --------------------------------------------------------------------
/**
  * Add or change data in the "userdata" array
  *
  * @access public
  * @param mixed $
  * @param string $
  * @return void
  */
function set_userdata($newdata = array(), $newval = ''){
  if (is_string($newdata)){
   $newdata = array($newdata => $newval);
  }

  if (count($newdata) > 0){
   foreach ($newdata as $key => $val){
    $this->userdata[$key] = $val;

    $this->writeSessionVar($key, $val);
   }
  }

  $this->renew();
}

function delSessionVar($name){
  if ($this->checkSessionVar($name)){
   $var = $this->__sessionVarNames($name);
   eval ("unset($var);");
   return true;
  }
  $this->__setError(2, "$name doesn't exist");
  return false;
}
// --------------------------------------------------------------------
/**
  * Delete a session variable from the "userdata" array
  *
  * @access array
  * @return void
  */
function unset_userdata($newdata = array()){
  if (is_string($newdata)){
   $newdata = array($newdata => '');
  }

  if (count($newdata) > 0){
   foreach ($newdata as $key => $val){
    $this->delSessionVar($key);
    unset($this->userdata[$key]);
   }
  }
}

/**
  * Sets session attributes to the given values
  */
function set($newkey, $newval){
  log_message('debug', 'Session SET with key ' . $newkey . ' with value ' . $newval . '');

  if (is_string($newkey) AND is_string($newval)){
   $this->writeSessionVar($newkey, $newval);
  }

  $newdata = array($newval);

  log_message('debug', "Problem with the SESSION lib. Cannot find an array $newdata,$newval");

  $this->writeSessionVar($newkey[0], $newdata);

  return;
}

function __destroy ($id){
  $id = int($id);
  log_message('debug', "The _destroy function is called for id ($id). Let's clean the SESSION");
  // $db->execute("DELETE FROM " . $db->name($table) . " WHERE " . $db->name($table.'.id') . " = " . $db->value($key, 'integer'));
  $this->CI->db->delete($this->tablename, array('session_id' => $id));
  return $true;
}

// 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:12 PM



Theme © iAndrew 2016 - Forum software by © MyBB