简体   繁体   English

设置cookie后获取cookie

[英]get cookie after set cookie

I've set my cookie using set-cookie as follows on the server's cgi scripts 我在服务器的cgi脚本上使用set-cookie设置了cookie,如下所示

print "Content-type: text/html\n";
sys.stdout.write(cookie.output())
print "\n\n";

but when I reload the page I can't get the cookie as it doesn't appear on 但是当我重新加载页面时,由于没有出现在cookie上,所以我无法获取cookie

document.cookie

How do I get the cookies? 我如何获得饼干? and was Set-cookie : name=value supposed to appear on document.cookie after I refresh the page? 刷新页面后,Set-cookie:name = value应该出现在document.cookie上吗?

Overall I want the client side to recognize the cookie if exists and act accordingly 总体而言,我希望客户端识别Cookie(如果存在)并采取相应措施

Thanks 谢谢

W3Schools is explaining it beautifully. W3Schools对其进行了精美的解释。 You can have a look at the function getCookie and setCookie provided in the website. 您可以查看网站中提供的功能getCookie和setCookie。 You can use that as a base. 您可以以此为基础。 Its pure javascript 其纯JavaScript

Cookies are set out by the server. Cookie由服务器设置。 The client (browser) just sends them back. 客户端(浏览器)只是将它们发回。 You cannot use Javascript to set cookies. 您不能使用Javascript设置Cookie。

EDIT 编辑

You need fewer quotes and the \\r is not required. 您需要较少的引号,并且不需要\\r Probably need ; 大概需要; instead of , . 而不是, ie

print "Content-type: text/html\n";
print "Set-Cookie: name=value\n\n";

Also use HTTP live header add-on for Firefox to check the HTTP communication. 也可以使用Firefox的HTTP活动标头插件检查HTTP通信。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM