nginx服务器怎样向cookies里面添加数据

nginx服务器怎样向cookies里面添加数据,第1张

server{

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'])


欢迎分享,转载请注明来源:内存溢出

原文地址: https://www.outofmemory.cn/bake/11745227.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-18
下一篇 2023-05-18

发表评论

登录后才能评论

评论列表(0条)

保存