mirror of
https://github.com/php/doc-zh.git
synced 2026-04-24 16:48:17 +02:00
8c2ec3127d
* Update book.xml * Update constants.xml * Update examples.xml * Update security.xml * Update upload-progress.xml * Update session-cache-expire.xml * Update session-cache-limiter.xml * Update session-destroy.xml * Update session-encode.xml * Update session-get-cookie-params.xml * Update session-id.xml * Update session-name.xml * Update session-regenerate-id.xml * Update session-save-path.xml * Update session-set-cookie-params.xml * Update session-set-save-handler.xml * Update session-start.xml * Update session-start.xml * Update session-status.xml * Update session-unset.xml * Update book.xml * Update session-cache-expire.xml * Update session-unset.xml
258 lines
7.4 KiB
XML
258 lines
7.4 KiB
XML
<?xml version="1.0" encoding="utf-8"?>
|
||
<!-- $Revision$ -->
|
||
<!-- EN-Revision: 151e61773c016edcae8fd4989ad9a86ffd03c283 Maintainer: yuanyuqiang Status: ready -->
|
||
<!-- CREDITS: mowangjuanzi -->
|
||
<refentry xml:id="function.session-start" xmlns="http://docbook.org/ns/docbook">
|
||
<refnamediv>
|
||
<refname>session_start</refname>
|
||
<refpurpose>启动新会话或者重用现有会话</refpurpose>
|
||
</refnamediv>
|
||
|
||
<refsect1 role="description">
|
||
&reftitle.description;
|
||
<methodsynopsis>
|
||
<type>bool</type><methodname>session_start</methodname>
|
||
<methodparam choice="opt"><type>array</type><parameter>options</parameter><initializer>[]</initializer></methodparam>
|
||
</methodsynopsis>
|
||
<para>
|
||
<function>session_start</function> 会创建新会话或者重用现有会话。
|
||
如果通过 GET 或者 POST 方式,或者使用 cookie 提交了会话 ID,
|
||
则会重用现有会话。
|
||
</para>
|
||
<para>
|
||
当会话自动开始或者通过 <function>session_start</function> 手动开始的时候,
|
||
PHP 内部会调用会话处理程序的 open 和 read 回调函数。
|
||
会话处理程序可能是 PHP 默认的,
|
||
也可能是扩展提供的(SQLite 或者 Memcached 扩展),
|
||
也可能是通过 <function>session_set_save_handler</function> 设定的用户自定义会话处理程序。
|
||
通过 read 回调函数返回的现有会话数据(使用特殊的序列化格式存储),
|
||
PHP 会自动反序列化数据并且填充 $_SESSION 超级全局变量。
|
||
</para>
|
||
<para>
|
||
要想使用命名会话,请在调用 <function>session_start</function> 函数
|
||
之前调用 <function>session_name</function> 函数。
|
||
</para>
|
||
<para>
|
||
如果启用了 <link linkend="ini.session.use-trans-sid">session.use_trans_sid</link> 选项,
|
||
<function>session_start</function> 函数会注册一个内部输出处理程序,
|
||
该输出处理程序完成 URL 重写的工作。
|
||
</para>
|
||
<para>
|
||
如果用户联合使用 <function>ob_start</function>
|
||
和 <literal>ob_gzhandler</literal> 函数,
|
||
那么函数的调用顺序会影响输出结果。
|
||
例如,必须在开始会话之前调用 <literal>ob_gzhandler</literal> 函数完成注册。
|
||
</para>
|
||
</refsect1>
|
||
|
||
<refsect1 role="parameters">
|
||
&reftitle.parameters;
|
||
<variablelist>
|
||
<varlistentry>
|
||
<term><parameter>options</parameter></term>
|
||
<listitem>
|
||
<para>
|
||
此参数是一个关联数组,如果提供,那么会用其中的项目覆盖 <link linkend="session.configuration">会话配置指示</link> 中的配置项。此数组中的键无需包含 <literal>session.</literal> 前缀。
|
||
</para>
|
||
<para>
|
||
除了常规的会话配置指示项,
|
||
还可以在此数组中包含 <literal>read_and_close</literal>
|
||
选项。如果将此选项的值设置为 &true;,
|
||
那么会话文件会在读取完毕之后马上关闭,
|
||
因此,可以在会话数据没有变动的时候,避免不必要的文件锁。
|
||
</para>
|
||
</listitem>
|
||
</varlistentry>
|
||
</variablelist>
|
||
</refsect1>
|
||
|
||
<refsect1 role="returnvalues">
|
||
&reftitle.returnvalues;
|
||
<para>
|
||
成功开始会话返回 &true; ,反之返回 &false;
|
||
</para>
|
||
</refsect1>
|
||
|
||
<refsect1 role="changelog">
|
||
&reftitle.changelog;
|
||
<para>
|
||
<informaltable>
|
||
<tgroup cols="2">
|
||
<thead>
|
||
<row>
|
||
<entry>&Version;</entry>
|
||
<entry>&Description;</entry>
|
||
</row>
|
||
</thead>
|
||
<tbody>
|
||
<row>
|
||
<entry>7.1.0</entry>
|
||
<entry>
|
||
当 <function>session_start</function> 执行失败,
|
||
无法开始一个会话的时候,会返回 &false;,
|
||
并且不会初始化超级变量 <varname>$_SESSION</varname>。
|
||
</entry>
|
||
</row>
|
||
</tbody>
|
||
</tgroup>
|
||
</informaltable>
|
||
</para>
|
||
</refsect1>
|
||
|
||
<refsect1 role="examples">
|
||
&reftitle.examples;
|
||
<refsect2>
|
||
<title>基本的会话示例</title>
|
||
<para>
|
||
<example>
|
||
<title><filename>page1.php</filename></title>
|
||
<programlisting role="php">
|
||
<![CDATA[
|
||
<?php
|
||
// page1.php
|
||
|
||
session_start();
|
||
|
||
echo 'Welcome to page #1';
|
||
|
||
$_SESSION['favcolor'] = 'green';
|
||
$_SESSION['animal'] = 'cat';
|
||
$_SESSION['time'] = time();
|
||
|
||
// 如果使用 cookie 方式传送会话 ID
|
||
echo '<br /><a href="page2.php">page 2</a>';
|
||
|
||
// 如果不是使用 cookie 方式传送会话 ID,则使用 URL 改写的方式传送会话 ID
|
||
echo '<br /><a href="page2.php?' . SID . '">page 2</a>';
|
||
?>
|
||
]]>
|
||
</programlisting>
|
||
</example>
|
||
</para>
|
||
<para>
|
||
请求 <filename>page1.php</filename> 页面之后,
|
||
第二个页面 <filename>page2.php</filename>
|
||
会包含会话数据。
|
||
请查阅 <link linkend="ref.session">会话参考</link>
|
||
获取更多关于 <link linkend="session.idpassing">会话 ID 传送</link>的信息,
|
||
在该参考页面中有关于常量 <constant>SID</constant> 的详细说明。
|
||
</para>
|
||
<para>
|
||
<example>
|
||
<title><filename>page2.php</filename></title>
|
||
<programlisting role="php">
|
||
<![CDATA[
|
||
<?php
|
||
// page2.php
|
||
|
||
session_start();
|
||
|
||
echo 'Welcome to page #2<br />';
|
||
|
||
echo $_SESSION['favcolor']; // green
|
||
echo $_SESSION['animal']; // cat
|
||
echo date('Y m d H:i:s', $_SESSION['time']);
|
||
|
||
// 类似 page1.php 中的代码,你可能需要在这里处理使用 SID 的场景
|
||
echo '<br /><a href="page1.php">page 1</a>';
|
||
?>
|
||
]]>
|
||
</programlisting>
|
||
</example>
|
||
</para>
|
||
</refsect2>
|
||
<refsect2>
|
||
<title>调用 <function>session_start</function> 的时候指定选项</title>
|
||
|
||
<example>
|
||
<title>覆盖 Cookie 超时时间设定</title>
|
||
<programlisting role="php">
|
||
<![CDATA[
|
||
<?php
|
||
// 设置 cookie 的有效时间为 1 天
|
||
session_start([
|
||
'cookie_lifetime' => 86400,
|
||
]);
|
||
?>
|
||
]]>
|
||
</programlisting>
|
||
</example>
|
||
|
||
<example>
|
||
<title>读取会话之后立即关闭会话存储文件</title>
|
||
<programlisting role="php">
|
||
<![CDATA[
|
||
<?php
|
||
// 如果确定不修改会话中的数据,
|
||
// 我们可以在会话文件读取完毕之后立即关闭它
|
||
// 来避免由于给会话文件加锁导致其他页面阻塞
|
||
session_start([
|
||
'cookie_lifetime' => 86400,
|
||
'read_and_close' => true,
|
||
]);
|
||
]]>
|
||
</programlisting>
|
||
</example>
|
||
</refsect2>
|
||
|
||
</refsect1>
|
||
|
||
<refsect1 role="notes">
|
||
&reftitle.notes;
|
||
<note>
|
||
<para>
|
||
要使用基于 cookie 的会话,
|
||
必须在输出开始之前调用 <function>session_start</function> 函数。
|
||
</para>
|
||
</note>
|
||
<note>
|
||
<para>
|
||
建议使用 <link linkend="ini.zlib.output-compression">zlib.output_compression</link>
|
||
来替代 <function>ob_gzhandler</function>。
|
||
</para>
|
||
</note>
|
||
<note>
|
||
<para>
|
||
根据配置不同,本函数会发送几个 HTTP 响应头。
|
||
参考 <function>session_cache_limiter</function>
|
||
来自定义 HTTP 响应头。
|
||
</para>
|
||
</note>
|
||
</refsect1>
|
||
|
||
<refsect1 role="seealso">
|
||
&reftitle.seealso;
|
||
<para>
|
||
<simplelist>
|
||
<member><varname>$_SESSION</varname></member>
|
||
<member>
|
||
<link linkend="ini.session.auto-start">session.auto_start</link>
|
||
配置指示
|
||
</member>
|
||
<member><function>session_id</function></member>
|
||
</simplelist>
|
||
</para>
|
||
</refsect1>
|
||
|
||
</refentry>
|
||
<!-- Keep this comment at the end of the file
|
||
Local variables:
|
||
mode: sgml
|
||
sgml-omittag:t
|
||
sgml-shorttag:t
|
||
sgml-minimize-attributes:nil
|
||
sgml-always-quote-attributes:t
|
||
sgml-indent-step:1
|
||
sgml-indent-data:t
|
||
indent-tabs-mode:nil
|
||
sgml-parent-document:nil
|
||
sgml-default-dtd-file:"~/.phpdoc/manual.ced"
|
||
sgml-exposed-tags:nil
|
||
sgml-local-catalogs:nil
|
||
sgml-local-ecat-files:nil
|
||
End:
|
||
vim600: syn=xml fen fdm=syntax fdl=2 si
|
||
vim: et tw=78 syn=sgml
|
||
vi: ts=1 sw=1
|
||
-->
|