Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: Session handler
Forum PHP.pl > Forum > PHP
tabbi
Stworzylem klase Session

i w niej metody przykladowe:

  1. function Session($gc_maxlifetime = "", $gc_probability = "", $gc_divisor = "",$_param=array())
  2. {
  3. if (!is_array($_param)) {
  4. $this->_handleErrors();
  5. } else {
  6.  
  7. $this->_db['db_host'] = $_param['db_host'];
  8. $this->_db['db_name'] = $_param['db_name'];
  9. $this->_db['db_user'] = $_param['db_user'];
  10. $this->_db['db_pass'] = $_param['db_pass'];
  11. $this->_db['db_name2'] = "sessions";
  12. }
  13.  
  14. // if $gc_maxlifetime is specified and is an integer number
  15. if ($gc_maxlifetime != "" && is_integer($gc_maxlifetime)) {
  16.  
  17. // set the new value
  18. @ini_set('session.gc_maxlifetime', $gc_maxlifetime);
  19.  
  20. }
  21.  
  22. // if $gc_probability is specified and is an integer number
  23. if ($gc_probability != "" && is_integer($gc_probability)) {
  24.  
  25. // set the new value
  26. @ini_set('session.gc_probability', $gc_probability);
  27.  
  28. }
  29.  
  30. // if $gc_divisor is specified and is an integer number
  31. if ($gc_divisor != "" && is_integer($gc_divisor)) {
  32.  
  33. // set the new value
  34. @ini_set('session.gc_divisor', $gc_divisor);
  35.  
  36. }
  37.  
  38. // get session lifetime
  39. $this->sessionLifetime = ini_get("session.gc_maxlifetime");
  40.  
  41. // register the new handler
  42. array(&$this, 'open'),
  43. array(&$this, 'close'),
  44. array(&$this, 'read'),
  45. array(&$this, 'write'),
  46. array(&$this, 'destroy'),
  47. array(&$this, 'gc')
  48. );
  49. register_shutdown_function('session_write_close');
  50.  
  51. // start the session
  52. $this->_sess_name = session_name();
  53. $this->_sess_id = session_id();
  54.  
  55. }
  56.  
  57.  
  58. public function open($save_path, $session_name)
  59. {
  60.  
  61. try{
  62. $this->_dbh = new PDO('mysql:dbname='.$this->_db['db_name'].';host='.$this->_db['db_host'], $this->_db['db_user'], $this->_db['db_pass']);
  63.  
  64. echo 'asd';
  65. }
  66. catch(PDOException $e){
  67. echo $e->getMessage();
  68. }
  69. return true;
  70. }
  71.  
  72. function read($session_id)
  73. {
  74.  
  75. // reads session data associated with the session id
  76. // but only if the HTTP_USER_AGENT is the same as the one who had previously written to this session
  77. // and if session has not expired
  78. foreach($this->_dbh->query("
  79. SELECT
  80. session_data
  81. FROM
  82. session_data
  83. WHERE
  84. session_id = '".$session_id."' AND
  85. http_user_agent = '".$_SERVER["HTTP_USER_AGENT"]."' AND
  86. session_expire > '".time()."'
  87. ") as $row);
  88.  
  89. // if anything was found
  90. if ($row > 0) {
  91.  
  92. // return found data
  93.  
  94. return $row["session_data"];
  95.  
  96. }


NIe wiem czemu nie łączy mi z baza danych oraz czemu wypisuje ze nie rozpoznaje zapytania query

SQLSTATE[28000] [1045] Access denied for user 'ODBC'@'localhost' (using password: NO)
Fatal error: Call to a member function query() on a non-object in C:\xampp\htdocs\pobieralnia\user\include\class_session.php on line 166


Dane do mysql dobre bo wcześniej się łączyłem i łączyło mi przez PDO
skoro select w pdo wywoluje sie za pomoca query. ?
kbsucha
Mam nadzieje ze to nie jest listing 1 do 1 z twojego pliku class_session.php bo jak tak to masz kilka błędów. Jak nie jest to zrób print_r($this->_db) na początku metody open, zobacz czy dane są prawidłowe i czy się w ogóle wyświetla. Jak nie to przerzuć łączenie z PDO do konstruktora klasy, bo pewnie nie wywołuje ci się metoda open.
To jest wersja lo-fi głównej zawartości. Aby zobaczyć pełną wersję z większą zawartością, obrazkami i formatowaniem proszę kliknij tutaj.
Invision Power Board © 2001-2025 Invision Power Services, Inc.