APC_handler is sessionhandler class which combine memory (apc) with
mysql base to store session data. If is not possible to write
data to memory (the keeped free memory can be set), then the handler write them to base.
User can not log in (with login()) twice in the same time with different browsers (SID-s),
and the is_logged() function returns SID of logged user which can be compare with current.
For multiple logins do not use login() & is_logged, no need for them. If You found a bug, You can send mail to kami.eper@gmail.com. |
APC_handler public functions: | |
mysql_connect ($host, $user, $password) | Define data for mysql connection. |
mysql_base($base, $sess_table, $logg_table) | Define base, sessions & loggeds table for store the data. |
expire($minutes) | Define session expiration, 0-> never expire. |
keep_free($MB) | If the size of free memory below $MB start to write into base. |
set_session_handler() | Activate the handler. |
login($user) | Sets user to logged (only if session is started), returns TRUE or FALSE if fails. |
is_logged($user) | Returns SID if user is logged or FALSE (the user can be logged only from one SID at the same time). |
logout($user) | Logout. |
base2mem() | Copy all session data (without logged data) from base to memory (how many is possible). |
mem2base() | Copy all session data (without logged data) from memory to base. |
get_connection() | Returns connection resource for reuse (persistent connection). |
reset() | Empty the memory & base used by sessions (the other data in memory will be keeped). This function must to be called before set_session_handler();. Usable for testing. |
no_apc() | If this function is called before set_session_handler the handler will only use base to store data. Exist sessions will copy from memory to base and new logins will write to base (old logins will still read from memory as long not expired). |
keep_base_on_restart() | If this function is called on system restart the session data in mysql base will be keeped (by default the session data will be deleted). The logged users anyway will be deleted on restart, and the session data in memory will be lost. If the session data are important, you can call this function, and before restart copy memory to base, and after copy it back. |
APC_handler limitations: |
The handler stores the data in memory in form sess:SID=>value & logged:SID=>value, and sets one variable in memory md5('session') to check system restarts. Overwriting them, clearing the cache or system restart will result data loss. If the system is restarted, the handler will create a new empty session space if is not called keep_base_on_restart() function. The base2mem and mem2base can be used to make backup copy from data in memory. The handler use persisten connection for base, the server must support it (how many is allowed). The handler cannot overload the apc memory, but if another process overload it, may cause unexpected result. |
Example APC_handler call file |
<?php include_once('APC_handler.class.php'); $session = new APC_handler(); // $session->reset(); // $session->no_apc(); // $session->keep_base_on_restart(); $session->mysql_connect ('host', 'user', 'password'); $session->mysql_base('temp', 'sessions', 'loggeds'); $session->expire(5); // minutes $session->keep_free(10); // MB $session->set_session_handler(); // get the connection if not want // to make more (persistent) $mysqli=$session->get_connection(); session_id(md5('user')); session_start(); $SID=$session->is_logged('user'); if (! $SID) { echo '<br>User is not logged (session expired or logged out).'; $session->login('user'); } elseif (($SID) && ($SID!=session_id())) { echo "<br>User is logged once with id:$SID."; } else { echo "<br>User is logged with current session id."; } if (! isset($_SESSION['check'])) { echo '<br>Session is expired or first time is created.'; $_SESSION['check']=1; } else { echo '<br>Session check passed ($_SESSION[\'check\']='.$_SESSION['check'].').'; } // $session->logout('user'); //session_destroy(); ?> |