0

I created a class to save the session data to a database. it is not saving to the database and there are no errors. Any idea why? here is the class.

 <?php

class Session {


public function __construct(){
    //set handle to override SESSION
    $session_id = session_id();
    $session_name = session_name();
    $session_path = session_save_path();
    echo "in class";
}

function open(){
    include("includes/globals.php");
    global $_sess_db;
echo "in open";
    if(mysql_connect('127.0.0.1','root','') or die(mysql_error())){
        return mysql_select_db($CallPro_dbname, $_sess_db);
        echo "connected";
    }
    return FALSE;
}

function close(){
    global $_sess_db;
    return mysql_close($_sess_db);
}

function read($session_id){
    mysql_connect('127.0.0.1','root','');
            $record;
    $sql = "select * from callprodev.session_data where agentid = '".$_SESSION['agentid']."'";
    $result = mysql_query($sql,$_sess_db)or die(mysql_error());
    $record = mysql_fetch_assoc($result)or die(mysql_error());
    echo "read";

}
function write($session_id){
    mysql_connect('127.0.0.1','root','');
            $access = time();
    $access = mysql_real_escape_string($access);
    $sql = "REPLACE
    INTO    callprodev.session_data
    VALUES  (
    '{$_SESSION['agentid']}',
    '{$_SESSION['extension']}',
    '{$_SESSION['page']}',
    '{$_SESSION['vduser']}',
    '{$_SESSION['vdpass']}',
    '{$_SESSION['action_main_plugin']}',
    '{$_SESSION['caller_phone']}',
    '{$_SESSION['campaignid']}',
    '{$_SESSION['uniqueid']}',
    '{$_SESSION['dialed']}',
    '{$_SESSION['rurl']}',
    '{$_SESSION['reference_id']}',
    '{$_SESSION['contactid']}',
    '{$_SESSION['groupid']}',
    '{$_SESSION['script_info_plugin']}',
    '{$_SESSION['flags']}',
    '{$_SESSION['dispositioned']}'
    )";
    echo "write";
    return mysql_query($sql, $_sess_db)or die(mysql_error());
}
function destroy($session_id){
    mysql_connect('127.0.0.1','root','');
            $sql = "delete from callprodev.session_data where id = '{$_SESSION['agentid']}'";
            echo "destroy";
    return mysql_query($sql, $_sess_db)or die(mysql_error());
}
function gc($max){

}

}

$session_record = new Session();

session_set_save_handler(
    array($session_record, "open"),
    array($session_record, "close"),
    array($session_record, "read"),
    array($session_record, "write"),
    array($session_record, "destroy"),
    array($session_record, "gc"));

register_shutdown_function('session_write_close');


session_start();

The session is included in an init.php file. it is called like this.

include("classes/Session.php");


$session_record                     = new Session();

session_set_save_handler(
    array($session_record, "open"),
    array($session_record, "close"),
    array($session_record, "read"),
    array($session_record, "write"),
    array($session_record, "destroy"),
    array($session_record, "gc"));

session_start();
Adam Mills
  • 11
  • 1
  • 1
  • 6
  • What aspect of it is not working? That is a lot of code, can you narrow down the problem? Also, you will need to provide some means of garbage collection. Unless you don't plan to provide a way for sessions to expire after the specific max lifetime. – Mike Brant Jul 05 '13 at 17:36
  • it is not saving to the database. it is like it is not using the functions in the class. – Adam Mills Jul 05 '13 at 17:37
  • So what errors are you seeing? I not that you rely on the fragile approach of using a `global` declaration for the session database rather than passing a DB connection to the class. Have you verified this connection exists? If so are you getting any specific MySQL errors? I don't see any issue with the way you are setting up the session_set_save_handler. – Mike Brant Jul 05 '13 at 17:40
  • you have some `echo` in your code would you please paste the out put of your code too! – Boynux Jul 05 '13 at 17:40
  • "not working" is NOT USEFUL. have you done any debugging yourself? verified that your handlers are actually being called? checked for error messages? And of course, you're using the deprecated and obsolete mysql_*() functions, so even if this code was working right now, it'll most likely be NOT working anymore soon enough. – Marc B Jul 05 '13 at 17:41
  • Another thing I might point out as useful. If you are using PHP >= 5.4, you might simply want to extend and override the default SessionHandler object, and simply pass your instantiated child object to `session_set_save_handler()` – Mike Brant Jul 05 '13 at 17:44
  • @AdamMills I also just noticed that in your class constructor you are trying to read `session_id` which would not be available at the time of instantiation and `session_save_path` which has no meaning in the context of a database-backed session sure I am not sure what value these add. – Mike Brant Jul 05 '13 at 17:49
  • it is not outputing anything from the echo. no error messages, are being thrown either. – Adam Mills Jul 05 '13 at 18:00

1 Answers1

0

try

session_set_save_handler(
    array(&$session_record, "open"),
    array(&$session_record, "close"),
    array(&$session_record, "read"),
    array(&$session_record, "write"),
    array(&$session_record, "destroy"),
    array(&$session_record, "gc"));
id614515
  • 187
  • 1
  • 4