动网论坛,站长建站首选,国内使用量最多的论坛软件 动网论坛官方技术讨论区 站长工具 申请属于您自己的免费论坛
首页 | 新闻资讯 | 网站运营 | 网络编程 | 数据库 | 服务器 | 网页设计 | 图像媒体 | 网络应用 | 搜索优化 | 资源下载 | 动网主机 | DVBOX
    本站内  互联网 ASP论坛  ASP.Net论坛  PHP论坛
  
   PHP → 阅读文章

 在PHP3中实现SESSION的功能

作者来源: 
阅读 1444 人次 , 2006-3-29 4:11:00 


<?php require("cookie.inc.php3") ; ?>
<!doctype html public "-//w3c//dtd html 4.0 transitional//en">

<html>
<head>
<title>untitled</title>
</head>

<body>
<p>在网页的任何地方设置cookie
<?php
if (!$show){
$username="liubing";
jssetcookie("username","liubing",1);
echo "<p>cookie username 被设置成 $username<br>";
echo "有效期1分钟<br>" ;
echo "<a href='$path_info?show=1'> 试一下cookiee 有没有起作用</a>" ;
}
else{
echo "<p>读到的cookie username 值为: $username<br>";
echo "有效期1分钟,1分钟后再刷新本页面就会看不到了<br>" ;
</a>" ;
}

?>
</body>
</html>

<?php
if (!isset($__session_inc__)){
$__session_inc__=1;
//require("cookie.inc.php3");
# -------------------------------------------------------------------
# session management v1.0 21.6.1998
# (c) wild karl heinz <kh.wild@wicom.at>
#
# this include handle session based variable handling
#
# please feel free and use it. if you make it more functional
# it would be nice to send me a copy.
#
# don't forget - mysql_connect !
#
# the database structure
# table structure for table 'session'
#
# create table session (
# id int(11) default '0' not null auto_increment,
# sid varchar(20) default '' not null,
# val blob,
# times timestamp(14),
# primary key (id),
# key sid (sid),
# unique sid_2 (sid)
# );
#
# you'll miss here a cron job to delete the old sessions from db
# -------------------------------------------------------------------

// 请注意上面被注释掉的create table语句,
// 你需要在你所使用的数据库上执行这条语句,
// 表名也可以不是session,那么就需要设置下面的$sess_table变量了。

// 此处你需要设置库名,和表名。
// 不过一般建议就使用session作为表名
 $sess_db = 'dbname';
 $sess_table = 'session';
 
# ----------------------------------------------------
# session_checkid - 检查、设置并返回 session-id
# 参数......: cookie保存时间(以分钟计)
#  也可不设置表示这个 cookie 只在当前session 有效
#  这其实就象asp中session的时效一样。
# 返回值....: 一个唯一的session-id (作为cookie存储)
# ----------------------------------------------------
function session_checkid( $min )
{
global $sess_sid;

if( !$sess_sid ) {
$sess_sid = uniqid( sc ); //取得一个唯一的随机数
/*
if( $min > 0 ) {
 setcookie("sess_sid", $sess_sid, time()+($min*60), "/", "", 0 );
 }
else {
 setcookie("sess_sid", $sess_sid, "", "/", "", 0 );
 }
上面是原先的代码,会出错。所以另外用了一个更好的函数。
函数库:cookie.inc.php3
*/
jssetcookie("sess_sid",$sess_sid,$min);
return( false );
}
 else {
return( true );
}
}

# ----------------------------------------------------------
# str2arr - 将字符串转换成session数组
# 参数.....: string
# 返回值...: 全局数组(其实就是session)
#本函数用途:将字符串转换成session数组
#如"session[username]=yourid&session[userpass]=12345"
#将会被转换成下面的数组
# session[username]="yourid"
# session[userpass]="12345"
#请注意函数split(),each(),list(),eval()的用法。
# ----------------------------------------------------------
function str2arr( $ts )
{
 global $session;

 $vals = split( "&", $ts );
 while( list($key,$val) = each($vals) ) {
list( $name, $wert ) = split( "=", $val );
if( $val ) eval( "\$$name = \"$wert\";" );
}
}

# ----------------------------------------------------------
# session_read() - 从session表中取数据,转换成session数组
# 参数........: 无
# 返回值......: 如果读出数据,返回 true ,否则返回 false
#注意.........: 用到了str2arr()这个函数
# ----------------------------------------------------------
function session_read()
{
 # hash array to keep session-variables
 global $session;
 global $sess_sid, $sess_db, $sess_table, $sess_error;

 $sel = "select val from $sess_table where sid = '$sess_sid'";
 $res = mysql_db_query( $sess_db, $sel );
 if( mysql_numrows( $res ) ) {
$val = mysql_result( $res, 0, "val" );
str2arr( $val );
mysql_free_result( $res );
return( true );
}
 else {
return( false );
$sess_error = mysql_error();
}
}

# ------------------------------------------------------
# split_array() - 将session数组转换成字符串
# 参数.......: 数组
# 返回值.....: 数组转换得来的字符串
#
# thanks to rasmus (这人好象是php的发明人)
# 注意:将session数组转换成字符串
#如session[username]="yourid"
# session[userpass]="12345"
#将会被转换成"session[username]=yourid&session[userpass]=12345"
#同时该函数考虑到了数组的某个元素也是数据的情况
#这个函数被设计成一个递归函数
# ------------------------------------------------------
function split_array( $arr, $a = "", $b = "", $c = "" )
{
 while( list( $key, $val ) = each( $arr ) ) {
if( is_array( $val ) ) {
 $ts .= split_array( $arr[ $key ],
( strlen( $a ) ? $a : $key ),
( strlen( $b ) ? $b : ( strlen( $a ) ? $key : "" ) ),
( strlen( $c ) ? $c : ( strlen( $b ) ? $key : "" ) ) );
 }
else {
 $ts .= "session";
 $ts .= $a ? "[$a]" : "";
 $ts .= $b ? "[$b]" : "";
 $ts .= $c ? "[$c]" : "";
 $ts .= "[$key]=$val&";
 }
}
 return( $ts );
}

# ---------------------------------------------------
# session_write - 将session数组转换成字符串,再存到session表中
# 参数.: 无
# 返回值...: 如果存入正常返回 true ,否则返回 false
# ---------------------------------------------------
function session_write()
{
 # hash array to keep session-variables
 global $session;

 global $sess_sid, $sess_db, $sess_table;
 global $sess_error;

 # if you like to delete a session-cookie
 # you must check it before writting the session
 # array

 if( !$sess_sid ) { session_checkid( 0 ); }

 $ts = split_array( $session );
 if( $ts > "" ) { $ts = substr( $ts, 0, strlen( $ts ) - 1 ); }
 $res = mysql_db_query( $sess_db, "select * from session where sid = '$sess_s'");
 if( mysql_numrows( $res ) == 0 ) {
$sel = "insert into $sess_table ( id, sid, val, times ) ";
$sel .= "values( 0, '$sess_sid', '$ts', null )";
}
 else {
$sel = "update $sess_table set val = '$ts', ";
$sel .= "times = null where sid = '$sess_sid'";
}
 if( !mysql_db_query( $sess_db, $sel ) ) {
$sess_error = mysql_error();
return( false );
}
 else { return( true ); }
}

# ---------------------------------------------
# session_del - 清除当前所有的session
#  并删除session表中和当前session有关的记录
# 参数.....: 一个随机的session id
# 返回值...: 无
# ---------------------------------------------
function session_del()
{
 global $session, $sess_db, $sess_table, $sess_sid;

 $sel = "delete from $sess_table where sid = '$sess_sid'";
 if( !mysql_db_query( $sess_db, $sel ) ) {
$sess_error = mysql_error();
}
 $sess_sid = '';
}
}
?> 

