本文实例讲述了Yii框架 session 数据库存储操作方法。分享给大家供大家参考,具体如下:
在组件中声明session 组件开启数据库库存储
[ 'components' => [ 'db' => [ 'class' => 'yii\db\Connection', 'dsn' => 'mysql:host=localhost;dbname=yii', 'username' => 'root', 'password' => 'xxxxxx', 'charset' => 'utf8' ], 'session' => [ 'class' => 'yii\web\DbSession', //session 操作对象 'db' => 'db', //指定数据库操作组件是上面的组件db 'sessionTable' => 'yii_session' //session 数据库表名称 ] ] ]
yii_session 表结构
CREATE TABLE yii_session ( id CHAR(40) NOT NULL PRIMARY KEY, expire INTEGER, data BLOB )
控制器:
namespace app\controllers\home; use Yii; use yii\web\Controller; class IndexController { public function actionIndex() { $session = Yii::$app->session; if(isset($session['name']) && isset($session['tel'])) { var_dump($session); }else{ $session['name'] = '谭勇'; $session['tel'] = 22; echo 'set session'; } } }
测试 http://host/index.php?r=home/index/index
更多关于Yii相关内容感兴趣的读者可查看本站专题