Rewrite the session mechanism in ecshop and port it from DB to Memcache-PHP source code

Source: Internet
Author: User
Rewrite the session mechanism in ecshop and port the database to Memcache.
AddServer ('2017. 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-> s Ession_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 )); re Turn $ 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_e Xpiry = 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 ('expiry' => $ this-> _ time, 'IP' => $ this-> _ ip, 'userid' => $ userid, 'adminid' => $ adminid, 'data' => $ data), false, $ this-> m Ax_life_time);} function close_session () {$ this-> update_session (); return true;} function delete_spec_admin_session ($ adminid) {if (! Empty ($ GLOBALS ['_ session'] ['admin _ id']) & $ adminid) {$ all_items = $ this-> db-> getExtendedStats ('items '); $ items = $ all_items ['123. 0.0.1: 11211 '] ['items']; foreach ($ items as $ key => $ item) {if (isset ($ item ['adminid']) {if ($ item ['adminid'] ==$ adminid) return $ this-> db-> delete ($ key) ;}} else {return false ;}} function destroy_session () {$ GLOBALS ['_ session'] = array (); setcookie ($ this-> session_na Me, $ this-> session_id, 1, $ this-> session_cookie_path, $ this-> session_cookie_domain, $ this-> session_cookie_secure ); /* ECSHOP custom execution part */if (! Empty ($ GLOBALS ['ecs']) {$ GLOBALS ['DB']-> query ('delete from '. $ GLOBALS ['ecs']-> table ('cart '). "WHERE session_id = '$ this-> session_id '");} /* ECSHOP custom execution part */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 ['2017. 0.0.1: 11211 '] ['curr _ items']; // Is cached with other keys, so this is only a close value }}?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.