Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> My SessionHandler - problem z odczytem danych
404
post
Post #1





Grupa: Zarejestrowani
Postów: 226
Pomógł: 25
Dołączył: 22.05.2011

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


Witam!
  1. class MySessionHandler implements SessionHandlerInterface {
  2. public function open($save_path, $name) {
  3. //echo "SessionHandler::open<br />";
  4. return true;
  5. }
  6.  
  7. public function close() {
  8. //echo "SessionHandler::close<br />";
  9. return true;
  10. }
  11.  
  12. public function read($id) {
  13. echo "SessionHandler::read<br />";
  14. echo "id = $id<br />";
  15.  
  16. return ($data = DB::get_row('SELECT * FROM `session` WHERE `session_identifier` = ? LIMIT 1', array($id))) !== false ? serialize($data) : '';
  17. }
  18.  
  19. public function write($id, $data) {
  20. echo "SessionHandler::write<br />";
  21. $data = unserialize($data);
  22. if(($session = DB::get_row('SELECT * FROM `session` WHERE `session_identifier` = ?', array($id))) !== false) {
  23. echo 'update<br />';
  24.  
  25. return DB::update('session', array('session_update_time' => time()), 'session_identifier = \''. $id .'\'') !== false;
  26. } else {
  27. echo 'insert<br />';
  28.  
  29. $time = time();
  30. $session_data = array(
  31. 'session_identifier' => $id,
  32. 'session_start_time' => $time,
  33. 'session_update_time' => $time,
  34. 'user_id' => isset($data['user_id']) ? $data['user_id'] : SESSION_GUEST,
  35. 'user_data' => NULL,
  36. 'user_ip' => get_ip(),
  37. 'user_host' => get_host(),
  38. 'user_agent' => get_agent()
  39. );
  40. //return DB::insert(session', $session_data) !== false;
  41. }
  42.  
  43. return true;
  44. }
  45.  
  46. public function destroy($id) {
  47. echo "SessionHandler::destroy<br />";
  48.  
  49. return DB::query('DELETE FROM `session` WHERE `session_identifier` = ? LIMIT 1', array($id)) !== false;
  50. }
  51.  
  52. public function gc($max_lifetime) {
  53. echo "SessionHandler::gc<br />";
  54.  
  55. $time = time();
  56. return DB::query('DELETE FROM `session` WHERE (? - `session_update_time`) > ?', array($time, $time + $max_lifetime)) !== false;
  57. }
  58. }

Jakieś 6 godzin siedzę, patrzę w ten monitor i... nic. Czy naprawdę nie ma funkcji, która "przerobi" mi dane w metodzie SessionHandler::read na takie, które akceptuje sesja? Przy powyższym kodzie dane sesji po prostu przepadają ponieważ przy przekazaniu ich do $_SESSION wykryty zostaje niepoprawny format (tak podejrzewam). Przekopałem naprawdę dużo stron i jedyna sensowna metoda jaką znalazłem to napisanie własnego modułu i skompilowanie go z PHP ^^

Ten post edytował 404 13.09.2012, 22:04:33
Go to the top of the page
+Quote Post
 
Start new topic
Odpowiedzi (1 - 7)
Crozin
post
Post #2





Grupa: Zarejestrowani
Postów: 6 476
Pomógł: 1306
Dołączył: 6.08.2006
Skąd: Kraków

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


1. Dane powinieneś serializować przy zapisie i deserializować przy odczycie. Teraz robisz na odwrót.
2. Za takie potworki jak wyrażenie przy return w metodzie read ktoś powinien Ci palce połamać... (IMG:style_emoticons/default/wink.gif)
Go to the top of the page
+Quote Post
404
post
Post #3





Grupa: Zarejestrowani
Postów: 226
Pomógł: 25
Dołączył: 22.05.2011

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


