• <ruby id="5koa6"></ruby>
    <ruby id="5koa6"><option id="5koa6"><thead id="5koa6"></thead></option></ruby>

    <progress id="5koa6"></progress>

  • <strong id="5koa6"></strong>
    • 軟件測試技術
    • 軟件測試博客
    • 軟件測試視頻
    • 開源軟件測試技術
    • 軟件測試論壇
    • 軟件測試沙龍
    • 軟件測試資料下載
    • 軟件測試雜志
    • 軟件測試人才招聘
      暫時沒有公告

    字號: | 推薦給好友 上一篇 | 下一篇

    在PHP3中實現SESSION的功能

    發布: 2007-7-14 19:53 | 作者: 佚名    | 來源: 網絡轉載     | 查看: 21次 | 進入軟件測試論壇討論

    領測軟件測試網 <?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>

    延伸閱讀

    文章來源于領測軟件測試網 http://www.kjueaiud.com/


    關于領測軟件測試網 | 領測軟件測試網合作伙伴 | 廣告服務 | 投稿指南 | 聯系我們 | 網站地圖 | 友情鏈接
    版權所有(C) 2003-2010 TestAge(領測軟件測試網)|領測國際科技(北京)有限公司|軟件測試工程師培訓網 All Rights Reserved
    北京市海淀區中關村南大街9號北京理工科技大廈1402室 京ICP備2023014753號-2
    技術支持和業務聯系:info@testage.com.cn 電話:010-51297073

    軟件測試 | 領測國際ISTQBISTQB官網TMMiTMMi認證國際軟件測試工程師認證領測軟件測試網

    老湿亚洲永久精品ww47香蕉图片_日韩欧美中文字幕北美法律_国产AV永久无码天堂影院_久久婷婷综合色丁香五月

  • <ruby id="5koa6"></ruby>
    <ruby id="5koa6"><option id="5koa6"><thead id="5koa6"></thead></option></ruby>

    <progress id="5koa6"></progress>

  • <strong id="5koa6"></strong>