<?php
if (!isset($__cookie_inc__)){
$__cookie_inc__=1;
function jssetcookie($cname,$cvalue,$cexpr=false){
// 这个函数允许你在html头标记之后设置cookie ,
// 可以作setcookie函数的补充,甚至代替。
// $cname.....: cookie 的名字
// $cvalue....: cookie 的值
// $cexpr.....: cookie 的有效期,以分钟为单位,也可以修改加入小时,天数

if($cexpr > 0){
$cookiestring="astr= '$cname' + '=' + '$cvalue' + ';expires=' + expr + ';path=/';";
$cookie.="\n<script language=\"javascript\">\n";
$cookie.='function makeyearexpdate(min){
var expire = new date();
expire.settime(expire.gettime() + ((min * 60) * 1000));
expire = expire.togmtstring()
return expire
}
expr =makeyearexpdate('.$cexpr.');';
$cookie.="\n".$cookiestring."\n";
$cookie.="document.cookie=astr;\n</script>\n";
}else{
$cookie.="\n<script language=\"javascript\">\n";
$cookie.="document.cookie='$cname=$cvalue;path=/';";
$cookie.="\n</script>\n";
}
echo $cookie;
}
}
?>

<?php require( "session.inc.php3");
require("cookie.inc.php3");
?>
<?php
 session_checkid( 20 ); //20分钟后session失效
//下面你需要设置mysql的连接参数
mysql_connect('localhost','user','pass') or die("can't connect to db!");
?>
<!doctype html public "-//w3c//dtd html 3.2 final//en">
<html>
<head>
<title>session/cookie-测试</title>
</head>
<body>
<h2>this page should show how to handle the "session.inc.php3" library</h2>
<h3>we will use a mask with a record showing routine</h3>
<?php
if( $show ) {
if( session_read() ) {
$username = $session[username];
$userpass = $session[userpass];
echo "<p>session[username]:$username<br>session[userpass]:$userpass";
}
}
else{
$session[username]="yourid";
$session[userpass]="12345";
if( !session_write() ) {
print $sess_error;
}else{
echo "<p>session[username]被设置成:$session[username]<br>" ;
echo "session[userpass]被设置成:$session[userpass]<br>" ;
echo "<a href='$path_info?show=1'>测试一下session的作用</a>" ;
}
}
?>

</body>
</html>

 本文Tagssession  
 收藏本文  打印本文  论坛讨论  关闭窗口
· 上一篇:require()和include()的区别
· 下一篇:怎样校验输入的日期为合法的日期
· PHP 编码规范(18)
· PHP/MySQL三日通-第三天(二)
· PHP5 的对象模型 -- Classes and Objects in PHP5
· 一个获取远端文件的函数(Linux和Windows均适用)
· mSQL 数据库函数库


关于本站 | 联系我们 | 业务合作 | 客户案例 | 诚聘英才 | 广告合作 | 收藏本站
海口动网先锋网络科技有限公司版权所有
Copyright © 2000 - 2006 Cndw.Com
中华人民共和国电信与信息服务业务经营许可证编号 琼 ICP 020077