北京网站制作公司,丰富的网站建设经验,为企业提供精品网站制作服务!
服务热线:

织梦DedeCMS使用MySQL存储session值

织梦DedeCMS使用MySQL存储session值

<?php
/**
 * -----------------------------------
 * session
 * -----------------------------------
 * Author: 安徽宝讯信息技术有限公司
 * Creater: Hoogl
 * Date: 2020/11/3
 * Version: 1.0.0
 * -----------------------------------
 */

function sess_open($save_path, $session_name)
{
    return true;
}

function sess_close()
{
    return true;
}

function sess_read($key)
{
    global $dsql;
    if (empty($dsql)) {
        require_once(DEDEDATA.'/common.inc.php');
        if ( $GLOBALS['cfg_dbtype'] =='mysql' )
        {
            if ($GLOBALS['cfg_mysql_type'] == 'mysqli' && function_exists("mysqli_init") || !function_exists('mysql_connect'))
            {
                require_once(DEDEINC.'/dedesqli.class.php');
                $dsql = new DedeSqli(FALSE);
            } else {
                require_once(DEDEINC.'/dedesql.class.php');
                $dsql = new DedeSql(FALSE);
            }
        } else {
            require_once(DEDEINC.'/dedesqlite.class.php');
            $dsql = new DedeSqlite(FALSE);
        }
    }
    $res = $dsql->GetOne("Select value,expiry_time From `#@__session` where `key`='{$key}'");
    if ($res && $res['expiry_time'] >= time()) {
        return strval($res['value']);
    }
    return '';
}

function sess_write($key, $val)
{
    if (empty($val)) {
        return true;
    }

    global $dsql;

    if (empty($dsql)) {
        require_once(DEDEDATA.'/common.inc.php');
        if ( $GLOBALS['cfg_dbtype'] =='mysql' )
        {
            if ($GLOBALS['cfg_mysql_type'] == 'mysqli' && function_exists("mysqli_init") || !function_exists('mysql_connect'))
            {
                require_once(DEDEINC.'/dedesqli.class.php');
                $dsql = new DedeSqli(FALSE);
            } else {
                require_once(DEDEINC.'/dedesql.class.php');
                $dsql = new DedeSql(FALSE);
            }
        } else {
            require_once(DEDEINC.'/dedesqlite.class.php');
            $dsql = new DedeSqlite(FALSE);
        }
    }

    $max_life_time = intval(get_cfg_var("session.gc_maxlifetime"));
    $max_life_time = $max_life_time ?: 1800;
    $expiry_time   = time() + $max_life_time; //过期时间
    $value         = addslashes($val);
    $data          = unSerializeStr($val);
    $member_id     = empty($data['member_id']) ? 0 : intval($data['member_id']);
    $res           = $dsql->GetOne("Select `value` From `#@__session` where `key`='{$key}'");
    if (empty($res)) {
        $dbRes = $dsql->ExecuteNoneQuery("INSERT INTO `#@__session` (`key`,`value`,`member_id`,`expiry_time`)
               VALUES ('$key','$value',{$member_id},{$expiry_time}); ");
    } else {
        $dbRes = $dsql->ExecuteNoneQuery("update `#@__session` set value='" . $value . "',member_id=".$member_id.",expiry_time=" . $expiry_time . " where `key`='" . $key . "';");
    }

    return $dbRes;
}

function sess_destroy($key)
{
    global $dsql;
    if (empty($dsql)) {
        require_once(DEDEDATA.'/common.inc.php');
        if ( $GLOBALS['cfg_dbtype'] =='mysql' )
        {
            if ($GLOBALS['cfg_mysql_type'] == 'mysqli' && function_exists("mysqli_init") || !function_exists('mysql_connect'))
            {
                require_once(DEDEINC.'/dedesqli.class.php');
                $dsql = new DedeSqli(FALSE);
            } else {
                require_once(DEDEINC.'/dedesql.class.php');
                $dsql = new DedeSql(FALSE);
            }
        } else {
            require_once(DEDEINC.'/dedesqlite.class.php');
            $dsql = new DedeSqlite(FALSE);
        }
    }

    $dsql->ExecuteNoneQuery("delete from `#@__session` where `key`='" . $key . "';");

    return true;
}

function sess_gc($maxlifetime)
{
    global $dsql;
    if (empty($dsql)) {
        require_once(DEDEDATA.'/common.inc.php');
        if ( $GLOBALS['cfg_dbtype'] =='mysql' )
        {
            if ($GLOBALS['cfg_mysql_type'] == 'mysqli' && function_exists("mysqli_init") || !function_exists('mysql_connect'))
            {
                require_once(DEDEINC.'/dedesqli.class.php');
                $dsql = new DedeSqli(FALSE);
            } else {
                require_once(DEDEINC.'/dedesql.class.php');
                $dsql = new DedeSql(FALSE);
            }
        } else {
            require_once(DEDEINC.'/dedesqlite.class.php');
            $dsql = new DedeSqlite(FALSE);
        }
    }

    $dsql->ExecuteNoneQuery("delete from `#@__session` where `expiry_time` < " . time() . ";");

    return true;
}

/**
 * 解析session数据
 *
 * @param $sessionData
 * @return array
 */
function unSerializeStr($sessionData)
{
    $return_data = [];
    $offset      = 0;
    while ($offset < strlen($sessionData)) {
        $pos = strpos($sessionData, "|", $offset);
        if (!strstr(substr($sessionData, $offset), "|")) {
            return [];
        }
        $num                   = $pos - $offset;
        $varName               = substr($sessionData, $offset, $num);
        $offset                += $num + 1;
        $data                  = unserialize(substr($sessionData, $offset));
        $return_data[$varName] = $data;
        $offset                += strlen(serialize($data));
    }
    return $return_data;
}

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

if (!session_id()) {
    session_start();
}
其它相关文章:
专业网站制作公司 — 安徽宝讯信息技术有限公司 © 版权所有
京ICP备09005099号-7

在线
客服

在线客服服务时间:9:00-20:00

选择下列客服马上在线沟通:

客服
热线

133 9152 9507
7*24小时客服服务热线

关注
微信

官方微信
顶部