先来看下自定义数据库处理 session 的执行顺序:

sess_open
sess_read
sess_gc
...其他程序
sess_write || sess_destroy
sess_close

// $domain = '';
//不使用 GET/POST 变量方式
ini_set('session.use_trans_sid', 0);
//设置垃圾回收最大生存时间
ini_set('session.gc_maxlifetime', 1440);
//使用 COOKIE 保存 SESSION ID 的方式
ini_set('session.use_cookies', 0);
//ini_set('session.cookie_path',      '/');
//多主机共享保存 SESSION ID 的 COOKIE
//ini_set('session.cookie_domain', $domain);
//设置用户自定义Session存储
ini_set('session.save_handler', 'user');
ini_set('session.gc_probability', 1);
ini_set('session.gc_divisor', 100);

session_set_save_handler(
	'sess_open',
	'sess_close',
	'sess_read',
	'sess_write',
	'sess_destroy',
	'sess_gc'
);

$SESS_DB = '';
$SESS_DBHOST = '127.0.0.1'; /* database server hostname */
$SESS_DBPORT = 5432; /* database server port */
$SESS_DBNAME = 'DBNAME'; /* database name */
$SESS_DBUSER = 'DBUSER; /* database user */
$SESS_DBPASS = 'DBPASS'; /* database password */
$SESS_LIFE = ini_get('session.gc_maxlifetime');
$SESS_NAME = 'SNAME';


function sess_open($save_path, $session_name){
	global $SESS_DB, $SESS_DBHOST, $SESS_DBPORT, $SESS_DBNAME, $SESS_DBUSER, $SESS_DBPASS;
	$SESS_DB = pg_connect("host=$SESS_DBHOST port=$SESS_DBPORT dbname=$SESS_DBNAME user=$SESS_DBUSER password=$SESS_DBPASS") or die('Could not connect');
	return true;
}

function sess_close(){
	global $SESS_DB;
	if(!empty($SESS_DB)){
		pg_close($SESS_DB);
	}
	return true;
}

function sess_read($key){
	global $SESS_DB, $SESS_LIFE, $uid, $uip;
	自定义
	return false;
}

function sess_write($key, $val){
	global $SESS_DB, $SESS_LIFE, $uid, $ip;
	$expiry = time() + $SESS_LIFE;
	$value = addslashes($val);
	自定义
}

function sess_destroy($key){
	global $SESS_DB;
	自定义
}

function sess_gc($maxlifetime){
	global $SESS_DB;
	自定义
}

One Comment

  • 王炜


    — PostgreSQL database dump

    SET client_encoding = ‘UTF8’;
    SET standard_conforming_strings = off;
    SET check_function_bodies = false;
    SET client_min_messages = warning;
    SET escape_string_warning = off;

    SET search_path = public, pg_catalog;

    SET default_tablespace = ”;

    SET default_with_oids = false;


    — Name: sessions; Type: TABLE; Schema: public; Owner: david; Tablespace:

    CREATE TABLE sessions (
    id character(32) NOT NULL,
    expiry integer DEFAULT 0 NOT NULL,
    value text,
    “user” integer DEFAULT 0 NOT NULL,
    “create” timestamp with time zone NOT NULL,
    update timestamp with time zone NOT NULL,
    ip integer NOT NULL
    );

    ALTER TABLE public.sessions OWNER TO david;


    — Name: sessions_pkey; Type: CONSTRAINT; Schema: public; Owner: david; Tablespace:

    ALTER TABLE ONLY sessions
    ADD CONSTRAINT sessions_pkey PRIMARY KEY (id);


    — PostgreSQL database dump complete

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *