<?php
$SESS_LIFE = 1440;
function sess_open($save_path, $session_name) {
        sess_gc(0);
        return true;
} 

function sess_close() {
        return true;
} 

function sess_read($key) {
        global $DB, $db_prefix;
        $session = $DB->fetch_one_array("SELECT * FROM " . $db_prefix . "session WHERE sesskey = '".$key."' AND expiry > ".time());
        if (!empty($session)) {
                return $session['value'];
        } else {
                return "";
        } 
} 

function sess_write($key, $val) {
        global $DB, $db_prefix, $SESS_LIFE;
        $expiry = time() + $SESS_LIFE;
		$value = addslashes($val);
        $check_query = $DB->query("select count(*) as total from ".$db_prefix."session where sesskey = '" .$key."'");
		$check = $DB->fetch_array($check_query);
		if ($check['total'] > 0) {
			$query=$DB->query("UPDATE " .$db_prefix."session SET value = '" .($value)."', expiry = '" .$expiry."' WHERE sesskey = '" .$key."'");
		}else {
			$query = $DB->query("INSERT INTO " .$db_prefix."session (sesskey, expiry, value) VALUES ('".$key."','".$expiry."','".$value."')");
		}
		Return $query;
} 

function sess_destroy($key) {
        global $DB, $db_prefix;
        return $DB->query("DELETE FROM " . $db_prefix . "session WHERE sesskey='".$key."'");
} 

function sess_gc($maxlifetime) {
        global $DB, $db_prefix;
        $query = $DB->query("DELETE FROM " . $db_prefix . "session WHERE expiry <" . time() . "");
        return $DB->affected_rows();
} 

session_set_save_handler("sess_open",
        "sess_close",
        "sess_read",
        "sess_write",
        "sess_destroy",
        "sess_gc");

@session_start();

?>