Witam,
Zend samego w sobie machanizmu sesji nie ma. Nic nie stoi na przeszkodzie zeby uzywac $_SESSION.
Przyklad mniej wiecej taki:
1. session_start() w index.php
2. Akcja Logowanie:
- sprawdz czy istnieje uzytkownik w bazie (haslo i login poprawne)
- $_SESSION['zalogowany'] = true;
I juz (IMG:
http://forum.php.pl/style_emoticons/default/winksmiley.jpg)
Bardzo proste i bardzo nieladne.
Napisalem (moim zdaniem) bardzo ciekawy mechanimz obslugi sesji.
ZałożeniaFramework udostępnia klasę Rapide_Session do obsługi sesji.
<?php
Rapide::loadInterface('Rapide_Session_Interface');
Rapide::loadClass('Rapide_Session_Exception');
Rapide::loadInterface('Rapide_Session_Handler_Interface');
class Rapide_Session
{
private
$_bSessionStarted = false,
$_sNameSpace;
public function __construct($sNameSpace = null)
{
{
$this->_sNameSpace = $sNameSpace;
if(!isset($_SESSION[$this->_sNameSpace
])) $_SESSION[$this->_sNameSpace
] = array(); }
}
public function getNameSpaceName()
{
return $this->_sNameSpace;
}
public function setHandler(Rapide_Session_Handler_Interface $oHandler)
{
if($this->started())
{
$sError = 'Session has already started';
throw new Rapide_Session_Exception($sError);
}
(
array(& $oSessionHandler, 'open'), array(& $oSessionHandler, 'close'), array(& $oSessionHandler, 'read'), array(& $oSessionHandler, 'write'), array(& $oSessionHandler, 'destroy'), array(& $oSessionHandler, 'gc') );
}
public function started()
{
}
public function start()
{
if(!$this->started())
{
{
$sError = 'Headers has already sent';
throw new Rapide_Session_Exception($sError);
}
}
}
public function close()
{
if(!$this->started())
{
$sError = 'Session has not started yet';
throw new Rapide_Session_Exception($sError);
}
if(isset($this->_sNameSpace
)) $_SESSION[$this->_sNameSpace
] = array(); else
$this->destroy();
}
public function destroy()
{
if(!$this->started())
{
$sError = 'Session has not started yet';
throw new Rapide_Session_Exception($sError);
}
}
public function __get($sParameters)
{
if(!$this->started())
{
$sError = 'Session has not started yet';
throw new Rapide_Session_Exception($sError);
}
if(isset($this->_sNameSpace
)) {
return isset($_SESSION[$this->_sNameSpace
][$sParameters]) ?
$_SESSION[$this->_sNameSpace][$sParameters] :
null;
}
else
{
return isset($_SESSION[$sParameters]) ?
$_SESSION[$sParameters] :
null;
}
}
public function __set($sParameters, $mValue)
{
if(!$this->started())
{
$sError = 'Session has not started yet';
throw new Rapide_Session_Exception($sError);
}
if(isset($this->_sNameSpace
)) $_SESSION[$this->_sNameSpace][$sParameters] = $mValue;
else
$_SESSION[$sParameters] = $mValue;
}
public function __isset($sParameters)
{
if(!$this->started())
{
$sError = 'Session has not started yet';
throw new Rapide_Session_Exception($sError);
}
if(isset($this->_sNameSpace
)) return isset($_SESSION[$this->_sNameSpace
][$sParameters]); else
return isset($_SESSION[$sParameters]); }
public function getNameSpace()
{
if(!$this->started())
{
$sError = 'Session has not started yet';
throw new Rapide_Session_Exception($sError);
}
if(isset($this->_sNameSpace
)) return $_SESSION[$this->_sNameSpace];
else
return $_SESSION;
}
}
?>
Klasa Rapide_Session jest we frameworku, ale w zasadzie skorzystac z niej powinien tylko sam framework (programista uzyje innej klasy).
Programista ma do uzytku klase Rapide_User
<?php
Rapide::loadInterface('Rapide_User_Interface');
Rapide::loadClass('Rapide_User_Exception');
Rapide::loadClass('Rapide_Session');
class Rapide_User implements Rapide_User_Interface
{
private
$_oSession,
$_oDataSession;
public function __construct()
{
$this->_oSession = new Rapide_Session('Rapide_User');
$this->_oDataSession = new Rapide_Session('Rapide_User_Data');
$this->_oSession->start();
$this->_oDataSession->start();
if(!isset($this->_oSession
->authenticated)) $this->_oSession->authenticated = false;
if(!isset($this->_oSession
->groups)) $this->_oSession
->groups = array(); }
public function getGroups()
{
return $this->_oSession->groups;
}
public function setGroups
($aGroups = array()) {
{
$sError = 'Argument 1 for Rapide_User::setGroups() must be an array';
throw new Rapide_User_Exception($sError);
}
$this->_oSession->groups = $aGroups;
}
public function hasGroup($sGroup)
{
foreach($this->getGroups() as $iKey => $sUserGroup)
{
if($sUserGroup == $sGroup)
return true;
}
return false;
}
public function isAuthenticated()
{
return $this->_oSession->authenticated;
}
public function setAuthenticated($bAuthenticated = true)
{
$this->_oSession->authenticated = (bool)$bAuthenticated;
}
public function __get($sParameter)
{
return $this->_oDataSession->$sParameter;
}
public function __set($sParameters, $mValue)
{
$this->_oDataSession->$sParameter;
}
public function __isset($sParameter)
{
return isset($this->_oDataSession
->$sParameter); }
public function destroy()
{
$this->setGroups(array()); $this->setAuthenticated(false);
$this->_oSession->close();
$this->_oDataSession->close();
}
}
?>
Kiedy programista chce zapisac dane w sesji, pisze:
<?php
$oUser = new Rapide_User;
$oUser->zmienna = 'wartosc';
?>
Ponadto klasa Rapide_User dostarcza metod dajacych mozliwosc kontrole bezpieczenstwa w aplikacji.
Po zalogowaniu sie nalezy zrobic:
<?php
$oUser->setAuthenticated();
?>
Pozniej w aby sprawdzic czy uzytkownik zostal zalogowany:
<?php
if($oUser->isAuthenticated()) {
//cos
}
?>
Przepraszam, ze post napisany na szybko, ale nie mam duzo czasu (IMG:
http://forum.php.pl/style_emoticons/default/winksmiley.jpg)
Moze pozniej przedstawie lepszy opis i uzycie.
Pozdrawiam, Adrian.