Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> Session handler pomoc
tabbi
post 17.11.2010, 20:27:24
Post #1





Grupa: Zarejestrowani
Postów: 150
Pomógł: 3
Dołączył: 30.10.2010

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


Witam,

po malu staram sie rozkminiac session handlera ktorego mam z phpclasses. Mam kilka pytań.

Po zalożaniu sesji w bazie mysql pojawia sie expire-date ta data sie jednak nie zmienia wiec czemu ma sluzyc ? (pobiera czas sesji i co z tym ?)
  1. ".(time() + $this->sessionLifetime)."


odwolanie do tego:

  1. // get session lifetime
  2. $this->sessionLifetime = ini_get("session.gc_maxlifetime");


Potem nastepnie mama:

session_set_save_handler w tym read write open closed itp. Jak do tych funkcji odwolac sie z poziomu strony ?

bo dzieki funkcji read($session_id) moge wyczytac dane ktora mam w session-data czyli dane ktore zapisze $_SESSION['data']= 'x' np.

chciałbym bardziej zrozumieć dzialanie tego typu obslugi sesji.

Nie wiem jak je sprawdzac po stronie klienta bo w login.php tworze obiekt sesji zapisuje do niego dane ale np. na stronie zalogowany.php nie wiem jak do nich sie odwolac ?

SESSIOn handler:

  1. <?php
  2.  
  3.  
  4.  
  5. require_once ("class_connect_db.php");
  6.  
  7. class dbSession
  8. {
  9.  
  10.  
  11. function dbSession($gc_maxlifetime = "", $gc_probability = "", $gc_divisor = "")
  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.  
  53. }
  54.  
  55. /**
  56.   * Deletes all data related to the session
  57.   *
  58.   * @return void
  59.   */
  60. function stop()
  61. {
  62. $this->regenerate_id();
  63. }
  64.  
  65. /**
  66.   * Regenerates the session id.
  67.   *
  68.   * <b>Call this method whenever you do a privilege change!</b>
  69.   *
  70.   * @return void
  71.   */
  72. function regenerate_id()
  73. {
  74.  
  75. // saves the old session's id
  76. $oldSessionID = session_id();
  77.  
  78. // regenerates the id
  79. // this function will create a new session, with a new id and containing the data from the old session
  80. // but will not delete the old session
  81.  
  82. // because the session_regenerate_id() function does not delete the old session,
  83. // we have to delete it manually
  84. $this->destroy($oldSessionID);
  85.  
  86. }
  87.  
  88. /**
  89.   * Get the number of online users
  90.   *
  91.   * This is not 100% accurate. It depends on how often the garbage collector is run
  92.   *
  93.   * @return integer approximate number of users currently online
  94.   */
  95. function get_users_online()
  96. {
  97.  
  98. // counts the rows from the database
  99. SELECT
  100. COUNT(session_id) as count
  101. FROM session_data
  102. "));
  103.  
  104. // return the number of found rows
  105. return $result["count"];
  106.  
  107. }
  108.  
  109. /**
  110.   * Custom open() function
  111.   *
  112.   * @access private
  113.   */
  114. function open($save_path, $session_name)
  115. {
  116.  
  117. return true;
  118.  
  119. }
  120.  
  121. /**
  122.   * Custom close() function
  123.   *
  124.   * @access private
  125.   */
  126. function close()
  127. {
  128. return true;
  129. }
  130.  
  131. /**
  132.   * Custom read() function
  133.   *
  134.   * @access private
  135.   */
  136. function read($session_id)
  137. {
  138.  
  139. // reads session data associated with the session id
  140. // but only if the HTTP_USER_AGENT is the same as the one who had previously written to this session
  141. // and if session has not expired
  142. $result = mysql_query("
  143. SELECT
  144. session_data
  145. FROM
  146. session_data
  147. WHERE
  148. session_id = '".$session_id."' AND
  149. http_user_agent = '".$_SERVER["HTTP_USER_AGENT"]."' AND
  150. session_expire > '".time()."'
  151. ");
  152.  
  153. // if anything was found
  154. if (is_resource($result) && mysql_num_rows($result) > 0) {
  155.  
  156. // return found data
  157. $fields = mysql_fetch_assoc($result);
  158. // don't bother with the unserialization - PHP handles this automatically
  159. return $fields["session_data"];
  160.  
  161. }
  162.  
  163. // if there was an error return an empty string - this HAS to be an empty string
  164. return "";
  165.  
  166. }
  167.  
  168. /**
  169.   * Custom write() function
  170.   *
  171.   * @access private
  172.   */
  173. function write($session_id, $session_data)
  174. {
  175.  
  176. // first checks if there is a session with this id
  177. $result = mysql_query("
  178. SELECT
  179. *
  180. FROM
  181. session_data
  182. WHERE
  183. session_id = '".$session_id."'
  184. ");
  185.  
  186. // if there is
  187. if (@mysql_num_rows($result) > 0) {
  188.  
  189. // update the existing session's data
  190. // and set new expiry time
  191. $result = mysql_query("
  192. UPDATE
  193. session_data
  194. SET
  195. session_data = '".$session_data."',
  196. session_expire = '".(time() + $this->sessionLifetime)."'
  197. WHERE
  198. session_id = '".$session_id."'
  199. ");
  200.  
  201. // if anything happened
  202.  
  203. // return true
  204. return true;
  205.  
  206. }
  207.  
  208. // if this session id is not in the database
  209. } else {
  210.  
  211. // insert a new record
  212. $result = mysql_query("
  213. INSERT INTO
  214. session_data
  215. (
  216. session_id,
  217. http_user_agent,
  218. session_data,
  219. session_expire
  220. )
  221. VALUES
  222. (
  223. '".$session_id."',
  224. '".$_SERVER["HTTP_USER_AGENT"]."',
  225. '".$session_data."',
  226. '".(time() + $this->sessionLifetime)."'
  227. )
  228. ");
  229.  
  230. // if anything happened
  231.  
  232. // return an empty string
  233. return "";
  234.  
  235. }
  236.  
  237. }
  238.  
  239. // if something went wrong, return false
  240. return false;
  241.  
  242. }
  243.  
  244. /**
  245.   * Custom destroy() function
  246.   *
  247.   * @access private
  248.   */
  249. function destroy($session_id)
  250. {
  251.  
  252. // deletes the current session id from the database
  253. $result = mysql_query("
  254. DELETE FROM
  255. session_data
  256. WHERE
  257. session_id = '".$session_id."'
  258. ");
  259.  
  260. // if anything happened
  261.  
  262. // return true
  263. return true;
  264.  
  265. }
  266.  
  267. // if something went wrong, return false
  268. return false;
  269.  
  270. }
  271.  
  272. /**
  273.   * Custom gc() function (garbage collector)
  274.   *
  275.   * @access private
  276.   */
  277. function gc($maxlifetime)
  278. {
  279.  
  280. // it deletes expired sessions from database
  281. $result = mysql_query("
  282. DELETE FROM
  283. session_data
  284. WHERE
  285. session_expire < '".(time() - $maxlifetime)."'
  286. ");
  287.  
  288. }
  289.  
  290. }
  291. ?>


Ten post edytował tabbi 17.11.2010, 20:28:10
Go to the top of the page
+Quote Post

Posty w temacie
- tabbi   Session handler pomoc   17.11.2010, 20:27:24
- - mathijas   Po kolei :-). Co do czasu sesji - jest to data, c...   23.11.2010, 09:17:39


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 Wersja Lo-Fi Aktualny czas: 23.06.2025 - 05:34