php db sessions


/ Published in: PHP
Save to your folder(s)



Copy this code and paste it in your HTML
  1. // DB Managed Sessions using PHP and MySQL.
  2. // Written by: Greg Donald <[email protected]>
  3. // $Id: session.php 6 2005-12-26 01:22:57Z destiney $
  4.  
  5. // MySQL database connection parameters:
  6. $GLOBALS[ 'dbhost' ] = 'localhost';
  7. $GLOBALS[ 'dbuser' ] = 'root';
  8. $GLOBALS[ 'dbpasswd' ] = 'changeme';
  9. $GLOBALS[ 'dbname' ] = 'dbsessions';
  10.  
  11. // Session lifetime
  12. $GLOBALS[ 'online_expire' ] = 900;
  13.  
  14. // Default sessions table:
  15. $GLOBALS[ 'tb_sessions' ] = 'sessions';
  16.  
  17. // Use transparent sessions:
  18. ini_set( 'session.use_trans_sid', 1 );
  19.  
  20.  
  21. ///////////////////////////////////////////////////////////////////////
  22. // Below here should not require any changes
  23.  
  24. error_reporting( E_ALL );
  25.  
  26. $GLOBALS[ 'sdbh' ] = '';
  27.  
  28. function sess_open( $save_path, $session_name )
  29. {
  30. if (! $GLOBALS[ 'sdbh' ] = mysql_pconnect( $GLOBALS[ 'dbhost' ],
  31. $GLOBALS[ 'dbuser' ],
  32. $GLOBALS[ 'dbpasswd' ] ) ){
  33. }
  34.  
  35. if( !mysql_select_db( $GLOBALS[ 'dbname' ], $GLOBALS[ 'sdbh' ] ) )
  36. {
  37. }
  38.  
  39. return true;
  40. }
  41.  
  42. function sess_close()
  43. {
  44. return mysql_close( $GLOBALS[ 'sdbh' ] );
  45. }
  46.  
  47. function sess_read( $key )
  48. {
  49. $key = mysql_real_escape_string( $key );
  50.  
  51. $sql = "
  52. SELECT `data`
  53. FROM `" . $GLOBALS[ 'tb_sessions' ] . "`
  54. WHERE `id` = '$key'
  55. AND `expire` > UNIX_TIMESTAMP()
  56. ";
  57.  
  58. $query = mysql_query( $sql ) or die( mysql_error() );
  59.  
  60. if( mysql_num_rows( $query ) )
  61. {
  62. return mysql_result( $query, 0, 'data' );
  63. }
  64.  
  65. return '';
  66. }
  67.  
  68. function sess_write( $key, $val )
  69. {
  70. $key = mysql_real_escape_string( $key );
  71. $value = mysql_real_escape_string( $val );
  72.  
  73. $sql = "
  74. REPLACE INTO `" . $GLOBALS[ 'tb_sessions' ] . "` (
  75. `id`,
  76. `data`,
  77. `expire`
  78. ) VALUES (
  79. '$key',
  80. '$value',
  81. UNIX_TIMESTAMP() + " . $GLOBALS[ 'online_expire' ] . "
  82. )
  83. ";
  84.  
  85. return mysql_query( $sql ) or die( mysql_error() );
  86. }
  87.  
  88. function sess_destroy( $key )
  89. {
  90. $key = mysql_real_escape_string( $key );
  91.  
  92. $sql = "
  93. DELETE FROM `" . $GLOBALS[ 'tb_sessions' ] . "`
  94. WHERE `id` = '$key'
  95. ";
  96.  
  97. return mysql_query( $sql ) or die( mysql_error() );
  98. }
  99.  
  100. function sess_gc()
  101. {
  102. $sql = "
  103. DELETE FROM `" . $GLOBALS[ 'tb_sessions' ] . "`
  104. WHERE `expire` < UNIX_TIMESTAMP() - " . $GLOBALS[ 'online_expire' ] . "
  105. ";
  106.  
  107. $query = mysql_query( $sql ) or die( mysql_error() );
  108.  
  109. }
  110.  
  111. 'sess_close',
  112. 'sess_read',
  113. 'sess_write',
  114. 'sess_destroy',
  115. 'sess_gc' );
  116.  
  117.  
  118. $sn = session_name();
  119. $sid = session_id();

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.