第一次访问页面后如何删除cookie,Server可将Cookie信息加入返回给浏览器,浏览器会自动将这些信息保存在本地;
当再次访问相同域名时,浏览器将对应Cookie信息随请求一起发送,Server端可读取这些信息进行处理。
Cookie可设置过期时间,到期后由浏览器自动删除;
当我们要返回页面时,首先输出如下:
1
print \"Content-type: text/plain\\n\"
它其实是一个, 连续碰到两个换行表示部分结束,剩余部分将被做为数据处理;
包含了Cookie后输出大致如下:
1
2
3
print \"Content-Type: text/html\"
print \"Set-Cookie: session=12345\"
print # 多加一个换行表示部分结束
代码:
setcookie.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#!/usr/bin/env python
import Cookie
import datetime
import random
expiration = datetime.datetime.now() + datetime.timedelta(days=30)
cookie = Cookie.SimpleCookie()
cookie[\"session\"] = random.randint(0,1000000000)
cookie[\"session\"][\"domain\"] = \"localhost\"
cookie[\"session\"][\"path\"] = \"/\"
cookie[\"session\"][\"expires\"] = \\
expiration.strftime(\"%a, %d-%b-%Y %H:%M:%S PST\")
print \"Content-type: text/plain\"
print cookie.output()
print \"Cookie set with: \" + cookie.output()
getcookie.py
1
2
3
4
5
6
7
8
9
10
11
12
#!/usr/bin/env python
import Cookie
import os
print \"Content-type: text/plain\\n\"
try:
cookie = Cookie.SimpleCookie(os.environ[\"\"])
print \"session = \" + cookie[\"session\"].value
except (Cookie.CookieError, KeyError):
print \"session cookie not set!\"
用js怎么实现 点击[退出]后 删除cookie 记录???
<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"\">
<html xmlns=\"\">
<head>
<meta =\"Content-Type\" content=\"text/html; charset=utf-8\" />
<title>无标题文档</title>
</head>
<body>
<SCRIPT LANGUAGE=\"JavaScript\">
<!--
function on_del_click(cookie_name,obj)
{
//这里本人不知道怎么写
obj.parentNode.removeChild(obj)
document.cookie = cookie_name+\"=\";
//deleteCookie(cookie_name);
window.open(\"123.asp\", \"_self\", \"\");
return false;
}
//-->
</SCRIPT>
<form action=\'ok.asp\' method=\'post\'>
aaa:<input type=\'button\' onclick=\'return on_del_click(\"aaa\",this);\' value=\"aaa\"><br>
bbb:<input type=\'button\' onclick=\'return on_del_click(\"bbb\",this);\' value=\"bbb\"><br>
ccc:<input type=\'button\' onclick=\'return on_del_click(\"ccc\",this);\' value=\"ccc\">
</form>
</body>
</html>