选择显示字体大小

在php3中实现session的功能(一、session函数库:session.inc.php3)(转译)


   <?php
if (!isset(&#36;__session_inc__)){
&#36;__session_inc__=1;
//require(&quot;cookie.inc.php3&quot;);
# -------------------------------------------------------------------
# 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&#39;t forget - mysql_connect !
#
# the database structure
# table structure for table &#39;session&#39;
#
#  create table session (
#    id int(11) default &#39;0&#39; not null auto_increment,
#    sid varchar(20) default &#39;&#39; not null,
#    val blob,
#    times timestamp(14),
#    primary key (id),
#    key sid (sid),
#    unique sid_2 (sid)
#  );
#
# you&#39;ll miss here a cron job to delete the old sessions from db
# -------------------------------------------------------------------

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

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

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

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

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

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

   &#36;sel = &quot;select val from &#36;sess_table where sid = &#39;&#36;sess_sid&#39;&quot;;
   &#36;res = mysql_db_query( &#36;sess_db, &#36;sel );
   if( mysql_numrows( &#36;res ) ) {
      &#36;val = mysql_result( &#36;res, 0, &quot;val&quot; );
      str2arr( &#36;val );
      mysql_free_result( &#36;res );
      return( true );
      }
   else {
      return( false );
      &#36;sess_error = mysql_error();
      }
}

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

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

   global &#36;sess_sid, &#36;sess_db, &#36;sess_table;
   global &#36;sess_error;

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

   if( !&#36;sess_sid ) { session_checkid( 0 ); }

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

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

   &#36;sel = &quot;delete from &#36;sess_table where sid = &#39;&#36;sess_sid&#39;&quot;;
   if( !mysql_db_query( &#36;sess_db, &#36;sel ) ) {
      &#36;sess_error = mysql_error();
      }
   &#36;sess_sid = &#39;&#39;;
}
}
?> 


 


关键字 本文所属关键字

相关 与本文相关文章

分类 所有文章关键字导航

源码编程相关

Java   Asp   PHP   .Net   XML   C/C++   CGI   VB   Jsp   J2ee   J2se   J2me   EJB   Servlet   Tomcat   Resin   Struts   Weblogic   Eclipse   ANT   GUI   JMS   Web servise   IDEA   Webphere   Hibernate   Spring   Jboss   Applet   Swing   Socket   Javamail   Perl   Ajax   P2P   安全   模式   框架   测试   开源   游戏

SQL数据库相关

My-SQL   Ms-SQL   Access   DB2   Oracle   Sybase   SQLserver   索引   存储过程   加密   数据库   分页   视图  

手机无线相关

3G   Wap   CDMA   GRPS   GSM   IVR   彩信   短信   无线   增值业务

网页设计制作相关

HTML   CSS   网页配色   网页特效   Javascript   VBscript   Dreamweaver   Frontpage   JS   Web   网站设计

网站建设推广相关

建站经验   网站优化   网站排名   推广   Alexa

操作系统/服务器相关

Windows XP   Windows 2000   Windows 2003   Windows Me   Windows 9.x   Linux   UNIX   注册表   操作系统   服务器   应用服务器

图形图像多媒体相关

Photoshop   Fireworks   Flash   Coreldraw   Illustrator   Freehand   Photoimpact   多媒体   图形图像

标准 网站致力的规范

Valid CSS!

无不良内容,无不良广告,无恶意代码

Valid XHTML 1.0 Transitional

creativecommons