Cookie Session Redius

本文深入解析了Cookie的工作原理,包括其如何被服务器发送到客户端并存储,以及在自动登录、浏览记录和购物车等功能中的应用。文章还探讨了Cookie在提升用户体验和收集用户习惯方面的重要性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Cookie


介绍:

Cookie(小饼干,代表一份小数据),是服务器传递给客户端,并且存储在客户端上的一份小数据。

应用场景:

自动登录,浏览记录,购物车   

为什么用它:

Http的请求是无状态的,当客户端与服务器端进行通讯,客户端第二次来访,服务器不知道这个客户端有没有访问过。那么基于用户体验(自动登录),基于公司,收集用户习惯,就要使用Cookie。

	@RequestMapping("cookie")
	public String getCookie(HttpServletResponse response) {
		
		Cookie cookie = new Cookie("name", "cuckoo");
		response.setContentType("text/html;charset=utf-8");
		response.addCookie(cookie);
		try {
			response.getWriter().write("返回cookie成功");
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		return null;
	}

 

以原生php模板而不是html模板改写下文 <!DOCTYPE html> <html> <head> <title>调试信息</title> <style> .debug-section { margin: 20px 0; padding: 15px; border: 1px solid #ddd; border-radius: 5px; background-color: #f9f9f9; } .debug-section h2 { margin-top: 0; color: #333; border-bottom: 1px solid #eee; padding-bottom: 10px; } .debug-item { margin-bottom: 15px; padding: 10px; background-color: #fff; border: 1px solid #eee; border-radius: 3px; } .debug-item pre { background-color: #f5f5f5; padding: 10px; border-radius: 3px; overflow-x: auto; } .empty-message { color: #999; font-style: italic; } </style> </head> <body> <h1>系统调试信息</h1> <!-- Session 数据 --> <div class="debug-section"> <h2>Session 数据</h2> {if empty($sessionData)} <p class="empty-message">Session is empty</p> {else} {foreach $sessionData as $name => $value} <div class="debug-item"> <strong>Session Name:</strong> {$name|htmlspecialchars}<br> <strong>Value:</strong> <pre>{$value|print_r|htmlspecialchars}</pre> </div> {/foreach} {/if} </div> <!-- 普通 Cookie 数据 --> <div class="debug-section"> <h2>普通 Cookie 数据</h2> {if empty($cookieData)} <p class="empty-message">No cookies found</p> {else} {foreach $cookieData as $name => $value} {if $name != 'waxx' || !is_array($value)} <div class="debug-item"> <strong>Cookie Name:</strong> {$name|htmlspecialchars}<br> <strong>Value:</strong> <pre>{$value|print_r|htmlspecialchars}</pre> </div> {/if} {/foreach} {/if} </div> <!-- 原站点数组形式 Cookie --> <div class="debug-section"> <h2>原站点数组形式 Cookie (waxx)</h2> {if empty($waxxCookie)} <p class="empty-message">No waxx cookies found</p> {else} {foreach $waxxCookie as $key => $value} <div class="debug-item"> <strong>Cookie Name:</strong> waxx[{$key|htmlspecialchars}]<br> <strong>Value:</strong> <pre>{$value|print_r|htmlspecialchars}</pre> </div> {/foreach} {/if} </div> </body> </html>
最新发布
07-09
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值