Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> Sesje w Zend Framework
1maciek
post
Post #1





Grupa: Zarejestrowani
Postów: 75
Pomógł: 0
Dołączył: 12.10.2004

Ostrzeżenie: (10%)
X----


Witam,
szukam informacji na temat używania seji w Zend Framework, może ktoś gdzieś widział coś na ten temat, albo wie coś o tym więcej? Może jakiś tutorial z np. systemem logowania?
Z góry wielkie dzięki
Pozdrawiam
Go to the top of the page
+Quote Post
 
Start new topic
Odpowiedzi
Prph
post
Post #2





Grupa: Zarejestrowani
Postów: 338
Pomógł: 2
Dołączył: 4.03.2006
Skąd: Łódź

Ostrzeżenie: (0%)
-----


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żenia

Framework udostępnia klasę Rapide_Session do obsługi sesji.

  1. <?php
  2.  
  3. Rapide::loadInterface('Rapide_Session_Interface');
  4. Rapide::loadClass('Rapide_Session_Exception');
  5.  
  6. Rapide::loadInterface('Rapide_Session_Handler_Interface');
  7.  
  8. class Rapide_Session
  9. {
  10. private
  11. $_bSessionStarted = false,
  12. $_sNameSpace;
  13.  
  14. public function __construct($sNameSpace = null)
  15. {
  16. if(isset($sNameSpace))
  17. {
  18. $this->_sNameSpace = $sNameSpace;
  19.  
  20. if(!isset($_SESSION[$this->_sNameSpace]))
  21. $_SESSION[$this->_sNameSpace] = array();
  22. }
  23. }
  24.  
  25. public function getNameSpaceName()
  26. {
  27. return $this->_sNameSpace;
  28. }
  29.  
  30. public function setHandler(Rapide_Session_Handler_Interface $oHandler)
  31. {
  32. if($this->started())
  33. {
  34. $sError = 'Session has already started';
  35. throw new Rapide_Session_Exception($sError);
  36. }
  37.  
  38. (
  39. array(& $oSessionHandler, 'open'),
  40. array(& $oSessionHandler, 'close'),
  41. array(& $oSessionHandler, 'read'),
  42. array(& $oSessionHandler, 'write'),
  43. array(& $oSessionHandler, 'destroy'),
  44. array(& $oSessionHandler, 'gc')
  45. );
  46. }
  47.  
  48. public function started()
  49. {
  50. return (bool)strlen(session_id());
  51. }
  52.  
  53. public function start()
  54. {
  55. if(!$this->started())
  56. {
  57. {
  58. $sError = 'Headers has already sent';
  59. throw new Rapide_Session_Exception($sError);
  60. }
  61.  
  62. }
  63. }
  64.  
  65. public function close()
  66. {
  67. if(!$this->started())
  68. {
  69. $sError = 'Session has not started yet';
  70. throw new Rapide_Session_Exception($sError);
  71. }
  72.  
  73. if(isset($this->_sNameSpace))
  74. $_SESSION[$this->_sNameSpace] = array();
  75. else
  76. $this->destroy();
  77. }
  78.  
  79. public function destroy()
  80. {
  81. if(!$this->started())
  82. {
  83. $sError = 'Session has not started yet';
  84. throw new Rapide_Session_Exception($sError);
  85. }
  86.  
  87. }
  88.  
  89. public function __get($sParameters)
  90. {
  91. if(!$this->started())
  92. {
  93. $sError = 'Session has not started yet';
  94. throw new Rapide_Session_Exception($sError);
  95. }
  96.  
  97. if(isset($this->_sNameSpace))
  98. {
  99. return isset($_SESSION[$this->_sNameSpace][$sParameters]) ?
  100.  $_SESSION[$this->_sNameSpace][$sParameters] :
  101.  null;
  102. }
  103. else
  104. {
  105. return isset($_SESSION[$sParameters]) ?
  106.  $_SESSION[$sParameters] :
  107.  null;
  108. }
  109. }
  110.  
  111. public function __set($sParameters, $mValue)
  112. {
  113. if(!$this->started())
  114. {
  115. $sError = 'Session has not started yet';
  116. throw new Rapide_Session_Exception($sError);
  117. }
  118.  
  119. if(isset($this->_sNameSpace))
  120. $_SESSION[$this->_sNameSpace][$sParameters] = $mValue;
  121. else
  122. $_SESSION[$sParameters] = $mValue;
  123. }
  124.  
  125. public function __isset($sParameters)
  126. {
  127. if(!$this->started())
  128. {
  129. $sError = 'Session has not started yet';
  130. throw new Rapide_Session_Exception($sError);
  131. }
  132.  
  133. if(isset($this->_sNameSpace))
  134. return isset($_SESSION[$this->_sNameSpace][$sParameters]);
  135. else
  136. return isset($_SESSION[$sParameters]);
  137. }
  138.  
  139. public function getNameSpace()
  140. {
  141. if(!$this->started())
  142. {
  143. $sError = 'Session has not started yet';
  144. throw new Rapide_Session_Exception($sError);
  145. }
  146.  
  147. if(isset($this->_sNameSpace))
  148. return $_SESSION[$this->_sNameSpace];
  149. else
  150. return $_SESSION;
  151. }
  152. }
  153.  
  154. ?>


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

  1. <?php
  2.  
  3. Rapide::loadInterface('Rapide_User_Interface');
  4. Rapide::loadClass('Rapide_User_Exception');
  5. Rapide::loadClass('Rapide_Session');
  6.  
  7. class Rapide_User implements Rapide_User_Interface
  8. {
  9. private
  10. $_oSession,
  11. $_oDataSession;
  12.  
  13. public function __construct()
  14. {
  15. $this->_oSession  = new Rapide_Session('Rapide_User');
  16. $this->_oDataSession = new Rapide_Session('Rapide_User_Data');
  17.  
  18. $this->_oSession->start();
  19. $this->_oDataSession->start();
  20.  
  21. if(!isset($this->_oSession->authenticated))
  22. $this->_oSession->authenticated = false;
  23.  
  24. if(!isset($this->_oSession->groups))
  25. $this->_oSession->groups = array();
  26. }
  27.  
  28. public function getGroups()
  29. {
  30. return $this->_oSession->groups;
  31. }
  32.  
  33. public function setGroups($aGroups = array())
  34. {
  35. if(!is_array($aGroups))
  36. {
  37. $sError = 'Argument 1 for Rapide_User::setGroups() must be an array';
  38. throw new Rapide_User_Exception($sError);
  39. }
  40.  
  41. $this->_oSession->groups = $aGroups;
  42. }
  43.  
  44. public function hasGroup($sGroup)
  45. {
  46. foreach($this->getGroups() as $iKey => $sUserGroup)
  47. {
  48. if($sUserGroup == $sGroup)
  49. return true;
  50. }
  51.  
  52. return false;
  53. }
  54.  
  55. public function isAuthenticated()
  56. {
  57. return $this->_oSession->authenticated;
  58. }
  59.  
  60. public function setAuthenticated($bAuthenticated = true)
  61. {
  62. $this->_oSession->authenticated = (bool)$bAuthenticated;
  63. }
  64.  
  65. public function __get($sParameter)
  66. {
  67. return $this->_oDataSession->$sParameter;
  68. }
  69.  
  70. public function __set($sParameters, $mValue)
  71. {
  72. $this->_oDataSession->$sParameter;
  73. }
  74.  
  75. public function __isset($sParameter)
  76. {
  77. return isset($this->_oDataSession->$sParameter);
  78. }
  79.  
  80. public function destroy()
  81. {
  82. $this->setGroups(array());
  83. $this->setAuthenticated(false);
  84.  
  85. $this->_oSession->close();
  86. $this->_oDataSession->close();
  87. }
  88. }
  89.  
  90. ?>


Kiedy programista chce zapisac dane w sesji, pisze:

  1. <?php
  2. $oUser = new Rapide_User;
  3.  
  4. $oUser->zmienna = 'wartosc';
  5. ?>


Ponadto klasa Rapide_User dostarcza metod dajacych mozliwosc kontrole bezpieczenstwa w aplikacji.
Po zalogowaniu sie nalezy zrobic:
  1. <?php
  2. $oUser->setAuthenticated();
  3. ?>


Pozniej w aby sprawdzic czy uzytkownik zostal zalogowany:

  1. <?php
  2. if($oUser->isAuthenticated()) {
  3. //cos
  4. }
  5. ?>


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.
Go to the top of the page
+Quote Post

Posty w temacie


Reply to this topicStart new topic
1 Użytkowników czyta ten temat (1 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Aktualny czas: 18.10.2025 - 00:40