note 69614 deleted from ref.session by felipe

[email protected]
Newsgroups php.notes
Message-ID <[email protected]>
Note Submitter:  

----

Here is a replacement I wrote for Session Handeling.

Before use, you must replace "[--PATH-TO-SESSION-FOLDER--]" with the path to where sessions should be stored.  Make sure to set the proper permissions for that folder :)

--------------------------------------------------------------------

<?
 class Session
 {
  function _makeid()
  {
   $time    = time();
   $ip      = $_SERVER["REMOTE_ADDR"];
   $agent   = $_SERVER["HTTP_USER_AGENT"];
   $md5 = md5($time.$ip.$agent);
   setcookie("SClass", $md5);
   return($md5);
  }

  function getid()
  {
   $md5 = $_COOKIE['SClass'];
   return($md5);
  }

  function start()
  {
   $id = $this->getid();
   if (!$id) { $id = $this->_makeid(); }
  }

  function _getfile()
  {
   $file = $this->getid();
   $file .= ".ssn";
   $file = "[--PATH-TO-SESSION-FOLDER--]".$file;
   return($file);
  }

  function _setdata($data)
  {
   $file = $this->_getfile();
   $fs   = @fopen($file, w);
   if ($fs) { fwrite($fs, $data); fclose($fs);}
   if (!$fs) { return(FALSE); }
  }

  function _getdata()
  {
   $file = $this->_getfile();
   $fs   = @fopen($file, 'r');
   if ($fs) { $data = fread($fs, filesize($file)); fclose($fs); return $data; }
   if (!$fs) { return(FALSE); }
  }

  function set($key, $value)
  {
   $data          = $this->_getdata();
   $data          = base64_decode($data);
   $array         = unserialize($data);
   $array["$key"] = $value;
   $newdata       = serialize($array);
   $newdata       = base64_encode($newdata);
   $this->_setdata($newdata);
  }

  function get($key)
  {
   $data = $this->_getdata();
   $data = base64_decode($data);
   $array = unserialize($data);
   return($array[$key]);
  }

 }
?>

--------------------------------------------------------------------
Useage:

 $Session = new Session();
 $Session->start();
 $Session->set($key, $value);
 $sessionvalue = $Session->get($key);

--------------------------------------------------------------------

- Jestin S Larson
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.