php中的session完全教程第2/2页
更新时间:2007年03月18日 00:00:00 作者:
(很抱歉,由于版权原因,我不能把以下代码中的英文去掉,只好加些注释了
=====================================================================
<?
/* ------------------------------------------------------------------------
* session_mysql.php
* ------------------------------------------------------------------------
* php4 mysql session handler
* version 1.00
* by ying zhang (ying@zippydesign.com)
* 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 <ying@zippydesign.com>.
*
* ------------------------------------------------------------------------
* 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 */
$sess_dbh = "";
$sess_life = get_cfg_var("session.gc_maxlifetime");
function sess_open($save_path, $session_name) {
global $sess_dbhost, $sess_dbname, $sess_dbuser, $sess_dbpass, $sess_dbh;
if (! $sess_dbh = mysql_pconnect($sess_dbhost, $sess_dbuser, $sess_dbpass)) {
echo "<li>can't connect to $sess_dbhost as $sess_dbuser";
echo "<li>mysql error: ", mysql_error();
die;
}
if (! mysql_select_db($sess_dbname, $sess_dbh)) {
echo "<li>unable to select database $sess_dbname";
die;
}
return true;
}
function sess_close() {
return true;
}
function sess_read($key) {
global $sess_dbh, $sess_life;
$qry = "select value from sessions where sesskey = '$key' and expiry > " . time();
$qid = mysql_query($qry, $sess_dbh);
if (list($value) = mysql_fetch_row($qid)) {
return $value;
}
return false;
}
function sess_write($key, $val) {
global $sess_dbh, $sess_life;
$expiry = time() + $sess_life;
$value = addslashes($val);
$qry = "insert into sessions values ('$key', $expiry, '$value')";
$qid = mysql_query($qry, $sess_dbh);
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 (ying@zippydesign.com)
* 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 <ying@zippydesign.com>.
*
* ------------------------------------------------------------------------
* 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()之前请包含这个文件。之后就不需要作什么工作了。
*/
$sess_dbm = "";
$sess_life = get_cfg_var("session.gc_maxlifetime");
function sess_open($save_path, $session_name) {
global $sess_dbm;
$sess_dbm = dbmopen("$save_path/$session_name", "c");
return ($sess_dbm);
}
function sess_close() {
global $sess_dbm;
dbmclose($sess_dbm);
return true;
}
function sess_read($key) {
global $sess_dbm, $sess_life;
$var = "";
if ($tmp = dbmfetch($sess_dbm, $key)) {
$expires_at = substr($tmp, 0, strpos($tmp, "│"));
if ($expires_at > time()) {
$var = substr($tmp, strpos($tmp, "│") + 1);
}
}
return $var;
}
function sess_write($key, $val) {
global $sess_dbm, $sess_life;
dbmreplace($sess_dbm, $key, time() + $sess_life . "│" . $val);
return true;
}
function sess_destroy($key) {
global $sess_dbm;
dbmdele($sess_dbm, $key);
return true;
}
function sess_gc($maxlifetime) {
global $sess_dbm;
$now = time();
$key = dbmfirstkey($sess_dbm);
while ($key) {
if ($tmp = dbmfetch($sess_dbm, $key)) {
$expires_at = substr($tmp, 0, strpos($tmp, "│"));
if ($now > $expires_at) {
sess_destroy($key);
}
}
$key = dbmnextkey($sess_dbm, $key);
}
}
session_set_save_handler(
"sess_open",
"sess_close",
"sess_read",
"sess_write",
"sess_destroy",
"sess_gc");
?>
=================================================================
具体怎么用就不用多说了,因为这些函数都是php引擎调用的,与我们无关的。我们只需照上述配
置后就行了,你用的还是以前的session函数。
看看下面的代码就知道了:)
session定制的测试代码
==================================================================
<?
/* ------------------------------------------------------------------------
* test.php
* ------------------------------------------------------------------------
* php4 customer session handler test script
* version 1.00
* by ying zhang (ying@zippydesign.com)
* last modified: may 21 2000
*/
/* default to dbm handler */
if (! isset($handler)) {
$handler = "dbm";
}
/* default action is increment */
if (! isset($action)) {
$action = "increment";
}
/* load up the appropriate session handling script, depending on the handler */
if ($handler == "dbm") {
include("session_dbm.php");
} elseif ($handler == "mysql") {
include("session_mysql.php");
} else {
echo "<li>unrecognized handler ($handler)";
die;
}
/* start the session and register a simple counter */
session_start();
session_register("count");
/* figure out what we should do, depending on the action */
switch ($action) {
case "increment" :
$count = isset($count) ? $count + 1 : 0;
break;
case "destroy" :
session_destroy();
break;
case "gc" :
$maxlife = get_cfg_var("session.gc_maxlifetime");
sess_gc($maxlife);
break;
default:
echo "<li>unknown action ($action)";
break;
}
?>
<h1>session test script</h1>
<ul>
<li>handler: <b><?=$handler?></b>
<li>action: <b><?=$action?></b>
<li>count: <b><?=$count?></b>
</ul>
<hr size=1>
<form>
<table>
<tr>
<td>handler:</td>
<td>
<select name="handler">
<option value="dbm">dbm</option>
<option value="mysql">mysql</option>
</select>
</td>
</tr>
<tr>
<td>action:</td>
<td>
<select name="action">
<option value="increment">increment</option>
<option value="destroy">session destroy</option>
<option value="gc">force garbage collection</option>
</select>
</td>
</tr>
<tr>
<td></td>
<td><br><input type="submit"></td>
</tr>
</table>
</form>
=======================================================================
三、session应用举例
以下这些例子片断仅供参考,你可以定制也可以不定制session,随你便
(1)用于用户认证
<?
session_start();
$dbh = mysql_connect("localhost:3306","xxxx","xxxx");
mysql_select_db("znsoft");//选择数据库
$query="select userid from reguser where userid='$userid' and pass='$pass' ";
//$userid $pass 是登录form传递过来的用户名和密码
$res=mysql_query($query,$dbh);
if($row=mysql_fetch($res))
{
$reguser=$row[0];
?>
<script>
alert("ok,哥们,欢迎你!");
</script>
<?
}
else
{
$reguser="";
?>
<script>
alert("sorry,你不是注册用户!");
</script>
<?
//你自己放上代码吧
}
session_register("reguser");
?>
另一页面中检查是否已经登录
================
<?
session_start();
if(isset($reguser)&&$reguser!="")//已经登录
{
echo "欢迎你,哥们";
}
else//没有登录呀
echo "请注册吧";
?>
退出功能
===============================
<?
session_destroy();
//或 $reguser="";
?>
(2)用于传递变量
本程序用于在页面之间传递变量
<?
$name="m.y";
if(!sesion_is_registered("name"))//没有注册session变量 name
session_register("name");//注册 变量 name
?>
第二页
===================
<?
echo $name;
//不想用了,删掉吧
if(session_is_registered("name"))//是否注册,如果已经注册
session_unregister("name");//当然删掉啦
?>
相关文章
Discuz!5的PHP代码高亮显示插件(黑暗中的舞者更新)
Discuz!5的PHP代码高亮显示插件(黑暗中的舞者更新)...2007-01-01图片存储与浏览一例(Linux+Apache+PHP+MySQL)
图片存储与浏览一例(Linux+Apache+PHP+MySQL)...2006-10-10
最新评论