<span class="unnamed3">php的session默认是以文件方式保存在服务器端,并且在客户端使用cookie保存变量,这就会出现一个问题,当一个用户由于某种安全原因关闭了浏览器的cookie,程序中的session相关操作将无法执行。因此,如果能以数据库保存session数据,将不受客户端设置的限制,并且在性能和扩展性等方面有一个飞跃。程序中使用关键函数是session_set_save_handler,同时要将php.ini里的session.save_handler = files 改为 user。这里我们讨论的环境是linux(freesd)+apache+mysql+php。 <br><br>数据表结构:[sessions] <br>CREATE TABLE sessions ( <br>sesskey char(32) not null, <br>expiry int(11) unsigned not null, <br>value text not null, <br>PRIMARY KEY (sesskey) <br>); <br><br>程序代码:[session_inc.php] <br><?php <br/>$SESS_DBHOST = "yourhost"; /* database server hostname */ <br>$SESS_DBNAME = "yourdb"; /* database name */ <br>$SESS_DBUSER = "youruser"; /* database user */ <br>$SESS_DBPASS = "yourpassword"; /* database password */ <br><br>$SESS_DBH = ""; <br>$SESS_LIFE = get_cfg_var("session.gc_maxlifetime"); <br><br>function sess_open($save_path, $session_name) { <br> global $SESS_DBHOST, $SESS_DBNAME, $SESS_DBUSER, $SESS_DBPASS, $SESS_DBH; <br><br> if (! $SESS_DBH = mysql_pconnect($SESS_DBHOST, $SESS_DBUSER, $SESS_DBPASS)) { <br> echo "<li>Can't connect to $SESS_DBHOST as $SESS_DBUSER"; <br> echo "</li> <li>MySQL Error: " . mysql_error(); <br> die; <br> } <br><br> if (! mysql_select_db($SESS_DBNAME, $SESS_DBH)) { <br> echo "</li> <li>Unable to select database $SESS_DBNAME"; <br> die; <br> } <br><br> return true; <br>} <br><br>function sess_close() { <br> return true; <br>} <br><br>function sess_read($key) { <br> global $SESS_DBH, $SESS_LIFE; <br><br> $qry = "SELECT value FROM session_tbl WHERE sesskey = '$key' AND expiry > " . time(); <br> $qid = mysql_query($qry, $SESS_DBH); <br><br> if (list($value) = mysql_fetch_row($qid)) { <br> return $value; <br> } <br><br> return false; <br>} <br><br>function sess_write($key, $val) { <br> global $SESS_DBH, $SESS_LIFE; <br><br> $expiry = time() + $SESS_LIFE; //过期时间 <br> $value = addslashes($val); <br><br> $qry = "INSERT INTO session_tbl VALUES ('$key', $expiry, '$value')"; <br> $qid = mysql_query($qry, $SESS_DBH); <br><br> if (! $qid) { <br> $qry = "UPDATE session_tbl SET expiry = $expiry, value = '$value' WHERE sesskey = '$key' AND expiry > " . time(); <br> $qid = mysql_query($qry, $SESS_DBH); <br> } <br><br> return $qid; <br>} <br><br>function sess_destroy($key) { <br> global $SESS_DBH; <br><br> $qry = "DELETE FROM session_tbl WHERE sesskey = '$key'"; <br> $qid = mysql_query($qry, $SESS_DBH); <br><br> return $qid; <br>} <br><br>function sess_gc($maxlifetime) { <br> global $SESS_DBH; <br><br> $qry = "DELETE FROM session_tbl WHERE expiry $qid = mysql_query($qry, $SESS_DBH); <br><br> return mysql_affected_rows($SESS_DBH); <br>} <br><br>session_set_save_handler( <br>"sess_open", <br>"sess_close", <br>"sess_read", <br>"sess_write", <br>"sess_destroy", <br>"sess_gc"); <br><br>session_start(); <br>?> <br><br>完成以上步骤后,在程序中使用require("session_inc.php")来代替session_start()即可,其他的session函数还是象以前一样的方法调用。 <br><br> </li></span> <p style="width:100%;text-align:center;margin:10px 0"> <br> <br> </p> <p style="width:100%;text-align:center;margin:10px 0"> </p> <p class="clear"></p>