PHP Classes
elePHPant
Icontem

File: handlers/writeSometimes.inc.php

Recommend this page to a friend!
  Classes of Colin McKinnon  >  Stackable PHP Session Handler  >  handlers/writeSometimes.inc.php  >  Download  
File: handlers/writeSometimes.inc.php
Role: Class source
Content type: text/plain
Description: Reduce unnecessary writes to improve performance and scalability
Class: Stackable PHP Session Handler
Store session data with multiple session handlers
Author: By
Last change:
Date: 1 year ago
Size: 1,243 bytes
 

Contents

Class file image Download
<?php
/**
 * writeSometimes
 * a filter which reduces unnecessary writes
 */
class writeSometimes extends stackSess {
    private
$dataread;

    function
open($save_path, $name)
    {
        if (
$this->shStackNext) {
                        return
$this->shStackNext->open($save_path, $name);
        } else {
           
trigger_error("writeSometimes session handler has no storage capability");
            return
false;
        }
    }
    function
read($session_id)
    {
        if (
$this->shStackNext) {
           
$session_data=$this->shStackNext->read($session_id);
           
$this->dataread=sha1($session_id . $session_data);
            return
$session_data;
        } else {
            return
'';
        }
    }
    function
write($session_id, $session_data)
    {
        if (
$this->shStackNext) {
           
$data_to_write=sha1($session_id . $session_data);
            if (
$data_to_write!=$this->dataread) {
                            return
$this->shStackNext->write($session_id, $session_data);
            }
           
$age=time() - $this->shStackNext->lastAccessed();
           
$max=ini_get('session.gc_maxlifetime');
            if (
$age<$max && $age>0.7*$max) {
                return
$this->shStackNext->write($session_id, $session_data);
            }
            if (
$age>$max) {
               
// session should have expired
               
return false;
            }
           
// write is redundant
           
return true;
                }
        return
false;
    }
}