Here is a little class I've written to make handling cookie sessions a little nicer in PHP. I'm not a big fan of session cookies but PHP makes a real mess of 'transparent' session ids with search engines - making it think you have infinite web pages.. so here goes
class CookieManager{
//Copyright Mike Lang - icurtain.co.uk - please retain this header and give credit if used.
//documentation
//set your cookie up or it will throw exceptions - name/value
//you can then create the cookie by calling $cookie->creatCookie();
//you can keep it alive with $cookie->keepActive();
//you can destroy it by calling $cookie->destroy(); which sets the expire date to the past
//plus you can do all the usual cookie config malarky - no point implementing a contructor that takes params
//as setcookie(); already does this.
//if you have any comments or suggestions for improving or changing this class feel free to mail me
//mike [at] bluemedia dot co dot uk
private $cookieName = null;
private $sessValue = null;
private $time = null;
private $expireTime = null;
private $url = null;
private $directory = null;
private $https = null;
private $httpOnly = null;
public function CookieManager(){
//note if your server is in a differnt time zone then the setTime function will break
$this->setTime(time());
$this->setExpireTime(0);
//default access to entire domain
$this->setDirectory('/');
//can be sent unencrypted by default
$this->setHttps(0);
//can only be sent over http
$this->setHttpOnly(1);
}
public function getCookieName(){
return $this->cookieName;
}
public function setCookieName($cookieName){
$this->cookieName = $cookieName;
}
public function getSessValue(){
return $this->sessValue;
}
public function setSessValue($sessValue){
$this->sessValue = $sessValue;
}
public function getExpireTime(){
return $this->expireTime;
}
public function setExpireTime($expireTime){
$this->expireTime = $expireTime;
}
public function setUrl($url){
$this->url = $url;
}
public function getUrl(){
return $this->url;
}
public function setDirectory($directory){
$this->directory = $directory;
}
public function getDirectory(){
return $this->directory;
}
public function setHttps($https){
$this->https = $https;
}
public function getHttps(){
return $this->https;
}
public function setHttpOnly($httpOnly){
$this->httpOnly = $httpOnly;
}
public function getHttpOnly(){
return $this->httpOnly;
}
//actual code
private function isValid(){
if($this->getSessValue()==null){
throw new exception('Session value not set');
}
if($this->getCookieName()==null){
throw new exception('Cookie name not set');
}
}
public function createCookie(){
$this->isValid();
$success = setcookie($this->cookieName,
$this->getSessValue(),
$this->getTime() + $this->getExpireTime(),
$this->getDirectory(),
$this->getUrl(),
$this->getHttps()
);
//only validates cookie creation - not user acceptance
return $success;
}
public function getCookie(){
if($this->isActive()){
return $_COOKIE[$this->cookieName];
}
return null;
}
public function isActive(){
return isset($_COOKIE[$this->cookieName]);
}
public function keepActive(){
if($this->isActive()){
$this->setTime(time());
$this->createCookie();
}
}
public function destroy(){
if($this->isActive()){
$this->setExpireTime(-10);
$this->createCookie();
}
}
//private functions
private function setTime($time){
$this->time = $time;
}
private function getTime(){
return $this->time;
}
}
//test harness
$blah = new CookieManager();
$blah -> setCookieName('bluemedia');
$blah -> setSessValue(rand());
$blah -> setExpireTime(200);
$blah -> createCookie();
if(!$blah->isActive()){
echo '
You probably don\'t have cookies enabled or this is the first time you have visited this page