以上就是 Cookie 的基本工作原理。那么,Cookie 有哪些用途呢?最根本的用途是 Cookie 能够帮助 Web 站点保存有关访问者的信息。更概括地说,Cookie 是一种保持 Web 应用程序连续性(即执行“状态管理”)的方法。浏览器和 Web 服务器除了在短暂的实际信息交换阶段以外总是断开的,而用户向 Web 服务器发送的每个请求都是单独处理的,与其他所有请求无关。然而在大多数情况下,都有必要让 Web 服务器在您请求某个页面时对您进行识别。例如,购物站点上的 Web 服务器跟踪每个购物者,以便站点能够管理购物车和其他的用户相关信息。因此 Cookie 的作用就类似于名片,它提供了相关的标识信息,可以帮助应用程序确定如何继续执行。
使用 Cookie 能够达到多种目的,所有这些目的都是为了使 Web 站点记住您。例如,一个实施民意测验的站点可以简单地利用 Cookie 作为布尔值,表示您的浏览器是否已经参与了投票,从而避免您重复投票; 而那些要求用户登录的站点则可以通过 Cookie 来确定您是否已经登录过,这样您就不必每次都输入凭据。
有关 Cookie 的更多背景信息,建议您阅读 Verizon Web 站点中的“How Internet Cookies Work”一文,地址为 http://www22.verizon.com/about/community/learningcenter/articles/displayarticle1/0,4065,1022z1,00.html(英文)。其作者详细解释了什么是 Cookie 以及 Cookie 是如何在浏览器和服务器之间交换信息的,他还全面总结了 Cookie 涉及的隐私问题。
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here
Button1.Attributes.Add("onclick", "var st=window.showModalDialog('user.aspx?val='+document.all('TextBox1').value);document.all('TextBox2').value=st;return st;")
If IsPostBack Then
'RegisterStartupScript("t", "<script>document.all('TextBox2').value=window.returnValue;</script>")
End If
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
RegisterStartupScript("key", "<script>window.opener=null;window.close(this);</script>")
End Sub