. * * ------------------------------------------------------------------------ * DESCRIPTION: * ------------------------------------------------------------------------ * This library tells the PHP4 session handler to write to a MySQL database * instead of creating individual files for each session. * * Create a new database in MySQL called "".TBLPFX."Sessions" like so: * * CREATE TABLE ".TBLPFX."Sessions ( * sesskey char(32) not null, * expiry int(11) unsigned not null, * value text not null, * PRIMARY KEY (sesskey) * ); * * ------------------------------------------------------------------------ * INSTALLATION: * ------------------------------------------------------------------------ * Make sure you have MySQL support compiled into PHP4. Then copy this * script to a directory that is accessible by the rest of your PHP * scripts. * * ------------------------------------------------------------------------ * USAGE: * ------------------------------------------------------------------------ * Include this file in your scripts before you call session_start(), you * don't have to do anything special after that. */ //include_once ("globals.php"); global.php includes this file $SESS_DBHOST = HOST; /* database server hostname */ $SESS_DBNAME = DATABASE; /* database name */ $SESS_DBUSER = DBLOGIN; /* database user */ $SESS_DBPASS = DBPASSWORD; /* database password */ $SESS_DBH = ""; $SESS_LIFE = get_cfg_var("session.gc_maxlifetime"); function sess_open($save_path, $session_name) { global $SESS_DBHOST, $SESS_DBNAME, $SESS_DBUSER, $SESS_DBPASS, $SESS_DBH; if (! $SESS_DBH = mysql_pconnect($SESS_DBHOST, $SESS_DBUSER, $SESS_DBPASS)) { echo "
  • Can't connect to $SESS_DBHOST as $SESS_DBUSER"; echo "
  • MySQL Error: ", mysql_error(); die; } if (! mysql_select_db($SESS_DBNAME, $SESS_DBH)) { echo "
  • Unable to select database $SESS_DBNAME"; die; } return true; } function sess_close() { return true; } function sess_read($key) { global $SESS_DBH, $SESS_LIFE; $qry = "SELECT value FROM ".TBLPFX."Sessions WHERE sesskey = '$key' AND expiry > " . time(); $qid = mysql_query($qry, $SESS_DBH); if (list($value) = mysql_fetch_row($qid)) { return $value; } return false; } function sess_write($key, $val) { global $SESS_DBH, $SESS_LIFE; $expiry = time() + $SESS_LIFE; $value = addslashes($val); $qry = "INSERT INTO ".TBLPFX."Sessions VALUES ('$key', $expiry, '$value')"; $qid = mysql_query($qry, $SESS_DBH); if (! $qid) { $qry = "UPDATE ".TBLPFX."Sessions SET expiry = $expiry, value = '$value' WHERE sesskey = '$key' AND expiry > " . time(); $qid = mysql_query($qry, $SESS_DBH); } return $qid; } function sess_destroy($key) { global $SESS_DBH; $qry = "DELETE FROM ".TBLPFX."Sessions WHERE sesskey = '$key'"; $qid = mysql_query($qry, $SESS_DBH); return $qid; } function sess_gc($maxlifetime) { global $SESS_DBH; $qry = "DELETE FROM ".TBLPFX."Sessions WHERE expiry < " . time(); $qid = mysql_query($qry, $SESS_DBH); return mysql_affected_rows($SESS_DBH); } session_set_save_handler( "sess_open", "sess_close", "sess_read", "sess_write", "sess_destroy", "sess_gc"); ?> global.php now includes this file. // include_once $_SERVER['DOCUMENT_ROOT']."lib/global.php"; // $GLOBAL variables are used here for compatibility with the WSG Connect() function $dbhost = $GLOBALS['dbhost'] = HOST; $dbuser = $GLOBALS['dbuser'] = DBLOGIN; $dbpass = $GLOBALS['dbpass'] = DBPASSWORD; $db = $GLOBALS['db'] = DATABASE; $tblpfx = TBLPFX; $notificationemail = SYSADMIN; $urlbase = URLBASE; $css_styles = CSS_STYLES; $GLOBALS['WSGLIB_ERRORADDRESS'] = SYSADMIN; $GLOBALS['DBEDIT_ERRORADDRESS'] = SYSADMIN; $WSG_DOCUMENT_ROOT = WSG_DOC_ROOT; $SQL_CACHE = SQL_CACHE; include_once $_SERVER['DOCUMENT_ROOT']."/lib/wsg/lib/WSGlib.041beta/WSG.php"; ?>