note 69614 added to ref.session
| From: | php-general at lists dot php dot net | Date: | Tue, 12 Sep 2006 18:28:35 +0000 |
| Subject: | note 69614 added to ref.session | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-117076@lists.php.net to get a copy of this message | ||
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
----
Server IP: 216.194.113.175
Probable Submitter: 24.41.34.158
----
Manual Page -- http://www.php.net/manual/en/ref.session.php
Edit -- https://master.php.net/note/edit/69614
Del: integrated -- https://master.php.net/note/delete/69614/integrated
Del: useless -- https://master.php.net/note/delete/69614/useless
Del: bad code -- https://master.php.net/note/delete/69614/bad+code
Del: spam -- https://master.php.net/note/delete/69614/spam
Del: non-english -- https://master.php.net/note/delete/69614/non-english
Del: in docs -- https://master.php.net/note/delete/69614/in+docs
Del: other reasons-- https://master.php.net/note/delete/69614
Reject -- https://master.php.net/note/reject/69614
Search -- https://master.php.net/manage/user-notes.php