Location: PHPKode > scripts > XS PHP Library > xs-php-library/inc/sgdbsql.inc.php
<?php
//
// +--------------------------------------------------------------------------+
// |                                                                          |
// |                   XS PHP Library  Generic Classes Library                |
// |                                                                          |
// |                   Copyright (c) 2001-2002 XSPHPLib Group.                |
// |                                                                          |
// +--------------------------------------------------------------------------+
// |                                                                          |
// | Distributed under the terms of the GNU Lesser General Public License as  |
// | published by the Free Software Foundation version 2.1                    |
// | See the GNU Lesser General Public License for more details. You should   |
// | have received a copy of the GNU Lesser General Public License along with |
// | this package; if not, write to the Free Software Foundation, Inc.,       |
// | 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.                 |
// |                                                                          |
// +--------------------------------------------------------------------------+
// |                                                                          |
// | Authors: Robert Bala <hide@address.com>                                   |
// |                                                                          |
// +--------------------------------------------------------------------------+
//
// $Id: sgdbsql.inc.php,v 1.2 2002/11/28 09:50:30 rbala Exp $

/**
 * SQL database session storage cointainer class.
 *
 * Used for store session data within a SQL database. The file with appropirate
 * SQL databse class should be included before this one, otherwise it won't be
 * possible to use this storage container. Note - only one storage container is
 * allowed to handle session data. Before use it the following table should be
 * created in database:
 *
 * CREATE TABLE sessions (
 *   session_id varchar(50) NOT NULL default '',
 *   data text NOT NULL,
 *   update_time timestamp(14) NOT NULL,
 *   PRIMARY KEY  (session_id),
 *   KEY update_time (update_time)
 * );
 *
 * @author Robert Bala <hide@address.com>
 * @access public
 * @package sess
 * @version $Id: sgdbsql.inc.php,v 1.2 2002/11/28 09:50:30 rbala Exp $
 */
class Storage extends Object {

    /**
     * Storage class constructor.
     *
     * Creates the new instance of Storage class.
     * Note - this storage container is slower then that which comes from the
     * PHP distribution.
     *
     * @access public
     * @return void
     */
    function Storage() {
        Object::Object();
    }
    
    /**
     * Opens the session storage container.
     *
     * Always returns true. Called when the session is initialized. It is not
     * used in this storage container.
     *
     * @author Robert Bala <hide@address.com>
     * @access public
     * @param string $path the path to which data is saved.
     * @param string $name the current session name.
     * @return boolean
     */
    function open($path, $name) {
        return true;
    }

    /**
     * Closes the session storage container.
     *
     * Always returns true. Called when the session is going down. It is not
     * used in this storage container.
     *
     * @author Robert Bala <hide@address.com>
     * @access public
     * @return boolean
     */
    function close() {
        return true;
    }

    /**
     * Reads data from the session storage container.
     *
     * Returns current session data on success or empty string on any kind of
     * failure. Called just after the session is opened. Reads data from SQL
     * database.
     *
     * @author Robert Bala <hide@address.com>
     * @access public
     * @param string $id the current session id.
     * @return mixed
     */
    function read($id) {
        global $storage_path, $storage_name;
        $storage = new DBQuery("SELECT data FROM sessions WHERE sid='{id}'");
        if (is_object($storage)) {
            $storage->setParam('id', $id);
            if (($storage->open() == true) && ($storage->rowCount() != 0)) {
                $result = $storage->fetchRow();
                $storage->close();
                if (!object_isError($result)) {
                    return $result['data'];
                }
            }
        }
        return '';
    }

    /**
     * Writes data to the session storage container.
     *
     * Returns true on success or false on any kind of failure. Called when
     * there is the need to save session data. Writes data to SQL database.
     *
     * @author Robert Bala <hide@address.com>
     * @access public
     * @param string $id the current session id.
     * @param string $data the current session data.
     * @return boolean
     */
    function write($id, $data) {
        $storage = new DBQuery('');
        if (is_object($storage)) {
            $sqldate = date("YmdHis", time());
            $storage->setParam('id', $id);
            $storage->setParam('data', $data);
            $storage->setParam('sqldate', $sqldate);
            $storage->setQuery("UPDATE sessions SET data='{data}', update_time={sqldate} WHERE sid='{id}'");
            if ($storage->execute() == 1) {
                return true;
            }
            $storage->setQuery("INSERT INTO sessions VALUES ('{id}', '{data}', {sqldate})");
            if ($storage->execute() == 1) {
                return true;
            }

        }
        return false;
    }

    /**
     * Destroys all data registered to a session from storage container.
     *
     * Returns true on success or false on any kind of failure.
     *
     * @author Robert Bala <hide@address.com>
     * @access public
     * @param string $id the current session id.
     * @return boolean
     */
    function destroy($id)  {
        $storage = new DBQuery("DELETE FROM sessions WHERE sid='{id}'");
        if (is_object($storage)) {
            $storage->setParam('id', $id);
            if ($storage->execute() == 1) {
                return true;
            }
        }
        return false;

    }

    /**
     * Garbage collection for storage container.
     *
     * Returns true on success or false on any kind of failure. Removes inactive
     * session from storage container. Called just after the session is opened.
     *
     * @author Robert Bala <hide@address.com>
     * @access public
     * @param string $maxlifetime the maximum life time for inactive sessions.
     * @return boolean
     */
    function gc($maxlifetime) {
        $storage = new DBQuery("DELETE FROM sessions WHERE update_time < {sqldate}");
        if (is_object($storage)) {
            $sqldate = date("YmdHis", time() - $maxlifetime);
            $storage->setParam('sqldate', $sqldate);
            if (!object_isError($storage->execute())) {
                return true;
            }
        }
        return false;
    }
    
}

?>
Return current item: XS PHP Library