Yii2中cookie用法示例分析
发布时间:2020-12-12 21:38:09 所属栏目:PHP教程 来源:网络整理
导读:本篇章节讲解Yii2中cookie用法。供大家参考研究具体如下: 'cookie_monster','value' => 'Me want cookie!','expire' => time() + 86400 * 365,]);Yii::$app->getResponse()->getCookies()->add($cookie);//读取方法$value = Yii::$app->getRequ
本篇章节讲解Yii2中cookie用法。分享给大家供大家参考,具体如下: 'cookie_monster','value' => 'Me want cookie!','expire' => time() + 86400 * 365,]);
Yii::$app->getResponse()->getCookies()->add($cookie);
//读取方法
$value = Yii::$app->getRequest()->getCookies()->getValue('my_cookie');
//给cookie加域名
$cookie = new Cookie([
'name' => 'cookie_monster','value' => 'Me want cookie everywhere!','domain' => '.example.com' // <<<=== HERE
]);
Yii::$app->getResponse()->getCookies()->add($cookie);
//设置登录cookie
$config = [
// ...
'components' => [
// ...
'user' => [
'class' => 'yiiwebUser','identityClass' => 'appmodelsUser','enableAutoLogin' => true,'loginUrl' => '/user/login','identityCookie' => [ // <---- here!
'name' => '_identity','httpOnly' => true,'domain' => '.example.com',],'request' => [
'cookieValidationKey' => 'your_validation_key'
],'session' => [
'cookieParams' => [
'domain' => '.example.com',];
//只给批定目录配置cookie
$config = [
// ...
'components' => [
// ...
'session' => [
'name' => 'admin_session','cookieParams' => [
'httpOnly' => true,'path' => '/admin',];
?>
更多关于Yii相关内容感兴趣的读者可查看本站专题:《》、《》、《》、《》、《》、《》及《》 希望本文所述对大家基于Yii框架的PHP程序设计有所帮助。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |