[imp] IMP php database session problem

Joseph Lo jlo@pgicanada.com
Fri, 2 Aug 2002 16:41:51 -0700


Hi everyone!

I'm running into some problems right now with IMP.  When I had the php.ini
session.save_handler=file everything worked fine.  One problem that I did
have was that the session variables would not be detected, this was also a
similar problem which I had with my other php sites.

The problems with session was caused by a load balancing server.  Traffic is
directed from the load balancing server to two identical web servers.  The
previous session were not detected when users were directed to the opposing
web server which the session was not started on.

To solve this problem I decided to save my session in the database(mysql)
which sits in a seperate server which is connected to both webservers.  I
had to switched the session.savehandler = user in the php.ini file.  After
this my php sites worked fine.  Unfortunately, IMP no longer worked.  I'm
not too sure how to configure IMP to work with database sessions.  I've
tried to add my database session functions to the php files where sessions
are used which did not
work.  I've looked through previous posting and faq to try to figure this
out with little luck.

Any help, suggestions or reccomendations would be greatly appreciated.
Sorry for posting out all this code, but I've run flat out of ideas.

I'm running IMP v3.1  on php/4.1.2, apache/1.3.24 on debian linux
mod_ssl/2.8.7

Thanks in advance.  And thank you to those who responded to my emails.

Joseph Lo

These are my user defined functions:

$SESS_DBHOST = "xxxxxxx"            database server hostname */
$SESS_DBNAME = "xxxxxxx"           database name */
$SESS_DBUSER = "xxxxxxx"            database user */
$SESS_DBPASS = "xxxxxxx"             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 "<li>Can't connect to $SESS_DBHOST as $SESS_DBUSER";
  echo "<li>MySQL Error: ", mysql_error();
  die;
 }

 if (!mysql_select_db($SESS_DBNAME, $SESS_DBH)) {
  echo "<li>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 sessions WHERE sesskey = '$key' AND expiry > " .
time();
 $qid = mysql_query($qry, $SESS_DBH);

 if (list($value) = mysql_fetch_row($qid)) {
  return $value;
 }

 return " ";
}

function sess_write($key, $val) {
 global $SESS_DBH, $SESS_LIFE;

 $expiry = time() + $SESS_LIFE;
 $value = addslashes($val);

 $qry = "INSERT INTO sessions VALUES ('$key', $expiry, '$value')";
 $qid = mysql_query($qry, $SESS_DBH);

 if (! $qid) {
  $qry = "UPDATE 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 sessions WHERE sesskey = '$key'";
 $qid = mysql_query($qry, $SESS_DBH);

 return $qid;
}

function sess_gc($maxlifetime) {
 global $SESS_DBH;

 $qry = "DELETE FROM 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");