Re: PEAR Session Class
| From: | Erik Hjortsberg | Date: | Fri, 09 Nov 2001 15:28:01 +0000 |
| Subject: | Re: PEAR Session Class | ||
| References: | 1 | Groups: | php.pear.general |
| Request: | Send a blank email to pear-general+get-495@lists.php.net to get a copy of this message | ||
torsdagen den 8 november 2001 15:26 skrev Martin Jansen:
> On Fri, 9 Nov 2001 22:44:27 +0700, Ahmad Munif wrote:
> >Why PEAR package not include session class ?
>
> What should this session class do?
>
> - Martin
I use this _Session-class in all my projects. It is often extended to provide
more specific functionality (such as authorization).
By using the class I automaticly have access to the datetime of both the last
session by the user (if such exists) and the last request. Also total number
of sessions, total number of request, ip, referer, user agent string
(extended classes often store these values in a database and also adds
browser-detection etc.)
The methods setValue('key', $value) and getValue('key') are basic, but more
interesting are setPageValue('key', $value) and getPageValue('key') which
allows one to store and access values in the session in a per page basis.
The 'auth'-methods doesn't do anything here but provide an interface for an
extended class. You basically do test like
<code>
if ($SESSION->auth('/admin/pages.php')) {
yadayada
}
</code>
Is this of interest? (it needs some cleaning up though)
Erik Hjortsberg
<code>
class _Session {
var $uA_String = ''; //useragent string
var $ip = ''; //visitors ip-number
var $referer = ''; //visitors referer if one such exists
var $prevRequest = ''; //time of the previous request
var $currentRequest = ''; //time of the current request
var $prevSession = ''; //time of the previous session
var $currentSession = ''; //time of the previous session
var $numberOfRequests = 0; //total number of request made in this session
var $numberOfSessions = 0; //total number of sessions
var $_pageValues = array(); //values specific to a certain page
var $_values = array(); //values in the session
/**
* constructor
* stores information about the session such as ip-number, referrer and
UA-string
*
* @param boolean : whether to use cookies or not
*/
function _Session($useCookies = true) {
if (isset($GLOBALS['HTTP_SERVER_VARS']['REMOTE_ADDR'])) {
$this->ip = $GLOBALS['HTTP_SERVER_VARS']['REMOTE_ADDR'];
}
if (isset($GLOBALS['HTTP_SERVER_VARS']['HTTP_REFERER'])) {
$this->referer = $GLOBALS['HTTP_SERVER_VARS']['HTTP_REFERER'];
}
if (isset($GLOBALS['HTTP_SERVER_VARS']['HTTP_USER_AGENT'])) {
$this->uA_String = $GLOBALS['HTTP_SERVER_VARS']['HTTP_USER_AGENT'];
}
if (isset($GLOBALS['HTTP_COOKIE_VARS']['SESSION_SESSION_prevSession'])) {
$this->prevSession =
$GLOBALS['HTTP_COOKIE_VARS']['SESSION_SESSION_prevSession'];
$this->numberOfSessions =
$GLOBALS['HTTP_COOKIE_VARS']['SESSION_SESSION_numberOfSessions'];
}
$this->numberOfSessions++;
if (!headers_sent()) {
//register the session in the users browser
setcookie('SESSION_SESSION_prevSession', date('Y-m-d h:n:s'), time()+(3600
* 24 * 7 * 4));
setcookie('SESSION_SESSION_numberOfSessions', $this->numberOfSessions,
time()+(3600 * 24 * 7 * 4));
}
$this->currentSession = date('Y-m-d h:n:s');
//$this->_updateRequest();
}
//
//---------public instance methods begins here
//
/**
*verifies that the session has access to the specified page
*OVERRIDE THIS IN SUBCLASS!
*
*@param string
*@return boolean
*@access public
*/
function auth($page) {
return true;
}
/**
*verifies that the user has access to the current page, if not, exits
*@access public
*/
function authThisPage() {
if (!$this->auth(substr($GLOBALS['HTTP_SERVER_VARS']['PHP_SELF'],
strlen(HTTP_ROOT), strlen($GLOBALS['HTTP_SERVER_VARS']['PHP_SELF']) -
strlen(HTTP_ROOT)))) {
$this->_isNotAuthorized();
}
}
/**
*sets a value local to the current page (or a specified page)
*@param string
*@param variant
*@param string
*@access public
*/
function setPageValue($key, $value, $page = null) {
if ($page === null) {
$page = $GLOBALS['HTTP_SERVER_VARS']['PHP_SELF'];
}
if (!isset($this->_pageValues[$page])) {
$this->_pageValues[$page] = array();
}
$this->_pageValues[$page][$key] = $value;
}
/**
*gets value local to the current page (or a specified page)
*if none exists false is returned
*@param string
*@param string
*@return boolean|variant
*@access public
*/
function getPageValue($key, $page = null) {
if ($page === null) {
$page = $GLOBALS['HTTP_SERVER_VARS']['PHP_SELF'];
}
if (!isset($this->_pageValues[$page])) {
return false;
}
if (!isset($this->_pageValues[$page][$key])) {
return false;
}
return $this->_pageValues[$page][$key];
}
/**
*sets a value
*@param string
*@param variant
*@access public
*/
function setValue($key, $value) {
$this->_values[$key] = $value;
}
/**
*gets a value
*if none exists false is returned
*@param string
*@return boolean|variant
*@access public
*/
function getValue($key) {
if (!isset($this->_values[$key])) {
return false;
}
return $this->_values[$key];
}
function link($path, $text)
{
if ($this->auth($path)) {
return "<a href='$path'>$text</a>";
}
}
//
//---------private instance methods begins here
//
/**
*function that is called when the user isn't authorized to access the
current page, should be overridden in the subclass
*OVERRIDE THIS IN SUBCLASS!
*@param string
*@return boolean
*@access public
*/
function _isNotAuthorized() {
die('You are not authorized to view this page!');
}
/**
*updates the date of the current request and saves the date of the previous
*this is usually called at __wakeup-time
*
*@access private
*/
function _updateRequest() {
$this->numberOfRequests++;
$this->prevRequest = $this->currentRequest;
$this->currentRequest = date('Y-m-d h:n:s');
}
/**
* primitive function called at the start of an existing session when the
object is unserialized
*
* @access private
*/
function __wakeup() {
$this->_updateRequest();
}
/**
* primitive function called at the end of an existing session when the
object is serialized
*
* @access private
*/
/*
function __sleep() {
return
array('uA_String','ip','referer','prevRequest','currentRequest','prevSession','currentSession','numberOfRequests','numberOfSessions');
}
*/
//
//---------public static methods begins here
//
//
//---------private static methods begins here
//
}
</code>