Custom Session Handler

kenleycapps kenleycapps is offline Offline Sep 20th, 2004, 9:07 am |
0
Ever feel the default session handling is insecure? Or do you just want some originality to show off? Well, here you are. A custom, MySQL-based session handler. It supports base64_encoding and MySQL storage.

Note: I haven't tested this thing in months so I don't know how well it'll work : Good luck, hope you like it.

Configuration: All you gotta do is edit the MySQL configurations, the expire and GC probability (if you really want. its best left as is), and just include session.php in all your session-oriented scripts. =]

Please email me (kenleycapps@gmail.com) if you get ANY errors! Thanks.
Quick reply to this message  
PHP Syntax
  1. <?php
  2.  
  3. // config
  4. $m_host = ""; //MySQL Host
  5. $m_user = ""; //MySQL User
  6. $m_pass = ""; //MySQL Pass
  7. $m_db = ""; //MySQL Database
  8.  
  9. $session_expire = 600; // Session expire time, in seconds (minutes * 60 = seconds)
  10.  
  11. $gc_probability = 50; // Probability that the garbage collection function will be called. 50% chance by default
  12.  
  13. ini_set("session.gc_probability",$gc_probability);
  14.  
  15. /* Open function; Opens/starts session
  16.  
  17.   Opens a connection to the database and stays open until specifically closed
  18.   This function is called first and with each page load */
  19.  
  20. function open ($s,$n) // do not modify function parameters
  21. {
  22. global $session_connection, $m_host, $m_user, $m_pass, $m_db;
  23. $session_connection = mysql_pconnect($m_host,$m_user,$m_pass);
  24. mysql_select_db($m_db,$session_connection);
  25. return true;
  26. }
  27.  
  28. /* Read function; downloads data from repository to current session
  29.  
  30.   Queries the mysql database, unencrypts data, and returns it.
  31.   This function is called after 'open' with each page load. */
  32. function read ($id) // do not modify function parameters
  33. {
  34. global $session_connection,$session_read;
  35. $query = "SELECT data FROM sess_data WHERE id=\"{$id}\"";
  36. $res = mysql_query($query,$session_connection);
  37. if(mysql_num_rows($res) != 1) return ""; // must return string, not 'false'
  38. else
  39. {
  40. $session_read = mysql_fetch_assoc($res);
  41. $session_read["data"] = base64_decode($session_read["data"]);
  42. return $session_read["data"];
  43. }
  44. }
  45.  
  46.  
  47. /* Write function; uploads data from current session to repository
  48.  
  49.   Inserts/updates mysql records of current session. Called after 'read'
  50.   with each page load */
  51. function write ($id,$data) // do not modify function parameters
  52. {
  53. if(!$data) { return false; }
  54. global $session_connection, $session_read, $session_expire;
  55. $expire = time() + $session_expire;
  56. $data = mysql_real_escape_string(base64_encode($data));
  57. if($session_read) $query = "UPDATE sess_data SET data=\"{$data}\", expire=\"{$expire}\" WHERE id=\"{$id}\"";
  58. else $query = "INSERT INTO sess_data SET id=\"{$id}\", data=\"{$data}\"";
  59. mysql_query($query,$session_connection);
  60. return true;
  61. }
  62.  
  63. /*Close function; closes session
  64.  
  65.   closes mysql connection */
  66. function close ()
  67. {
  68. global $session_connection;
  69. mysql_close($session_connection);
  70. return true;
  71. }
  72.  
  73.  
  74. /* destroy function; deletes session data
  75.  
  76.   deletes records of current session. called ONLY when function 'session_destroy()'
  77.   is called */
  78. function destroy ($id) // do not modify function parameters
  79. {
  80. global $session_connection;
  81. $query = "DELETE FROM sess_data WHERE id=\"{$id}\"";
  82. mysql_query($query,$session_connection);
  83. return true;
  84. }
  85.  
  86. /* gc function; cleans expired sessions
  87.  
  88.   deletes all rows where expire < time(); called with a $gc_probability chance of executing */
  89. function gc ($expire)
  90. {
  91. global $session_connection;
  92. $query = "DELETE FROM sess_data WHERE expire < ".time();
  93. mysql_query($query,$session_connection);
  94. }
  95.  
  96.  
  97. // Set custom handlers
  98. session_set_save_handler ("open", "close", "read", "write", "destroy", "gc");
  99.  
  100. // Start session
  101. session_start();
  102. ?>
  103.  
  104. // MySQL Database Description
  105.  
  106. create table sess_data (
  107. id2 int not null auto_increment,
  108. id text not null,
  109. data text,
  110. expire int not null,
  111. primary key(id2)
  112. );

Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC