search
Homephp教程PHP源码将ecshop中的session机制重写,从DB移植到Memcache中去


addServer('127.0.0.1', 11211);
        $this->cls_session($m, $session_name, $session_id);
    }

    function cls_session(&$db, $session_name = 'ECS_ID', $session_id = '')
    {
        $GLOBALS['_SESSION'] = array();

        if (!empty($GLOBALS['cookie_path']))
        {
            $this->session_cookie_path = $GLOBALS['cookie_path'];
        }
        else
        {
            $this->session_cookie_path = '/';
        }

        if (!empty($GLOBALS['cookie_domain']))
        {
            $this->session_cookie_domain = $GLOBALS['cookie_domain'];
        }
        else
        {
            $this->session_cookie_domain = '';
        }

        if (!empty($GLOBALS['cookie_secure']))
        {
            $this->session_cookie_secure = $GLOBALS['cookie_secure'];
        }
        else
        {
            $this->session_cookie_secure = false;
        }
     
        $this->session_name       = $session_name;

        $this->db  = &$db;
        $this->_ip = real_ip();

        if ($session_id == '' && !empty($_COOKIE[$this->session_name]))
        {
            $this->session_id = $_COOKIE[$this->session_name];
        }
        else
        {
            $this->session_id = $session_id;
        }

        if ($this->session_id)
        {
            $tmp_session_id = substr($this->session_id, 0, 32);
            if ($this->gen_session_key($tmp_session_id) == substr($this->session_id, 32))
            {
                $this->session_id = $tmp_session_id;
            }
            else
            {
                $this->session_id = '';
            }
        }

        $this->_time = time();

        if ($this->session_id)
        {
            $this->load_session();
        }
        else
        {
            $this->gen_session_id();
            setcookie($this->session_name, $this->session_id . $this->gen_session_key($this->session_id), 0, $this->session_cookie_path, $this->session_cookie_domain, $this->session_cookie_secure);
        }
        register_shutdown_function(array(&$this, 'close_session'));
    }

    function gen_session_id()
    {
        $this->session_id = md5(uniqid(mt_rand(), true));

        return $this->insert_session();
    }

    function gen_session_key($session_id)
    {
        static $ip = '';

        if ($ip == '')
        {
            $ip = substr($this->_ip, 0, strrpos($this->_ip, '.'));
        }

        return sprintf('%08x', crc32(!empty($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] . ROOT_PATH . $ip . $session_id : ROOT_PATH . $ip . $session_id));
    }

    function insert_session()
    {
    	return $this->db->set($this->session_id, array('expiry'=>$this->_time, 'ip'=>$this->_ip, 'data'=>'a:0:{}'), false, $this->max_life_time);
    }

    function load_session()
    {
		$session = $this->db->get($this->session_id);
        if (empty($session))
        {
            $this->insert_session();
            $this->session_expiry = 0;
            $this->session_md5 = '40cd750bba9870f18aada2478b24840a';
            $GLOBALS['_SESSION'] = array();
        }
        else
        {
            if (!empty($session['data']) && $this->_time - $session['expiry'] max_life_time)
            {
                $this->session_expiry = $session['expiry'];
                $this->session_md5 = md5($session['data']);
                $GLOBALS['_SESSION']  = unserialize(stripslashes($session['data']));
            }
            else
            {
                $this->session_expiry = 0;
                $this->session_md5 = '40cd750bba9870f18aada2478b24840a';
                $GLOBALS['_SESSION']  = array();
            }
        }
    }

    function update_session()
    {
        $adminid = !empty($GLOBALS['_SESSION']['admin_id']) ? intval($GLOBALS['_SESSION']['admin_id']) : 0;
        $userid  = !empty($GLOBALS['_SESSION']['user_id'])  ? intval($GLOBALS['_SESSION']['user_id'])  : 0;

        $data = serialize($GLOBALS['_SESSION']);
        $this->_time = time();

        if ($this->session_md5 == md5($data) && $this->_time < $this->session_expiry + 10)
        {
            return true;
        }

        $data = addslashes($data);

    	return $this->db->replace($this->session_id, array(&#39;expiry&#39;=>$this->_time, &#39;ip&#39;=>$this->_ip, &#39;userid&#39;=>$userid, &#39;adminid&#39;=>$adminid, &#39;data&#39;=>$data), false, $this->max_life_time);
    }

    function close_session()
    {
    	$this->update_session();
        return true;
    }

    function delete_spec_admin_session($adminid)
    {
        if (!empty($GLOBALS[&#39;_SESSION&#39;][&#39;admin_id&#39;]) && $adminid)
        {
            $all_items = $this->db->getExtendedStats(&#39;items&#39;);
			$items = $all_items[&#39;127.0.0.1:11211&#39;][&#39;items&#39;];
			foreach ($items as $key => $item) {
				if (isset($item[&#39;adminid&#39;])) {
					if ($item[&#39;adminid&#39;] == $adminid) return $this->db->delete($key);
				}
			}
        }
        else
        {
            return false;
        }
    }

    function destroy_session()
    {    
        $GLOBALS[&#39;_SESSION&#39;] = array();

        setcookie($this->session_name, $this->session_id, 1, $this->session_cookie_path, $this->session_cookie_domain, $this->session_cookie_secure);

        /* ECSHOP 自定义执行部分 */
        if (!empty($GLOBALS[&#39;ecs&#39;]))
        {
			$GLOBALS[&#39;db&#39;]->query(&#39;DELETE FROM &#39; . $GLOBALS[&#39;ecs&#39;]->table(&#39;cart&#39;) . " WHERE session_id = &#39;$this->session_id&#39;");
        }
        /* ECSHOP 自定义执行部分 */

    	return $this->db->delete($this->session_id);
    }

    function get_session_id()
    {
        return $this->session_id;
    }

    function get_users_count()
    {
		$all_items = $this->db->getExtendedStats();
		return $count = $all_items[&#39;127.0.0.1:11211&#39;][&#39;curr_items&#39;];//由于有其他key的缓存,因此这只是个接近数值
    }

}

?>

                   

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
Will R.E.P.O. Have Crossplay?
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools