CGI is used Cookie

Is not the user's identity in the http protocol a big disadvantage judgment, such a great deal of inconvenience to the programmer, and the emergence cookie functionality made up for this deficiency.

cookie that is, while customers access the script, by the customer's browser, written on the client hard disk record data, retrieve data when customers visit next script, so as to achieve identity discrimination function, the cookie is often used in identity verification.

 

cookie syntax

Send http cookie is achieved through http head, he had to transfer files, set-cookie header syntax is as follows:

Set-cookie:name=name;expires=date;path=path;domain=domain;secure 
  • name = name:  to set the value of the cookie (name can not be used " ; " and " , " number), name when a plurality of values " ; " separated, for example: NAME1 = NAME1; NAME2 = NAME2; NAME3 = NAME3 .
  • DATE = Expires:  Cookie expiration date format: expires = "Wdy, DD- Mon-YYYY HH: MM: SS"
  • path = path:  Set the path to cookie support, if path is a path, the cookie of all files and subdirectories in this directory into force, for example: path = "/ cgi-bin /", if the path is a file, cookie It refers to the entry into force of this file, for example: path = "/ cgi-bin / cookie.cgi".
  • domain = domain:  the domain name cookie is valid, such as: domain = "www.runoob.com"
  • secure:  If this flag is given that the cookie can only be transferred through https server SSL protocol.
  • cookie is received by setting environment variables HTTP_COOKIE achieved, CGI program can access information by searching the cookie variable.

Cookie settings

Cookie's very easy to set, cookie will be sent separately at http header. The following examples set name and expires in a cookie:

 

#!/usr/bin/python
# -*- coding: UTF-8 -*-
# 
print 'Content-Type: text/html'
print 'Set-Cookie: name="菜鸟教程";expires=Wed, 28 Aug 2016 18:30:00 GMT'
print
print """
<html>
    <head>
        <meta charset="utf-8">
        <title>菜鸟教程(runoob.com)</title>
    </head>
    <body>
        <h1>Cookie set OK!</h1>
    </body>
</html>
"""

 

Guess you like

Origin www.cnblogs.com/furuihua/p/11289654.html