喜悦国际村 
» 游客:  注册 | 登录 | 搜索 | 统计 | 帮助

RSS 订阅当前论坛  

喜悦证交所已经关闭

上一主题 下一主题
     
标题: 數據庫存放 session 的類  
 
meteor_shower
高级会员
Rank: 4
揮刀戲江湖譜寫情緣傳奇


UID 66798
精华 2
积分 977
帖子 913
金钱 947 喜悦币
威望 30
人脉 0
阅读权限 50
注册 2005-9-21
来自 深圳
状态 离线
[广告]: q m
數據庫存放 session 的類

my_session.inc.php

<?PHP

#Sezione 1: DATABASE
$_MYSESSION_CONF['DATABASE_TYPE']    = 'mysql';//Tipo DBMS (non ancora implementato)
#Sezione 1.1: Dati di connessione
$_MYSESSION_CONF['DB_DATABASE']      = 'dbtest';//Nome DB
$_MYSESSION_CONF['DB_PASSWORD']      = '';//Pass MySql
$_MYSESSION_CONF['DB_SERVER']        = 'localhost';//ServerMySql
$_MYSESSION_CONF['DB_USERNAME']      = 'root';//Utente MySql
#Sezione 1.2: Dati tabelle e colonne
$_MYSESSION_CONF['TB_NAME']          = 'my_session';
$_MYSESSION_CONF['SID']              = 'sid';
$_MYSESSION_CONF['NOME']             = 'nome';
$_MYSESSION_CONF['VALORE']           = 'valore';
$_MYSESSION_CONF['EXP']              = 'expires';

#Sezione 2: CONFIGURAZIONE SCRIPT

#Sezione 2.1: Configurazione Generale
$_MYSESSION_CONF['SID_LEN']          = 32//Lunghezza session_id
$_MYSESSION_CONF['DURATA']           = 1800//Durata della sessione [secondi]
$_MYSESSION_CONF['MAX_DURATA']       = 3600//Durata massima della sessione, dopo questo tempo la sessione ? comunque distrutta [secondi]
$_MYSESSION_CONF['SESSION_VAR_NAME'] = 'MY_PHPSESSID'//Nome della variabile di sessione

#Sezione 2.2: Cookie
$_MYSESSION_CONF['USE_COOKIE']       = false//Utilizzare i cookie per propagare la sessione?

#Sezione 2.3: Cifratura
$_MYSESSION_CONF['CRIPT']            = true//Utilizzare la cifratura dei dati nel DB?
$_MYSESSION_CONF['CRIPT_KEY']        = "aJ63Klg243wfs5vcvfGTqC7Moe3dbKec52h7LsQcvE345Gw2Dg621wd4Rwe21vfWWG5h2C5G4MD55CSIb2aLiAs49KmdluJ42Dr6"//La chiave per la cifratura
?>
2006-2-28 02:06 PM#1
查看资料  Blog  发短消息  QQ  顶部
 
meteor_shower
高级会员
Rank: 4
揮刀戲江湖譜寫情緣傳奇


UID 66798
精华 2
积分 977
帖子 913
金钱 947 喜悦币
威望 30
人脉 0
阅读权限 50
注册 2005-9-21
来自 深圳
状态 离线
[推荐阅读] 女孩都应该记住的几句英文2006-2-27 15:45:00 By 林℃小鱼
例1

<?php
include("./my_session.class.php");
include(
"./my_session.conf.php");

$session = new my_session($_MYSESSION_CONF);

echo 
"All session vars: <pre>";
print_r($session->VARS);
echo 
"</pre>";

echo 
'<br> Changing a variables: cane="bar", $session->registra("cane","bar"); <br>';
$session->registra("cane","bar");
echo 
'<br> Registering a new variables: gatto="fufi", $session->registra("gatto","fufi"); <br>';
$session->registra("gatto","fufi");

