在php3中是没有session这种东东的,但我们又需要,怎么办呢?别急,有很多人替你做了这些,这其中最有名的要算phplib了。你可以去国外下载,可以上国内大部分php站点下载。我们要做的第一件事是让phplib和php3结合在一起使它能工作。为了能实现这方面的功能,我们需要先安装phplib。跟着我来做,很容易的(以下方法在win2000+php3.0.16+apache1.3.12+phplib7.2c+mysql3.23.21 for win32 上通过)phplib最基本的功能包括用户认证,session管理,权限及数据库的抽象化。
if (!isset($_phplib) or !is_array($_phplib)) { $_phplib["libdir"] = "d:/apache/php/"; //这儿改为你放phplib下php目录的路径 }
然后将d:/apache/php/local.inc文件改如下:
class db_example extends db_sql { var $host = "localhost";//你的mysql数据库所在主机名 var $database = "test";//数据库名 var $user = "root";//数据库用户名 var $password = "";//数据库用户口令 }
[session] session.save_handler = files ; handler used to store/retrieve data(用什么保存session变量,默认情况下用文件) session.save_path = c:/temp ; argument passed to save_handler(保存session变量的目录,在linux/unix下为/tmp,在win下设为你的目录) ; in the case of files, this is the ; path where data files are stored session.use_cookies = 1 ; whether to use cookies(是否使用cookies,当然,在win下别无选择) session.name = phpsessid ; name of the session(默认session使用的cookies名,建议不要改动) ; is used as cookie name session.auto_start = 0 ; initialize session on request startup(是否自动启用session,当为1时,在每页中就可以不必调用session_start()函数了) session.cookie_lifetime = 0 ; lifetime in seconds of cookie(设定 cookie 送到浏览器后的保存时间,单位为秒。缺省值为 0,表示直到浏览器关闭。) ; or if 0, until browser is restarted session.cookie_path = / ; the path the cookie is valid for(cookie)(cookies有效路径) session.cookie_domain = ; the domain the cookie is valid for(cookies有效域名) session.serialize_handler = php ; handler used to serialize data(定义序列化数据的标识,本功能只有 wddx 模块或 php 内部使用。缺省值为 php) ; php is the standard serializer of php session.gc_probability = 1 ; percentual probability that the (设定每次临时文件开始处理 (gc, garbage collection) 处理概率。缺省值为 1。 ) ; 'garbage collection' process is started ; on every session initialization session.gc_maxlifetime = 1440 ; after this number of seconds, stored(设定保存session的临时文件被清除前的存活秒数) ; data will be seen as 'garbage' and ; cleaned up by the gc process session.referer_check = ; check http referer to invalidate (决定参照到客户端的session 代码是否要删除。有时在安全或其它考虑时,会设定不删除。缺省值为 0。) ; externally stored urls containing ids session.entropy_length = 0 ; how many bytes to read from the file(设定 session 从高熵值资源读取的位数。缺省值为 0.) session.entropy_file = ; specified here to create the session id(设定 session 代码建立时,使用外部高熵值资源或文件来建立,例如 unix 系统上的 /dev/random 或 /dev/urandom。 ) ; session.entropy_length = 16 ; session.entropy_file = /dev/urandom session.cache_limiter = nocache ; set to { nocache,private,public } to (设定session缓冲限制) ; determine http caching aspects session.cache_expire = 180 ; document expires after n minutes(文档有效期,单位为分钟)
(很抱歉,由于版权原因,我不能把以下代码中的英文去掉,只好加些注释了 ================================================================================== <? /* ------------------------------------------------------------------------ * session_mysql.php * ------------------------------------------------------------------------ * php4 mysql session handler * version 1.00 * by ying zhang ([email protected]) * 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 <[email protected]>. * * ------------------------------------------------------------------------ * 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. * 确信你的php4有mysql支持,然后把这个脚本拷贝到和你的php脚本有关的目录。 * ------------------------------------------------------------------------ * usage:(使用方法) * ------------------------------------------------------------------------ * include this file in your scripts before you call session_start(), you * don't have to do anything special after that. * 包含这个文件到你要使用session的文件中,必须在调用session_start()之前,否则, * 会很惨的,不要怪我没告诉你。 这样就不需要再做什么工作了,还和你以前用session的方法一样。 */
$sess_dbhost = "localhost"; /* database server hostname */ $sess_dbname = "sessions"; /* database name */ $sess_dbuser = "phpsession"; /* database user */ $sess_dbpass = "phpsession"; /* database password */
if (! $qid) { $qry = "update sessions set expiry = $expiry, value = '$value' where sesskey = '$key' and expiry > " . time(); $qid = mysql_query($qry, $sess_dbh); }
return $qid; }
function sess_destroy($key) { global $sess_dbh;
$qry = "delete from sessions where sesskey = '$key'"; $qid = mysql_query($qry, $sess_dbh);
return $qid; }
function sess_gc($maxlifetime) { global $sess_dbh;
$qry = "delete from sessions where expiry < " . time(); $qid = mysql_query($qry, $sess_dbh);
return mysql_affected_rows($sess_dbh); }
session_set_save_handler( "sess_open", "sess_close", "sess_read", "sess_write", "sess_destroy", "sess_gc"); ?> ================================================================= 定制使用dbm文件时的接口 ================================================================= <? /* ------------------------------------------------------------------------ * session_dbm.php * ------------------------------------------------------------------------ * php4 dbm session handler * version 1.00 * by ying zhang ([email protected]) * 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 <[email protected]>. * * ------------------------------------------------------------------------ * 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. * 确信你的php4有dbm支持。拷贝这个文件在你的php脚本目录。 * ------------------------------------------------------------------------ * usage: * ------------------------------------------------------------------------ * include this file in your scripts before you call session_start(), you * don't have to do anything special after that. * 在调用session_start()之前请包含这个文件。之后就不需要作什么工作了。 */