laravel中使用cookie详解
2017-05-19 09:52
399 查看
前面的话:
Laravel会加密所有已创建的cookie信息,并附加上授权码,当客户端擅自修改cookie信息时,该cookie将被废弃,从而保证安全性。
1.Cookie::make() 添加一个新的cookie 、Cookie::forever() 创建一个永不过期的cookie键值、Cookie::get() 获取一个指定的cookie值:
[php] view plain copy
Route::get('cookieset', function()
{
$foreverCookie = Cookie::forever('forever', 'Success');
$tempCookie = Cookie::make('temporary', 'Victory', 5);
return Response::make()->withCookie($foreverCookie)->withCookie($tempCookie);
});
Route::get('cookietest', function()
{
$forever = Cookie::get('forever');
$temporary = Cookie::get('temporary');
return View::make('cookietest', array('forever' => $forever, 'temporary' => $temporary, 'variableTest' => 'works'));
});
注意:
laravel5.2使用make有点问题
需要使用是可以的
Cookie::queue('GobackUrl', $url, 30);
有些时候,你想设置一个 Cookie 是不加密的,例如想用来跟 JS 交互,JS 可以直接读取到明文,可是以下面的代码设置后:
解决方案# 如你cookie 键值为cookie_for_js的需要与前台交互 添加到 App\Http\Middleware\EncryptCookies 的 排除名单 中:[php] view plain copy
<?php
namespace App\Http\Middleware;
use Illuminate\Cookie\Middleware\EncryptCookies as BaseEncrypter;
class EncryptCookies extends BaseEncrypter
{
/**
* The names of the cookies that should not be encrypted.
*
* @var array
*/
protected $except = [
'cookie_for_js',
];
}
对了,如果要给 JS 读取的话,要把 cookie 的
http only属性去掉,请使用以下代码:[php] view plain copy
Cookie::queue('cookie_for_js', 'can you read me?', $minutes = 99999999, $path = null, $domain = null, $secure = false, $httpOnly = false);
2、Cookie存储数组:[php] view plain copy
Route::get('cookieset', function()
{
$user_info = array('name'=>'good','age'=>12);
$user = Cookie::make('user',$user_info,30);
return Response::make()->withCookie($user);
});
Route::get('cookietest', function()
{
dd(Cookie::get('user'));
});
相关文章推荐
- laravel中使用cookie详解
- JSP Cookie使用方法详解
- JSP开发中Cookie使用完全详解
- python使用cookie库操保存cookie详解
- CodeIgniter中使用cookie的三种方式详解
- CodeIgniter中使用cookie的三种方式详解
- web 开发中使用cookie 详解
- JSP Cookie 使用完全详解
- JSP Cookie 使用完全详解
- 使用cookie做用户登录的过程详解
- js中使用Cookie详解和实例
- JSP开发中Cookie使用完全详解
- PHP5中Cookie与 Session使用详解
- js cookie使用方法详解
- Yii框架 Cookie的使用方法详解
- JSP Cookie 使用完全详解
- JSP Cookie 使用完全详解
- PHP5中Cookie与 Session使用详解
- JSP Cookie使用方法详解
- laravel cookie的使用方法