echo 
'<br>
<br>Only one session vars:<br>
Using the array: $session->VARS["cane"] -> cane = '
.$session->VARS["cane"].'<br>
Using the new method: $session->get_var("cane") -> cane = '
.$session->get_var("cane").'<br>
Using the array: $session->VARS["gatto"] -> gatto = '
.$session->VARS["gatto"].'<br>
Using the new method: $session->get_var("gatto") -> gatto = '
.$session->get_var("gatto");

echo 
'<br><br>Session_ID:'.$session->session_id.'<br>';

echo 
'<br><a href="./esempio.php?MY_PHPSESSID='.$_REQUEST["MY_PHPSESSID"].'">GO!</a>';
?>
2006-2-28 02:09 PM#2
查看资料  Blog  发短消息  QQ  顶部
 
meteor_shower
高级会员
Rank: 4
揮刀戲江湖譜寫情緣傳奇


UID 66798
精华 2
积分 977
帖子 913
金钱 947 喜悦币
威望 30
人脉 0
阅读权限 50
注册 2005-9-21
来自 深圳
状态 离线
[推荐阅读] 写了一段缓存页面的代码,不知道是不是多此一举
例2

<?

include("./my_session.class.php");
include(
"./my_session.conf.php");

$session = new my_session($_MYSESSION_CONF);

echo 
"All session vars: <pre>";
print_r($session->VARS);
echo 
"</pre>";


echo 
'<br> Registering a variable: cane="foo", $session->registra("cane","foo");<br> ';
$session->registra("cane","foo");
echo 
'<br> Deleting a variable: gatto, $session->cancella("gatto");<br> ';
$session->cancella("gatto");

echo 
'<br>
<br>Only one session vars:<br>
Using the array: $session->VARS["cane"] -> cane = '
.$session->VARS["cane"].'<br>
Using the new method: $session->get_var("cane") -> cane = '
.$session->get_var("cane").'<br>
Trying to access the deleted variables:<br>
Using the array: $session->VARS["gatto"] -> gatto = '
.$session->VARS["gatto"].'<br>
Using the new method: $session->get_var("gatto") -> gatto = '
.$session->get_var("gatto");



echo 
'<br><br>Session_ID:'.$session->session_id.'<br>';

echo 
'<br><a href="./esempio2.php?MY_PHPSESSID='.$_REQUEST["MY_PHPSESSID"].'">GO!</a>';
?>
2006-2-28 02:10 PM#3
查看资料  Blog  发短消息  QQ  顶部
 
meteor_shower
高级会员
Rank: 4
揮刀戲江湖譜寫情緣傳奇


UID 66798
精华 2
积分 977
帖子 913
金钱 947 喜悦币
威望 30
人脉 0
阅读权限 50
注册 2005-9-21
来自 深圳
状态 离线
[推荐阅读] 【招聘全职php程序员/网页设计美工】 -- 北京
說明﹕

Strating: Include the files "my_session.class.php" and "my_session.conf.php" in your script.

...
include("my_session.class.php");
include("my_session.conf.php");
...

Create a new object:

$session = new my_session($_MYSESSION_CONF);

doing this you start a new session or recover an old one automatically.
Your session var are stored in the array: $session->VARS["nome"].
After this step, the session id are stored in the global variable $_REQUEST["MY_SESSIONID"], this is the value the you have to append to the URL if you don't use cookie.

echo ' <a href="second_page.php?MY_SESSIONID= '.$_REQUEST["MY_SESSIONID"].' "> Example </a> ';


The variable's name is defined in the configuration file. [ "$_MYSESSION_CONF['SESSION_VAR_NAME']" ].

Registering a variabile:

$session->registra(NAME,VALUE);
es: $session->registra("cane","foo");

Retriving a variable from session:

$session->get_var(NAME);
es: $session->get_var("cane");
oppure
$session->VARS[NAME];
es: $session->VARS["cane"];

Deleting a session variables :

$session->registra(NAME);
es: $session->registra("cane");

Destroy the session:

$session->destroy()

The Config file:
The file is splitted in section. The fist on is the database section.

