listen 80
add_header Set-Cookie 'CookieName=CookieValue'
}
简单的添加一个setcookie头就可以创建一个cookie,在当前请求的路径下创建了一个名为cookieName,值为CookieValue的cookie,为内存cookie,若要在其他位置创建,只需要加上path,改变生存期可以加expire
public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException
{
…………………………省略
// 为姓氏和名字创建Cookies
Cookie firstName = new Cookie("first_name",request.getParameter("first_name"))
Cookie lastName = new Cookie("last_name",request.getParameter("last_name"))
// 为两个 Cookies 设置过期日期为 24 小时后
firstName.setMaxAge(60*60*24)
lastName.setMaxAge(60*60*24)
// 在响应头中添加两个 Cookies
response.addCookie( firstName )
response.addCookie( lastName )
………………省略
}
$cookies = Yii::$app->response->cookies// add a new cookie to the response to be sent
$cookies->add(new \yii\web\Cookie([
'name' =>'username',
'value' =>'yiiuser',
]))
获取 Cookies
$cookies = Yii::$app->request->cookies
// get the cookie value
$username = $cookies->getValue('username')
//return default value if the cookie is not available
$username = $cookies->getValue('username', 'default')
// Check the availability of the cookie
if ($cookies->has('username'))
echo $cookies->getValue('username')
删除 Cookies
$cookies = Yii::$app->response->cookies
$cookies->remove('username')
unset($cookies['username'])
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)