Php 简明教程

PHP - Session Options

从 PHP 7 开始, session_start() 函数接受一个选项数组来覆盖在“php.ini”中设置的会话配置指令。在“php.ini”中的 [session] 会话定义了各种选项的默认值。

From PHP version 7 onwards, the session_start() function accepts an array of options to override the session configuration directives set in "php.ini". The [session] session in "php.ini" defines the default values of various options.

如果提供了选项,它们将采用将覆盖当前设置的会话配置指令的关联选项数组的形式。键不应包含“session.”前缀。

The options, if provided, are in the form of an associative array of options that will override the currently set session configuration directives. The keys should not include the "session." prefix.

Example

例如,您可以使用 session_start() 函数的参数定义的两个会话选项来启动 HTTP 会话 −

For example, you may start the HTTP session with the two session options defined as the parameters of session_start() function −

<?php
   session_start([
      'cache_limiter' => 'private',
      'read_and_close' => true,
   ]);
?>

Configurable Options of an HTTP Session

PHP 中 HTTP 会话的一些可配置选项如下 −

Some of the configurable options of an HTTP session in PHP are as follows −

session.name

它指定用作 cookie 名称的会话名称。它应只包含字母数字字符。默认为 PHPSESSID。

It specifies the name of the session which is used as cookie name. It should only contain alphanumeric characters. Defaults to PHPSESSID.

session.save_handler

它定义用于存储和检索与会话关联的数据的处理程序的名称。默认为文件。

It defines the name of the handler which is used for storing and retrieving data associated with a session. Defaults to files.

session.auto_start

它指定会话模块是否在请求启动时自动启动会话。默认为 0(已禁用)。

It specifies whether the session module starts a session automatically on request startup. Defaults to 0 (disabled).

它指定发送到浏览器的 cookie 的生命周期(以秒为单位)。值 0 表示“直到浏览器关闭”。默认为 0。

It specifies the lifetime of the cookie in seconds which is sent to the browser. The value 0 means "until the browser is closed." Defaults to 0.

session.cache_limiter

它指定会话页面使用的缓存控制方法。它可以是以下值之一:nocache、private、private_no_expire 或 public。默认为 nocache。

It specifies the cache control method used for session pages. It may be one of the following values: nocache, private, private_no_expire, or public. Defaults to nocache.

session.sid_length

它允许您指定会话 ID 字符串的长度。会话 ID 长度可在 22 到 256 之间。默认值为 32。

It allows you to specify the length of session ID string. Session ID length can be between 22 to 256. The default is 32.

session.upload_progress.enabled

它启用上传进度跟踪,填充 $_SESSION 变量。默认值为 1,已启用。

It enables upload progress tracking, populating the $_SESSION variable. Defaults to 1, enabled.

session.lazy_write

将此值设为 1 时,这意味着仅当会话数据发生更改时才将其重写。默认值为 1,已启用。

When it is set to 1, it means that the session data is only rewritten if it changes. Defaults to 1, enabled.