$_MYSESSION_CONF['DATABASE_TYPE'] type of the db, only mysql is supported
Connection Data:
$_MYSESSION_CONF['DB_DATABASE'] database name
$_MYSESSION_CONF['DB_PASSWORD'] database password
$_MYSESSION_CONF['DB_SERVER'] database server
$_MYSESSION_CONF['DB_USERNAME'] database user
Table data
$_MYSESSION_CONF['TB_NAME'] table name
$_MYSESSION_CONF['SID'] "sessiond id" column name
$_MYSESSION_CONF['NOME'] name of the column that store the name of the session vars
$_MYSESSION_CONF['VALORE'] name of the column that store the value of the session vars
$_MYSESSION_CONF['EXP'] name of the column that store the expire date of the session

CAUTION: If you are using the query from the mysql.sql file don't change the Table data

General Configuration
$_MYSESSION_CONF['SID_LEN'] the length of the univoque string user for the session_id
$_MYSESSION_CONF['DURATA'] duration of the session (seconds)
$_MYSESSION_CONF['MAX_DURATA'] max duration of the session (seconds). When this time is over the session will be destroyed
$_MYSESSION_CONF['SESSION_VAR_NAME'] name of the variable that store the session id (Ex: PHPSESSID).
$_MYSESSION_CONF['USE_COOKIE'] if true cookie are used to store the sessiond id, otherwise it is necessary to add to every links the session id. Es : index.php?MY_SESSIONID=$_REQUEST["MY_SESSIONID"]
$_MYSESSION_CONF['CRIPT'] if true all data stored in the database will be crypted.
$_MYSESSION_CONF['CRIPT_KEY'] the encrypt key.
2006-2-28 02:13 PM#4
查看资料  Blog  发短消息  QQ  顶部
 
php5
金牌会员
Rank: 6Rank: 6
中级会员


UID 62897
精华 0
积分 1263
帖子 1112
金钱 1263 喜悦币
威望 0
人脉 0
阅读权限 70
注册 2005-1-3
来自 福建
状态 离线
[推荐阅读] phpdig问题
先不说我用不用,且说我看到这大的文件,我心就寒了
2006-2-28 05:25 PM#5
查看资料  发短消息  顶部
 
shelly0577 (shelly0577)
金牌会员
Rank: 6Rank: 6
高级会员


UID 18493
精华 0
积分 1733
帖子 1736
金钱 1709 喜悦币
威望 0
人脉 24
阅读权限 70
注册 2003-2-21
状态 离线
[推荐阅读] 请教删除记录的方法。


<?php
/*
V3.00 6 Jan 2003  (c) 2000-2003 John Lim (jlim@natsoft.com.my). All rights reserved.
  Released under both BSD license and Lesser GPL library license. 
  Whenever there is any discrepancy between the two licenses, 
  the BSD license will take precedence.
      Set tabs to 4 for best viewing.
  
  Latest version of ADODB is available at [url]http://php.weblogs.com/adodb[/url]
  ======================================================================
  
 This file provides PHP4 session management using the ADODB database
wrapper library.
 
 Example
 =======
 
     GLOBAL $HTTP_SESSION_VARS;
    include('adodb.inc.php');
    include('adodb-session.php');
    session_start();
    session_register('AVAR');
    $HTTP_SESSION_VARS['AVAR'] += 1;
    print "<p>$HTTP_SESSION_VARS['AVAR']={$HTTP_SESSION_VARS['AVAR']}</p>";
    
To force non-persistent connections, call adodb_session_open first before session_start():

     GLOBAL $HTTP_SESSION_VARS;
    include('adodb.inc.php');
    include('adodb-session.php');
    adodb_session_open(false,false,false);
    session_start();
    session_register('AVAR');
    $HTTP_SESSION_VARS['AVAR'] += 1;
    print "<p>$HTTP_SESSION_VARS['AVAR']={$HTTP_SESSION_VARS['AVAR']}</p>";

 
 Installation
 ============
 1. Create this table in your database (syntax might vary depending on your db):
 
  create table sessions (
       SESSKEY char(32) not null,
       EXPIRY int(11) unsigned not null,
       DATA text not null,
      primary key (sesskey)
  );


  2. Then define the following parameters in this file:
      $ADODB_SESSION_DRIVER='database driver, eg. mysql or ibase';
    $ADODB_SESSION_CONNECT='server to connect to';
    $ADODB_SESSION_USER ='user';
    $ADODB_SESSION_PWD ='password';
    $ADODB_SESSION_DB ='database';
    $ADODB_SESSION_TBL = 'sessions'
    
  3. Recommended is PHP 4.0.6 or later. There are documented
     session bugs in earlier versions of PHP.

*/


