クッキー

□未翻訳

□翻訳中

□翻訳完了(細田謙二)

■レビュー(中垣健志)

クッキー

web2pyはPythonのクッキー・モジュールを、クッキーを処理するために使用します。

web2py uses the Python cookies modules for handling cookies.

ブラウザからのクッキーはrequest.cookiesにあり、サーバから送られるクッキーはresponse.cookiesにあります。

Cookies from the browser are in request.cookies and cookies sent by the server are inresponse.cookies.

クッキーは次のように設定することができます:

You can set a cookie as follows:

1.

2.

3.

response.cookies['mycookie'] = 'somevalue'

response.cookies['mycookie']['expires'] = 24 * 3600

response.cookies['mycookie']['path'] = '/'

2行目は、ブラウザにクッキーを24時間保持するように伝えます。3行目は、現在のドメインの任意のアプリケーション(URLパス)にクッキーを返送するようにブラウザに指示します。

The second line tells the browser to keep the cookie for 24 hours. The third line tells the browser to send the cookie back to any application (URL path) at the current domain.

クッキーは、次のように保護にすることができます:

The cookie can be made secure with:

1.

response.cookies['mycookie']['secure'] = True

保護されたクッキーは、HTTPではなくHTTPS経由でしか返送されません。

A secure cookie is only sent back over HTTPS and not over HTTP.

クッキーは次のようにして取り出すことができます:

The cookie can be retrieved with:

1.

2.

if request.cookies.has_key('mycookie'):

value = request.cookies['mycookie'].value

セッションが無効になってない限り、web2pyの内部では、次のようにクッキーを設定し、セッションを扱うために使用します:

Unless sessions are disabled, web2py, under the hood, sets the following cookie and uses it to handle sessions:

1.

2.

response.cookies[response.session_id_name] = response.session_id

response.cookies[response.session_id_name]['path'] = "/"

単一のアプリケーションが複数のサブドメインを含み、セッションをそれらのサブドメインで共有したい場合は(たとえば、sub1.yourdomain.com、sub2.yourdomain.comなど)、次のようにセッションのドメインを明示的に設定する必要があることに注意してください。

Note, if a single application includes multiple subdomains, and you want to share the session across those subdomains (e.g., sub1.yourdomain.com, sub2.yourdomain.com, etc.), you must explicitly set the domain of the session cookie as follows:

1.

2.

if not request.env.remote_addr in ['127.0.0.1', 'localhost']:

response.cookies[response.session_id_name]['domain'] = ".yourdomain.com"

これは、たとえば、ユーザーにサブドメイン間でログインを維持させるような場合に便利です。

The above can be useful if, for example, you want to allow the user to remain logged in across subdomains.