、
$gb_DBHOSTname = "127.0.0.1"; //主机的名称或是IP地址 02 $gb_DBname = "dbname"; //数据库名称 03 $gb_DBuser = "username"; //数据库用户名称 04 $gb_DBpass = "pwd"; //数据库密码 05 $gb_COOKIE_DOMAIN = ‘.a.com‘; 06 $SESS_DBH = ""; 07 $SESS_LIFE = get_cfg_var("session.gc_maxlifetime"); //得到session的最大有效期。 08 session_id(); //不使用 GET/POST 变量方式 09 ini_set(‘session.use_trans_sid‘, 0); //设置垃圾回收最大生存时间 10 ini_set(‘session.gc_maxlifetime‘, 13600); //使用 COOKIE 保存 SESSION ID 的方式 11 ini_set(‘session.use_cookies‘, 1); 12 ini_set(‘session.cookie_path‘, ‘/‘); //多主机共享保存 SESSION ID 的 COOKIE 13 ini_set("session.cookie_domain", $gb_COOKIE_DOMAIN); 14 //将 session.save_handler 设置为 user,而不是默认的 files session_module_name(‘user‘); 15 function sess_open($save_path, $session_name) { 16 global $gb_DBHOSTname, $gb_DBname, $gb_DBuser, $gb_DBpass, $SESS_DBH; 17 if (!$SESS_DBH = mysql_pconnect($gb_DBHOSTname, $gb_DBuser, $gb_DBpass)) { 18 die(‘MySQL Error‘); 19 } 20 mysql_query("SET character_set_connection=utf8, character_set_results=utf8, character_set_client=binary", $SESS_DBH); 21 if (!mysql_select_db($gb_DBname, $SESS_DBH)) { 22 die(‘MySQL Error‘); 23 } 24 return true; 25 } 26 27 function sess_close() { 28 global $SESS_DBH; 29 //$SESS_DBH->Close(); 30 return true; 31 } 32 33 function sess_read($key) { 34 global $SESS_DBH, $SESS_LIFE; 35 // var_dump($SESS_DBH); 36 $qry = "select session_data from sessions where session_id = ‘$key‘ "; 37 $qid = mysql_query($qry, $SESS_DBH); 38 // var_dump($qid); 39 if (list ($value) = mysql_fetch_row($qid)) { 40 return $value; 41 } 42 return false; 43 } 44 45 function sess_write($key, $val) { 46 global $SESS_DBH, $SESS_LIFE; 47 $session_last_access = time(); 48 $value = $val; 49 $qry = "insert into sessions values(‘$key‘,$session_last_access,‘$value‘)"; 50 $qid = mysql_query($qry, $SESS_DBH); 51 if (!$qid) { 52 $qry = "update sessions set session_last_access=$session_last_access, session_data=‘$value‘ where session_id=‘$key‘ "; 53 $qid = mysql_query($qry, $SESS_DBH); 54 } 55 return $qid; 56 } 57 58 function sess_destroy($key) { 59 global $SESS_DBH; 60 $qry = "delete from sessions where session_id = ‘$key‘"; 61 $qid = mysql_query($qry, $SESS_DBH); 62 return $qid; 63 } 64 65 function sess_gc($maxlifetime) { 66 global $SESS_DBH; 67 $old = time() - $maxlifetime; 68 $old = mysql_real_escape_string($old); 69 $qry = "delete from sessions where session_last_access < " . $old; 70 $qid = mysql_query($qry, $SESS_DBH); 71 return mysql_affected_rows($SESS_DBH); 72 } 73 session_module_name(); 74 session_set_save_handler("sess_open", "sess_close", "sess_read", "sess_write","sess_destroy", "sess_gc"); 75 session_start();
时间: 2024-10-22 19:34:16