yeah, b/c i'm attempting something like this:
class session extends db_conn {
//constructor
function session() {
}
function start() {
session_module_name("user");
session_set_save_handler("$this->sess_open()","$this->sess_close()","$this->sess_read()","$this->sess_write()","$this->sess_destroy()","$this->sess_garb_coll()");
session_start();
}
//logs a session to log file in use of testing
//change log location after testing or destroy -- this script is not required in sessions
function sess_log($message) {
if($file = fopen("log.txt","a")) {
fwrite($file,date("Y-m-d H:i:s") . " " . $message ."\n");
fclose($file); }
return true;
}
//function to open a session --actually doesn't do anything, but needed by session_set_save_handler function to log a session opening
//delete logging after testing
function sess_open($sess_path,$sess_name) {
$this->sess_log("sess_open");
return true;
}
//function to close a session --actually doesn't do anything, but needed by session_set_save_handler function to log a session closing
//delete logging after testing
function sess_close() {
$this->sess_log("sess_close");
return true;
}
//workhorse #1 of the session handler -- this function looks for a previous session
function sess_read($sess_id) {
GLOBAL $SESS_TABLE;
$sql = "select * from " . $SESS_TABLE . " where sess_id ='" . $sess_id . "'";
$this->sess_log("sess_read");
//test connection to db -- need to add contingency if db is down to use files as sessions (future)
if(!($this->db_pconnect())) {
$this->sess_log("error opening database for sess_read operation.");
return false; }
//query db to find an existing session with passed parameter -- tests if query completed successfully
if(!($this->db_query($sql))) {
$this->sess_log("mysql error: " . $this->db_sql_error() . " with SQL: " . $sql);
return false; }
//if we find a return value for our query then, the user has already attained a session
if($this->db_num_rows()) {
$this->sess_log("mysql query returned " . $this->db_num_rows() . " rows.");
$row = $this->db_fetch_assoc();
//the next statements needs to be adjusted according to the returned values
$this->sess_log("the query returned " . $row["user_data"]);
return $row["user_data"]; }
//since user did not have a session return null
else {
$this->sess_log("sess_read found zero rows with SQL: " . $sql);
return(""); }
}
//workhorse #2 of the session handler -- write session information to the database
function sess_write($sess_id,$sess_data) {
GLOBAL $SESS_TABLE;
//SQL to update session information
$sql = "update " . $SESS_TABLE . " set user_data ='" . addslashes($sess_data) . "', last_update ='" . date("YmdHis", time()) . "'";
$this->sess_log("sess_write");
//test connection to db -- need to add contingency if db is down to use files as sessions (future)
if(!($this->db_pconnect())) {
$this->sess_log("error opening database for sess_write operation.");
return false; }
//if user is logged in, find the username with the sess_id
if(isset($PHP_AUTH_USER)) {
$sql .= " user_name ='" . addslashes($PHP_AUTH_USER) . "'"; }
//add conditional where statement to only update the session user
$sql .= " where sess_id ='" . $sess_id . "'";
//send SQL to db to update session variables in the database
if(!($this->db_query($sql))) {
$this->sess_log("sess_write error " . $this->db_sql_error() . " with SQL: " . $sql);
return false; }
//check to see if SQL was effective and update our log
if($this->db_num_affected_rows()) {
$this->sess_log("sess_write update affected " . $this->db_num_affected_rows() . " rows with SQL: " . $sql);
return true; }
//if SQL was not effective, change our SQL to INSERT instead of update
$this->sess_log("sess_write updated zero rows with SQL: " .$sql);
$sql = "insert " . $SESS_TABLE . " set user_data ='" . addslashes($sess_data) . "',sess_id ='" . $sess_id . "', last_update ='" . date("YmdHis", time()) . "'";
if(!($this->db_query($sql))) {
$this->sess_log("sess_write error " . $this->db_sql_error() . " with SQL: " . $sql);
return false; }
else {
$this->sess_log("sess_write inserted with SQL: " . $sql);
return true; }
}
//destroys a session
function sess_destroy($sess_id) {
GLOBAL $SESS_TABLE;
$this->sess_log("sess_destroy");
$sql = "delete " .$SESS_TABLE . " where sess_id ='" . $sess_id . "'";
if(!($this->db_pconnect())) {
$this->sess_log("sess_destroy select database error: " . $this->db_sql_error());
return(false); }
if($this->db_query($sql)) {
$this->sess_log("MySQL query delete worked.");
return true; }
else {
$this->sess_log("MySQL update error: " . $this->db_sql_error() . " with SQL: " . $sql);
return(false); }
}
function sess_garb_coll() {
GLOBAL $SESS_TABLE,$SESS_LIFE;
$this->sess_log("sess_garb_coll");
$sql = "delete " . $SESS_TABLE . " where last_update < '" . date("YmdHis", time() - $SESS_LIFE) . "'";
$this->sess_log("sess_garb_coll sql: " . $sql);
if(!($this->db_pconnect())) {
$this->sess_log("sess_garb_coll select database error: " . $this->db_sql_error());
return(false); }
if($this->db_query($sql)) {
$this->sess_log("sess_garb_coll deleted " . $this->db_affected_rows() . " rows.");
return true; }
else {
$this->sess_log("sess_garb_coll error: " . $this->db_sql_error() . " with SQL: " . $sql);
return false; }
}
}
++++++++++++++++++++++++++++++++++++++++++
and the dbconn....
++++++++++++++++++++++++++++++++++++++++++
class db_conn {
var $db_current_row="";
//constructor
function db_conn() {
}
//use this to establish a persistent connection--it does not close at the end of the script
function db_pconnect() {
GLOBAL $HOSTNAME,$USERNAME,$PASSWORD,$DATABASE;
if(($this->db_conn_id = @mysql_pconnect($HOSTNAME,$USERNAME,$PASSWORD)) && ($this->db_select_db($DATABASE))) {
return true; }
else return false;
}
//use this to establish a normal connection--the connection will expire with the script termination
function db_connect() {
GLOBAL $HOSTNAME,$USERNAME,$PASSWORD,$DATABASE;
if(($this->db_conn_id = @mysql_connect($HOSTNAME,$USERNAME,$PASSWORD)) && ($this->db_select_db($DATABASE))) {
return true; }
else return false;
}
//submit a query to mysql and returns the results resource identifier
function db_query($sql) {
if($this->db_result_id = @mysql_query($sql,$this->db_conn_id)){
return $this->db_result_id; }
else return false;
}
//a super-function to get next row of results
function db_fetch_next_result() {
if(($this->db_num_rows()) && (!($this->db_current_row > $this->db_num_rows()))) {
$this->db_results = @mysql_fetch_row($this->db_result_id);
$this->db_current_row++;
@mysql_data_seek($this->db_result_id,$this->db_current_row);
return $this->db_results; }
else return false;
}
//returns number of fields in preceding query result
function db_num_fields() {
return @mysql_num_fields($this->db_result_id);
}
//returns number of rows in preceding query result
function db_num_rows() {
return @mysql_num_rows($this->db_result_id);
}
//returns number of rows affected in preceding query
function db_num_affected_rows() {
return @mysql_affected_rows($this->db_result_id);
}
//access current row in a numbered array from returned mysql query
function db_fetch_num() {
$this->db_results_num = @mysql_fetch_row($this->db_result_id);
return $this->db_results;
}
//access current row in an associative array from returned mysql query
function db_fetch_assoc() {
$this->db_results_assoc = @mysql_fetch_array($this->db_result_id);
return $this->db_results;
}
//seek a row in the current mysql results
function db_row_seek($row) {
return @mysql_data_seek($this->db_result_id,$row);
}
//access last mysql error message
function db_sql_error() {
$this->err_msg = @mysql_error($this->db_conn_id);
return $this->err_msg;
}
//select new db -- returns true or false
function db_select_db($database) {
if($this->db_conn_id) {
return @mysql_select_db($database,$this->db_conn_id); }
}
//get last insert id from auto_increment field
function db_last_insertid() {
return @mysql_insert_id($this->db_conn_id);
}
//close your mysql connection explicitly
function db_close() {
return @mysql_close($this->db_conn_id);
}
//frees results of previous mysql query -- returns true or false
function db_free_result() {
return @mysql_free_result($this->db_result_id);
}
}
any ideas?