在PHP3中实现SESSION的功能

来源: 作者: 2007-11-21 出处:pcdog.com

mysql  数据库  


<?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 ) ) {
   

上一篇:用PHP做一张xbm图片
下一篇:在PHP中得到多选的下拉菜单的各项值