Mysql存取session实例

files:
common/Common.config.php
include/session.inc.php
session_test.php
get_session_test.php
get_session_test2.php

Common.config.php

 1   
 2/*   
 3* Common config   
 4* By 恋太后天   
 5*/ 
 6
 7  
 8/*   
 9* Database config   
10*/   
11define( "DBTYPE", "mysql" );   
12$database = array   
13(   
14"mysql" => array   
15(   
16"default" => array   
17(   
18"host" => "localhost",   
19"user" => "root",   
20"password" => "",   
21"dbname" => ""   
22),   
23"session" => array   
24(   
25"host" => "localhost",   
26"user" => "session",   
27"password" => "session",   
28"dbname" => "sessions"   
29)   
30)   
31); 

session.inc.php

 1   
 2//使用mysql存放session 函数表   
 3// by 恋太后天 2005-4-28 
 4
 5if (!isset($include_path)) $include_path = ''; 
 6
 7if (!is_array($database))   
 8{   
 9include ($include_path."common/Common.config.php");   
10} 
11
12$DBsess = $database[DBTYPE]["session"];   
13$DBsess_link = mysql_connect($DBsess["host"], $DBsess["user"], $DBsess["password"])   
14or die ("Error:

<em>Can not connect to Mysql server.</em>

 1"); 
 2
 3$SESS_LIFE = get_cfg_var("session.gc_maxlifetime"); 
 4
 5function sess_open($path, $name)   
 6{   
 7return true;   
 8} 
 9
10function sess_close()   
11{   
12return true;   
13} 
14
15function sess_read($id)   
16{   
17global $DBsess , $DBsess_link;   
18mysql_select_db($DBsess["dbname"]);   
19$now = time();   
20$result = mysql_query("SELECT `data` FROM `sessions`   
21WHERE `id`= '$id' AND `expiry_time` > $now", $DBsess_link);   
22if (list($data) = mysql_fetch_row($result))   
23{   
24return $data;   
25}   
26return false;   
27} 
28
29function sess_write($id, $data)   
30{   
31global $DBsess , $DBsess_link, $SESS_LIFE;   
32mysql_select_db($DBsess["dbname"]); 
33
34$expiry_time = time() + $SESS_LIFE; 
35
36if ( !get_magic_quotes_gpc() )   
37{   
38$data = addslashes($data);   
39} 
40
41$now = time(); 
42
43$result = mysql_query("INSERT into `sessions` (`id`, `expiry_time`, `data`)", $DBsess_link); 
44
45if ( !$result )   
46{   
47$result = mysql_query("UPDATE `sessions` SET `data`='$data', `expiry_time`=$expiry_time   
48WHERE `id` = '$id' AND `expiry_time` > $now", $DBsess_link);   
49} 
50
51return $result;   
52} 
53
54function sess_destroy($id)   
55{   
56global $DBsess , $DBsess_link;   
57mysql_select_db($DBsess["dbname"]);   
58$query = mysql_query("DELETE FROM `session` WHERE `id`='$id'");   
59return $query;   
60} 
61
62function sess_gc($maxlifetime)   
63{   
64global $DBsess , $DBsess_link;   
65$query = mysql_query("DELETE FROM `sessions` WHERE `expiry_time` < " . time(), $DBsess_link);   
66return mysql_affected_rows($DBsess_link); 
67
68} 
69
70session_module_name();   
71session_set_save_handler("sess_open", "sess_close", "sess_read", "sess_write", "sess_destroy", "sess_gc"); 

session_test.php

 1   
 2// test for using session   
 3include ("common/Common.config.php");   
 4include ("include/session.inc.php"); 
 5
 6session_start(); 
 7
 8$_SESSION["abc"] = "A: I will be back!";   
 9$_SESSION["meto"] = "B: Me too ";   
10echo "

<a href='"get_session_test.php"'>click me</a>

1"; 

get_session_test.php

 1   
 2// test for using session   
 3include ("common/Common.config.php");   
 4include ("include/session.inc.php"); 
 5
 6session_start();   
 7/*   
 8* www.knowsky.com   
 9*/   
10$_SESSION["c"] = "

<br/>

1C: I will follow U. ^0^!";   
2print($_SESSION["abc"]);   
3print("

<br/>

1");   
2print($_SESSION["meto"]);   
3echo "

<br/>

1".   
2"

<a href='"get_session_test2.php"'>click again</a>

1"; 

get_session_test2.php

1   
2//get_session_test2.php   
3// test for using session   
4include ("common/Common.config.php");   
5include ("include/session.inc.php"); 
6
7session_start();   
8print($_SESSION["c"]);   
Published At
Categories with Web编程
Tagged with
comments powered by Disqus