[代码] [PHP]代码http://www.oschina.net/code/snippet_83895_6963 001 <?php 002 003 if (!defined('IN_ECS')) 004 { 005 die('Hacking attempt'); 006 } 007 0 ...
[代码] [PHP]代码
http://www.oschina.net/code/snippet_83895_6963
003 | if (!defined( 'IN_ECS' )) |
005 | die ( 'Hacking attempt' ); |
015 | var $max_life_time = 1800; |
017 | var $session_name = '' ; |
020 | var $session_expiry = '' ; |
021 | var $session_md5 = '' ; |
023 | var $session_cookie_path = '/' ; |
024 | var $session_cookie_domain = '' ; |
025 | var $session_cookie_secure = false; |
030 | function __construct(& $db , $session_table , $session_data_table , $session_name = 'ECS_ID' , $session_id = '' ) |
033 | $m ->addServer( '127.0.0.1' , 11211); |
034 | $this ->cls_session( $m , $session_name , $session_id ); |
037 | function cls_session(& $db , $session_name = 'ECS_ID' , $session_id = '' ) |
039 | $GLOBALS [ '_SESSION' ] = array (); |
041 | if (! empty ( $GLOBALS [ 'cookie_path' ])) |
043 | $this ->session_cookie_path = $GLOBALS [ 'cookie_path' ]; |
047 | $this ->session_cookie_path = '/' ; |
050 | if (! empty ( $GLOBALS [ 'cookie_domain' ])) |
052 | $this ->session_cookie_domain = $GLOBALS [ 'cookie_domain' ]; |
056 | $this ->session_cookie_domain = '' ; |
059 | if (! empty ( $GLOBALS [ 'cookie_secure' ])) |
061 | $this ->session_cookie_secure = $GLOBALS [ 'cookie_secure' ]; |
065 | $this ->session_cookie_secure = false; |
068 | $this ->session_name = $session_name ; |
071 | $this ->_ip = real_ip(); |
073 | if ( $session_id == '' && ! empty ( $_COOKIE [ $this ->session_name])) |
075 | $this ->session_id = $_COOKIE [ $this ->session_name]; |
079 | $this ->session_id = $session_id ; |
082 | if ( $this ->session_id) |
084 | $tmp_session_id = substr ( $this ->session_id, 0, 32); |
085 | if ( $this ->gen_session_key( $tmp_session_id ) == substr ( $this ->session_id, 32)) |
087 | $this ->session_id = $tmp_session_id ; |
091 | $this ->session_id = '' ; |
095 | $this ->_time = time(); |
097 | if ( $this ->session_id) |
099 | $this ->load_session(); |
103 | $this ->gen_session_id(); |
104 | 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); |
106 | register_shutdown_function( array (& $this , 'close_session' )); |
109 | function gen_session_id() |
111 | $this ->session_id = md5(uniqid(mt_rand(), true)); |
113 | return $this ->insert_session(); |
116 | function gen_session_key( $session_id ) |
122 | $ip = substr ( $this ->_ip, 0, strrpos ( $this ->_ip, '.' )); |
125 | return sprintf( '%08x' , crc32(! empty ( $_SERVER [ 'HTTP_USER_AGENT' ]) ? $_SERVER [ 'HTTP_USER_AGENT' ] . ROOT_PATH . $ip . $session_id : ROOT_PATH . $ip . $session_id )); |
128 | function insert_session() |
130 | return $this ->db->set( $this ->session_id, array ( 'expiry' => $this ->_time, 'ip' => $this ->_ip, 'data' => 'a:0:{}' ), false, $this ->max_life_time); |
133 | function load_session() |
135 | $session = $this ->db->get( $this ->session_id); |
138 | $this ->insert_session(); |
139 | $this ->session_expiry = 0; |
140 | $this ->session_md5 = '40cd750bba9870f18aada2478b24840a' ; |
141 | $GLOBALS [ '_SESSION' ] = array (); |
145 | if (! empty ( $session [ 'data' ]) && $this ->_time - $session [ 'expiry' ] <= $this ->max_life_time) |
147 | $this ->session_expiry = $session [ 'expiry' ]; |
148 | $this ->session_md5 = md5( $session [ 'data' ]); |
149 | $GLOBALS [ '_SESSION' ] = unserialize( stripslashes ( $session [ 'data' ])); |
153 | $this ->session_expiry = 0; |
154 | $this ->session_md5 = '40cd750bba9870f18aada2478b24840a' ; |
155 | $GLOBALS [ '_SESSION' ] = array (); |
160 | function update_session() |
162 | $adminid = ! empty ( $GLOBALS [ '_SESSION' ][ 'admin_id' ]) ? intval ( $GLOBALS [ '_SESSION' ][ 'admin_id' ]) : 0; |
163 | $userid = ! empty ( $GLOBALS [ '_SESSION' ][ 'user_id' ]) ? intval ( $GLOBALS [ '_SESSION' ][ 'user_id' ]) : 0; |
165 | $data = serialize( $GLOBALS [ '_SESSION' ]); |
166 | $this ->_time = time(); |
168 | if ( $this ->session_md5 == md5( $data ) && $this ->_time < $this ->session_expiry + 10) |
173 | $data = addslashes ( $data ); |
175 | return $this ->db->replace( $this ->session_id, array ( 'expiry' => $this ->_time, 'ip' => $this ->_ip, 'userid' => $userid , 'adminid' => $adminid , 'data' => $data ), false, $this ->max_life_time); |
178 | function close_session() |
180 | $this ->update_session(); |
184 | function delete_spec_admin_session( $adminid ) |
186 | if (! empty ( $GLOBALS [ '_SESSION' ][ 'admin_id' ]) && $adminid ) |
188 | $all_items = $this ->db->getExtendedStats( 'items' ); |
189 | $items = $all_items [ '127.0.0.1:11211' ][ 'items' ]; |
190 | foreach ( $items as $key => $item ) { |
191 | if (isset( $item [ 'adminid' ])) { |
192 | if ( $item [ 'adminid' ] == $adminid ) return $this ->db-> delete ( $key ); |
202 | function destroy_session() |
204 | $GLOBALS [ '_SESSION' ] = array (); |
206 | setcookie( $this ->session_name, $this ->session_id, 1, $this ->session_cookie_path, $this ->session_cookie_domain, $this ->session_cookie_secure); |
209 | if (! empty ( $GLOBALS [ 'ecs' ])) |
211 | $GLOBALS [ 'db' ]->query( 'DELETE FROM ' . $GLOBALS [ 'ecs' ]->table( 'cart' ) . " WHERE session_id = '$this->session_id'" ); |
215 | return $this ->db-> delete ( $this ->session_id); |
218 | function get_session_id() |
220 | return $this ->session_id; |
223 | function get_users_count() |
225 | $all_items = $this ->db->getExtendedStats(); |
226 | return $count = $all_items [ '127.0.0.1:11211' ][ 'curr_items' ]; |