Nie wydaje mi się. Właśnie problem w tym, że SessionHandlerInterface::read ma zwrócić zakodowany string, który potem zostanie przekazany do $_SESSION. Odczytując dane sesji z bazy muszę więc je zakodować. serialize i unserialize to nie te funkcje i w tym jest problem.
Cytat
Cytat
abstract public string SessionHandlerInterface::read ( string $session_id )

Reads the session data from the session storage, and returns the results. Called right after the session starts or when session_start() is called. Please note that before this method is called SessionHandlerInterface::open() is invoked.

This method is called by PHP itself when the session is started. This method should retrieve the session data from storage by the session ID provided. The string returned by this method must be in the same serialized format as when originally passed to the SessionHandlerInterface::write() If the record was not found, return an empty string.

The data returned by this method will be decoded internally by PHP using the unserialization method specified in session.serialize_handler. The resultig data will be used to populate the $_SESSION superglobal.

Note that the serialization scheme is not the same as unserialize() and can be accessed by session_decode().
Go to the top of the page
+Quote Post
Crozin
post
Post #4





Grupa: Zarejestrowani
Postów: 6 476
Pomógł: 1306
Dołączył: 6.08.2006
Skąd: Kraków

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


Teraz zwróć uwagę na ostatni akapit z zacytowanego przez Ciebie fragmentu manuala. (IMG:style_emoticons/default/wink.gif)
Go to the top of the page
+Quote Post
404
post
Post #5





Grupa: Zarejestrowani
Postów: 226
Pomógł: 25
Dołączył: 22.05.2011

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


Ok, w takim razie dlaczego jak robię tak:
  1. public function read($id) {
  2. if(($data = DB::get_row('SELECT * FROM `hb_session` WHERE `session_identifier` = ? LIMIT 1', array($id))) !== false){
  3. return $data;
  4. }
  5. return '';
  6. }

...to $_SESSION jest pusta? (IMG:style_emoticons/default/smile.gif)

Ten post edytował 404 13.09.2012, 21:44:52
Go to the top of the page
+Quote Post
Crozin
post
Post #6





Grupa: Zarejestrowani
Postów: 6 476
Pomógł: 1306
Dołączył: 6.08.2006
Skąd: Kraków

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


To jest ten moment, w którym odpalasz debugger i sprawdzasz czy na każdym etapie w każdej zmiennej jest to co być powinno. Być może z jakiegoś powodu dane nie są pobierane z bazy danych? Albo mają zły format.

Twoja metoda read($id) zwraca w tej chwili całą tablicę z bazy danych, a powinna zwracać jedynie zawartość sesji, tj. kolumnę session_data.

PS. Czy w bazie danych znajdują się odpowiednie dane?

Ten post edytował Crozin 14.09.2012, 11:44:58
Go to the top of the page
+Quote Post
404
post
Post #7





Grupa: Zarejestrowani
Postów: 226
Pomógł: 25
Dołączył: 22.05.2011

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


Widzisz, bo problem w tym, że ja chcę mieć dostęp nie tylko do "session_data", ale też np. do id usera, czasu rozpoczęcia sesji itd. w $_SESSION. Czyli podsumowując, szukam funkcji, która mi przerobi array na session_data ^^
Go to the top of the page
+Quote Post
Crozin
post
Post #8





Grupa: Zarejestrowani
Postów: 6 476
Pomógł: 1306
Dołączył: 6.08.2006
Skąd: Kraków

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


1. W konstruktorze MySessionHandler przekaż obiekt, który wypełnisz sobie metadanymi dot. sesji.
2. W read() uzupełnij ten obiekt pobranymi danymi, a zwróć jedynie dane z session_data, którymi PHP napełni tablicę $_SESSION.

Ewentualnie zostaw w ogóle wbudowany mechanizm sesji w spokoju i zrób swój od podstaw.
Go to the top of the page
+Quote Post

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

 



RSS Aktualny czas: 16.09.2025 - 02:41