前端js:
1,设置cookie: document.cookie = ‘name=zhangsan‘2,读取cookie: console.log(document.cookie)(直接读就行)3,删除cookie: 为了删除指定名称的cookie,可以将其过期时间设定为一个过去的时
//设置cookie function setCookie(cname, cvalue, exdays) { var d = new Date(); d.setTime(d.getTime() + (exdays*24*60*60*1000)); var expires = "expires="+d.toUTCString(); document.cookie = cname + "=" + cvalue + "; " + expires; } //获取cookie function getCookie(cname) { var name = cname + "="; var ca = document.cookie.split(‘;‘); for(var i=0; i<ca.length; i++) { var c = ca[i]; while (c.charAt(0)==‘ ‘) c = c.substring(1); if (c.indexOf(name) != -1) return c.substring(name.length, c.length); } return ""; } //清除cookie function clearCookie(name) { setCookie(name, "", -1); } function checkCookie() { var user = getCookie("username"); if (user != "") { alert("Welcome again " + user); } else { user = prompt("Please enter your name:", ""); if (user != "" && user != null) { setCookie("username", user, 365); } } } checkCookie();
后端Python(flask):
1,设置cookie: from flask import make_response @app.route(‘/‘) def index(): resp = make_response(render_template(...)) resp.set_cookie(‘username‘, ‘the username‘) return resp2,读取cookie: request.cookies.get(‘username‘)3,删除cookie: resp.set_cookie(‘username‘, ‘‘, expires=0)
另:
python—cookielib模块对cookies的操作
http://www.cnblogs.com/isuifeng/p/5903116.html 这个没看,不是很了解
时间: 2024-10-10 08:43:56