Kohana之COOKIE及SESSION使用

Cookie使用:
<?php
echo Cookie::salt("abc","def");#得到保存到客户端的字符
#下面几个不解释~
echo Cookie::get("abc");
Cookie::set("abc","def");
Cookie::delete("abc");
 
SESSION使用
类型有三个 (native,cookie,database)
默认是native(就是本地,PHP默认的)
其中database 配置
在APP的config里建立一个session.php文件
内容为:
<?php
return array(
	'database' => array(
                 /**
		 * Database settings for session storage.
		 *
		 * string   group  configuation group name
		 * string   table  session table name
		 * integer  gc     number of requests before gc is invoked
		 * columns  array  custom column names
		 */
		'group'   => 'default',
		'table'   => 'sessions',
		'gc'      => 500,
		'columns' => array(
			/**
			 * session_id:  session identifier
			 * last_active: timestamp of the last activity
			 * contents:    serialized session data
			 */
			'session_id'  => 'session_id',
			'last_active' => 'last_active',
			'contents'    => 'contents'
		),
	),
);

建立数据表:(和你配置的数据库配置有关系,下面描述)
CREATE TABLE  `sessions` (
        `session_id` VARCHAR( 24 ) NOT NULL,
       `last_active` INT UNSIGNED NOT NULL,
        `contents` TEXT NOT NULL,
        PRIMARY KEY ( `session_id` ),
        INDEX ( `last_active` )
    ) ENGINE = MYISAM ;
其中cookie 配置
 
<?php

return array(
	'cookie' => array(
		'encrypted' => FALSE,//设置为true需要配置encrypted
	),
);
 encrypted 配置
在相同目录下建立encrypt.php
<?php
return array(
	"default"=>array(
		"key"=>"dddd",//你的密钥
		"mode"=>"ecb",//加密方式,在PHP网站可以找到
		"cipher"=>MCRYPT_RIJNDAEL_128,//加密方式,在PHP网站可以找到 
	)
);
 
如果使用native就最方便啦,不用配置
在这里随便提下数据库配置吧
在同目录下建立
database.php
<?php defined('SYSPATH') or die('No direct access allowed.');

return array
(
	'default' => array(
		'type'       => 'pdo',
		'connection' => array(
			/**
			 * The following options are available for PDO:
			 *
			 * string   dsn         Data Source Name
			 * string   username    database username
			 * string   password    database password
			 * boolean  persistent  use persistent connections?
			 */
			'dsn'        => 'mysql:host=localhost;dbname=****',
			'username'   => 'root',
			'password'   => '******',
			'persistent' => FALSE,
		),
		/**
		 * The following extra options are available for PDO:
		 *
		 * string   identifier  set the escaping identifier
		 */
		'table_prefix' => 'ko_',
		'charset'      => 'utf8',
		'caching'      => FALSE,
		'profiling'    => TRUE,
	),
);

以下是使用方式
<?php
Session::instance("cookie")->set("abc","abcd");
echo Session::instance("cookie")->get("abc");
Session::instance("cookie")->delete("abc");
Session::instance("cookie")->destroy();//清除SESSION


你可以把COOKIE去掉或者使用database试试,用database你会发现数据的sessions表会多一条记录

本地方式的话可以留空 即为:Session::instance();

修改Kohana_Session的$default为你想要的默认方式,除了在控制器头上设置不知道还有其他地方设置没,有点不方便,

在不修改它源代码的情况下~


 
 
 
posted @ 2010-08-19 10:29  liushan  阅读(1722)  评论(0)    收藏  举报