if (!defined('_ADODB_LAYER')) {
    include (
dirname(__FILE__).'/adodb.inc.php');
}

if (!
defined('ADODB_SESSION')) {

 
define('ADODB_SESSION',1);
 
 
/* if database time and system time is difference is greater than this, then give warning */
 
define('ADODB_SESSION_SYNCH_SECS',60); 

/****************************************************************************************
    Global definitions
****************************************************************************************/
GLOBAL     $ADODB_SESSION_CONNECT
    
$ADODB_SESSION_DRIVER,
    
$ADODB_SESSION_USER,
    
$ADODB_SESSION_PWD,
    
$ADODB_SESSION_DB,
    
$ADODB_SESS_CONN,
    
$ADODB_SESS_LIFE,
    
$ADODB_SESS_DEBUG,
    
$ADODB_SESSION_CRC;
    
    
$ADODB_SESS_LIFE ini_get('session.gc_maxlifetime');
    if (
$ADODB_SESS_LIFE <= 1) {
     
// bug in PHP 4.0.3 pl 1  -- how about other versions?
     //print "<h3>Session Error: PHP.INI setting <i>session.gc_maxlifetime</i>not set: $ADODB_SESS_LIFE</h3>";
         
$ADODB_SESS_LIFE=1440;
    }
    
$ADODB_SESSION_CRC false;
    
//$ADODB_SESS_DEBUG = true;
    
    //////////////////////////////////
    /* SET THE FOLLOWING PARAMETERS */
    //////////////////////////////////
    
    
if (empty($ADODB_SESSION_DRIVER)) {
        
$ADODB_SESSION_DRIVER='mysql';
        
$ADODB_SESSION_CONNECT='localhost';
        
$ADODB_SESSION_USER ='root';
        
$ADODB_SESSION_PWD ='';
        
$ADODB_SESSION_DB ='session';
    }
    
    
//  Made table name configurable - by David Johnson [email]djohnson@inpro.net[/email]
    
if (empty($ADODB_SESSION_TBL)){
        
$ADODB_SESSION_TBL 'db-session';
    }

/****************************************************************************************
    Create the connection to the database. 
    
    If $ADODB_SESS_CONN already exists, reuse that connection
****************************************************************************************/
function adodb_sess_open($save_path$session_name,$persist=true
{
GLOBAL 
$ADODB_SESS_CONN;
    if (isset(
$ADODB_SESS_CONN)) return true;
    
GLOBAL     
$ADODB_SESSION_CONNECT
    
$ADODB_SESSION_DRIVER,
    
$ADODB_SESSION_USER,
    
$ADODB_SESSION_PWD,
    
$ADODB_SESSION_DB,
    
$ADODB_SESS_DEBUG;
    
    
// cannot use & below - do not know why...
    
$ADODB_SESS_CONN ADONewConnection($ADODB_SESSION_DRIVER);

    if (!empty(
$ADODB_SESS_DEBUG)) {
        
$ADODB_SESS_CONN->debug true;
        
ADOConnection::outp" conn=$ADODB_SESSION_CONNECT user=$ADODB_SESSION_USER pwd=$ADODB_SESSION_PWD db=$ADODB_SESSION_DB ");
    }

    if (
$persist$ok $ADODB_SESS_CONN->PConnect($ADODB_SESSION_CONNECT,
            
$ADODB_SESSION_USER,$ADODB_SESSION_PWD,$ADODB_SESSION_DB);
    else 
$ok $ADODB_SESS_CONN->Connect($ADODB_SESSION_CONNECT,
            
$ADODB_SESSION_USER,$ADODB_SESSION_PWD,$ADODB_SESSION_DB);
    
    if (!
$okADOConnection::outp"<p>Session: connection failed</p>",false);
}

/****************************************************************************************
    Close the connection
****************************************************************************************/
function adodb_sess_close() 
{
global 
$ADODB_SESS_CONN;

    if (
$ADODB_SESS_CONN$ADODB_SESS_CONN->Close();
    return 
true;
}

/****************************************************************************************
    Slurp in the session variables and return the serialized string
****************************************************************************************/
function adodb_sess_read($key
{
global 
$ADODB_SESS_CONN,$ADODB_SESSION_TBL,$ADODB_SESSION_CRC;

    
$rs $ADODB_SESS_CONN->Execute("SELECT data FROM $ADODB_SESSION_TBL WHERE sesskey = '$key' AND expiry >= " time());
    if (
$rs) {
        if (
$rs->EOF) {
            
$v '';
        } else 
            
$v rawurldecode(reset($rs->fields));
            
        
$rs->Close();
        
        
// new optimization adodb 2.1
        
$ADODB_SESSION_CRC strlen($v).crc32($v);
        
        return 
$v;
    }
    
    return 
''// thx to Jorma Tuomainen, webmaster#wizactive.com
}

/****************************************************************************************
    Write the serialized data to a database.
    
    If the data has not been modified since adodb_sess_read(), we do not write.
****************************************************************************************/
function adodb_sess_write($key$val
{
    global
        
$ADODB_SESS_CONN
        
$ADODB_SESS_LIFE
        
$ADODB_SESSION_TBL,
        
$ADODB_SESS_DEBUG
        
$ADODB_SESSION_CRC;

    
$expiry time() + $ADODB_SESS_LIFE;
    
    
// crc32 optimization since adodb 2.1
    // now we only update expiry date, thx to sebastian thom in adodb 2.32
    
if ($ADODB_SESSION_CRC !== false && $ADODB_SESSION_CRC == strlen($val).crc32($val)) {
        if (
$ADODB_SESS_DEBUG) echo "<p>Session: Only updating date - crc32 not changed</p>";
        
$qry "UPDATE $ADODB_SESSION_TBL SET expiry=$expiry WHERE sesskey='$key' AND expiry >= " time();
        
$rs $ADODB_SESS_CONN->Execute($qry);    
        return 
true;
    }
    
$val rawurlencode($val);
    
    
$rs $ADODB_SESS_CONN->Replace($ADODB_SESSION_TBL,
        array(
'sesskey' => $key'expiry' => $expiry'data' => $val),
        
'sesskey',$autoQuote true);
    
    if (!
$rs) {
        
ADOConnection::outp'<p>Session Replace: '.$ADODB_SESS_CONN->ErrorMsg().'</p>',false);
    }  else {
        
// bug in access driver (could be odbc?) means that info is not commited
        // properly unless select statement executed in Win2000
        
if ($ADODB_SESS_CONN->databaseType == 'access'
            
$rs $ADODB_SESS_CONN->Execute("select sesskey from $ADODB_SESSION_TBL WHERE sesskey='$key'");
    }
    return !empty(
$rs);
}

function 
adodb_sess_destroy($key
{
    global 
$ADODB_SESS_CONN$ADODB_SESSION_TBL;

    
$qry "DELETE FROM $ADODB_SESSION_TBL WHERE sesskey = '$key'";
    
$rs $ADODB_SESS_CONN->Execute($qry);
    return 
$rs true false;
}

function 
adodb_sess_gc($maxlifetime
{
    global 
$ADODB_SESS_DEBUG$ADODB_SESS_CONN$ADODB_SESSION_TBL;

    
$qry "DELETE FROM $ADODB_SESSION_TBL WHERE expiry < " time();
    
$ADODB_SESS_CONN->Execute($qry);

    if (
$ADODB_SESS_DEBUGADOConnection::outp("<p><b>Garbage Collection</b>: $qry</p>");
    
    
// suggested by Cameron, "GaM3R" <gamr@outworld.cx>
    
if (defined('ADODB_SESSION_OPTIMIZE')) {
    global 
$ADODB_SESSION_DRIVER;
    
        switch( 
$ADODB_SESSION_DRIVER ) {
            case 
'mysql':
            case 
'mysqlt':
                
$opt_qry 'OPTIMIZE TABLE '.$ADODB_SESSION_TBL;
                break;
            case 
'postgresql':
            case 
'postgresql7':
                
$opt_qry 'VACUUM '.$ADODB_SESSION_TBL;    
                break;
        }
        if (!empty(
$opt_qry)) {
            
$ADODB_SESS_CONN->Execute($opt_qry);
        }
    }
    
    
$rs $ADODB_SESS_CONN->SelectLimit('select '.$ADODB_SESS_CONN->sysTimeStamp.' from '$ADODB_SESSION_TBL,1);
    if (
$rs && !$rs->EOF) {
    
        
$dbt $rs->fields[0];
        
$rs->Close();
        
$dbt $ADODB_SESS_CONN->UnixTimeStamp($dbt);
        
$t time();
        if (
abs($dbt $t) >= ADODB_SESSION_SYNCH_SECS) {
        global 
$HTTP_SERVER_VARS;
            
$msg "adodb-session.php: Server time for webserver {$HTTP_SERVER_VARS['HTTP_HOST']} not in synch: database=$dbt, webserver=".$t;
            
error_log($msg);
            if (
$ADODB_SESS_DEBUGADOConnection::outp("<p>$msg</p>");
        }
    }
    
    return 
true;
}

session_module_name('user'); 
session_set_save_handler(
    
"adodb_sess_open",
    
"adodb_sess_close",
    
"adodb_sess_read",
    
"adodb_sess_write",
    
"adodb_sess_destroy",
    
"adodb_sess_gc");
}

/*  TEST SCRIPT -- UNCOMMENT */

if (0) {
GLOBAL 
$HTTP_SESSION_VARS;

    
session_start();
    
session_register('AVAR');
    
$HTTP_SESSION_VARS['AVAR'] += 1;
    
ADOConnection::outp"<p>$HTTP_SESSION_VARS['AVAR']={$HTTP_SESSION_VARS['AVAR']}</p>",false);
}

?>
2006-2-28 09:41 PM#6
查看资料  访问主页  Blog  发短消息  顶部
 
shelly0577 (shelly0577)
金牌会员
Rank: 6Rank: 6
高级会员


UID 18493
精华 0
积分 1733
帖子 1736
金钱 1709 喜悦币
威望 0
人脉 24
阅读权限 70
注册 2003-2-21
状态 离线
[推荐阅读] 求助 新手的奇怪问题


<?php
require "adodb.inc.php";
require 
"adodb-session.php";

session_start();
setcookie("PHPSESSID",session_id(),0,"","",0);    
Print_R($_COOKIE);
echo 
"foo: ".$_SESSION["foo"];
$_SESSION["foo"]++;
?>
2006-2-28 09:44 PM#7
查看资料  访问主页  Blog  发短消息  顶部
 
niexa123
注册会员
Rank: 2
中级会员



UID 50950
精华 0
积分 152
帖子 259
金钱 152 喜悦币
威望 0
人脉 0
阅读权限 20
注册 2004-11-18
状态 离线
[推荐阅读] Ajax事例
收下



http://www.ml188.org
2006-3-1 10:35 AM#8
查看资料  访问主页  Blog  发短消息  QQ  顶部
     


  可打印版本 | 推荐给朋友 | 订阅主题 | 收藏主题 | 开通个人空间  


 




Powered by Discuz! 6.1.0  © 2001-2010 Comsenz Inc.
Processed in 0.062996 second(s), 6 queries

(冀ICP备05009913号) 管理员:sadly 邮箱/MSN: sadly@phpx.com QQ:824008(长隐) 清除 Cookies - - Archiver - WAP