session/cookie wrapper class
Posted: Thu May 17, 2007 4:41 pm
I'm doing a wrapper class for handling sessions and cookies... but i'm getting an error 'headers already sent' and not quite sure why...
The error is on line 27 which is
Code: Select all
Class Security {
/**
* set() - Sets a session and cookie
* get() - Tries to get session, if it's not available, tries to get cookie
* encode() - encodes the session value
* decode() - decodes the session value
* __destruct - Destroys both sessions and cookies
*/
function __construct(){
if(!isset($_SESSION)) session_start();
}
function __destruct(){
session_unset();
session_destroy();
if(isset($_COOKIE)){
foreach($_COOKIE as $key => $cookie){
setcookie($key, "", time() - 3600);
}
}
}
public function set($name, $value, $cookie = null){
$this->name = $name;
$this->value = $value;
$this->cookie = $cookie;
$_SESSION[$this->name] = $this->encode($this->value);
if(!is_null($this->cookie)){
setcookie($this->name, $this->encode($this->value));
}
}
public function get($name){
$this->name = $name;
if(isset($_SESSION[$this->name])){
return $this->decode($_SESSION[$this->name]);
}elseif(isset($_COOKIE[$this->name])){
return $this->decode($_COOKIE[$this->name]);
}else{
return false;
}
}
private function encode($value){
return base64_encode($value);
}
private function decode($value){
return base64_decode($value);
}
}Code: Select all
$security = new Security();
$security->set('test','123');
echo $security->get('test');Code: Select all
setcookie($key, "", time() - 3600);