Mysql session处理程序 (This 我们的下一个范例是写一个将session数据存到mysql数据库的定制session处理程序。(这个在session_mysql.php文件中,见文章尾部) 在你有许多支持PHP的服务器并且你需要共享它们之间的session时你会想将session保存在数据库中的。(比如你服务于很多用户并且需要 负载平衡时) You have a bunch of machines doing web/PHP stuff, a machine serving 你有一批机器作支持php的服务器,需要一台机器作你的普通数据库服务器,另外一台运行mysql数据库处理session。仅这样对大多数人来 说就具有很大的杀伤力的。:)(译注:可能意思是太酷了吧)
faq: 如果你担心session文件在/tmp目录中和别的虚拟机混淆,正好将它们存到别处。 这就是为什么有session.save_path选项的原因。 如果你担心性能的话你可以考虑将session存在共享内存中。你只需要在编译php时加上MM支持(--with-mm)并指定sessio.save_handler 为 mm 在.htaccess中,php.ini中或httpd.conf中。
我觉得只有多台机器要保存session时才会用到数据库。 (最后这句实在不好译,自已看吧)
session_dbm.php ========================================================================= <? /* ------------------------------------------------------------------------ * session_dbm.php * ------------------------------------------------------------------------ * PHP4 DBM Session Handler * Version 1.00 * by Ying Zhang (ying@zippydesign.com) * Last Modified: May 21 2000 * * ------------------------------------------------------------------------ * TERMS OF USAGE: * ------------------------------------------------------------------------ * You are free to use this library in any way you want, no warranties are * expressed or implied. This works for me, but I don't guarantee that it * works for you, USE AT YOUR OWN RISK. * * While not required to do so, I would appreciate it if you would retain * this header information. If you make any modifications or improvements, * please send them via email to Ying Zhang <ying@zippydesign.com>. * * ------------------------------------------------------------------------ * DESCRIPTION: * ------------------------------------------------------------------------ * This library tells the PHP4 session handler to write to a DBM file * instead of creating individual files for each session. * * ------------------------------------------------------------------------ * INSTALLATION: * ------------------------------------------------------------------------ * Make sure you have DBM support compiled into PHP4. Then copy this * script to a directory that is accessible by the rest of your PHP * scripts. * * ------------------------------------------------------------------------ * USAGE: * ------------------------------------------------------------------------ * Include this file in your scripts before you call session_start(), you * don't have to do anything special after that. */
function sess_gc($maxlifetime) { global $SESS_DBM;
$now = time(); $key = dbmfirstkey($SESS_DBM); while ($key) { if ($tmp = dbmfetch($SESS_DBM, $key)) { $expires_at = substr($tmp, 0, strpos($tmp, "¦")); if ($now > $expires_at) { sess_destroy($key); } }
$key = dbmnextkey($SESS_DBM, $key); } }
session_set_save_handler( "sess_open", "sess_close", "sess_read", "sess_write", "sess_destroy", "sess_gc"); ?> ===================================================================================== session_mysql.php ======================================================================================= <? /* ------------------------------------------------------------------------ * session_mysql.php * ------------------------------------------------------------------------ * PHP4 MySQL Session Handler * Version 1.00 * by Ying Zhang (ying@zippydesign.com) * Last Modified: May 21 2000 * * ------------------------------------------------------------------------ * TERMS OF USAGE: * ------------------------------------------------------------------------ * You are free to use this library in any way you want, no warranties are * expressed or implied. This works for me, but I don't guarantee that it * works for you, USE AT YOUR OWN RISK. * * While not required to do so, I would appreciate it if you would retain * this header information. If you make any modifications or improvements, * please send them via email to Ying Zhang <ying@zippydesign.com>. * * ------------------------------------------------------------------------ * DESCRIPTION: * ------------------------------------------------------------------------ * This library tells the PHP4 session handler to write to a MySQL database * instead of creating individual files for each session. * * Create a new database in MySQL called "sessions" like so: * * CREATE TABLE sessions ( * sesskey char(32) not null, * expiry int(11) unsigned not null, * value text not null, * PRIMARY KEY (sesskey) * ); * * ------------------------------------------------------------------------ * INSTALLATION: * ------------------------------------------------------------------------ * Make sure you have MySQL support compiled into PHP4. Then copy this * script to a directory that is accessible by the rest of your PHP * scripts. * * ------------------------------------------------------------------------ * USAGE: * ------------------------------------------------------------------------ * Include this file in your scripts before you call session_start(), you * don't have to do anything special after that. */
$SESS_DBHOST = "localhost"; /* database server hostname */ $SESS_DBNAME = "sessions"; /* database name */ $SESS_DBUSER = "phpsession"; /* database user */ $SESS_DBPASS = "phpsession"; /* database password */