2010-03-28 22:10:10 +00:00
|
|
|
<?xml version="1.0" encoding="utf-8"?>
|
2009-07-11 08:54:10 +00:00
|
|
|
<!-- $Revision$ -->
|
2007-12-21 18:42:54 +00:00
|
|
|
|
|
|
|
<appendix xml:id="session.examples" xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink">
|
|
|
|
&reftitle.examples;
|
|
|
|
<section xml:id="session.examples.basic">
|
2011-05-09 14:13:30 +00:00
|
|
|
<title>Basic usage</title>
|
2007-12-21 18:42:54 +00:00
|
|
|
<para>
|
2012-03-12 19:13:43 +00:00
|
|
|
Sessions are a simple way to store data for individual users against a unique session ID.
|
|
|
|
This can be used to persist state information between page requests. Session IDs are normally
|
|
|
|
sent to the browser via session cookies and the ID is used to retrieve existing session data.
|
|
|
|
The absence of a ID or session cookie let's PHP know to create a new session, and generate a new
|
|
|
|
session ID.
|
2007-12-21 18:42:54 +00:00
|
|
|
</para>
|
|
|
|
<para>
|
2012-03-12 19:13:43 +00:00
|
|
|
Sessions follow a simple workflow. When a session is started, PHP will either retrieve an
|
|
|
|
existing session using the ID passed (usually from a session cookie) or if no session is passed
|
|
|
|
it will create a new session. PHP will populate the <varname>$_SESSION</varname> superglobal
|
|
|
|
with any session data after the session has started. When PHP shuts down, it will automatically
|
|
|
|
take the contents of the <varname>$_SESSION</varname> superglobal, serialize it, and send it
|
|
|
|
for storage using the session save handler.
|
|
|
|
</para>
|
|
|
|
<para>
|
|
|
|
By default, PHP uses the internal <parameter>files</parameter> save handler which
|
|
|
|
is set by <link linkend="ini.session.save-handler">session.save_handler</link>.
|
|
|
|
This saves session data on the server at the location specified by the
|
|
|
|
<link linkend="ini.session.save-path">session.save_path</link> configuration directive.
|
|
|
|
</para>
|
|
|
|
<para>
|
|
|
|
Sessions can be started manually using the <function>session_start</function> function,
|
|
|
|
and if the <link linkend="ini.session.auto-start">session.auto_start</link> directive is set
|
|
|
|
to <parameter>1</parameter>, a session will automatically start the moment PHP sends any output
|
|
|
|
to the output buffer.
|
|
|
|
</para>
|
|
|
|
<para>
|
|
|
|
Sessions normally shutdown automatically when PHP is finished executing a script, but can be
|
|
|
|
manually shutdown using the <function>session_write_close</function> function.
|
2007-12-21 18:42:54 +00:00
|
|
|
</para>
|
|
|
|
<para>
|
|
|
|
<example>
|
|
|
|
<title>
|
|
|
|
Registering a variable with <varname>$_SESSION</varname>.
|
|
|
|
</title>
|
|
|
|
<programlisting role="php">
|
|
|
|
<![CDATA[
|
|
|
|
<?php
|
|
|
|
session_start();
|
|
|
|
if (!isset($_SESSION['count'])) {
|
|
|
|
$_SESSION['count'] = 0;
|
|
|
|
} else {
|
|
|
|
$_SESSION['count']++;
|
|
|
|
}
|
|
|
|
?>
|
|
|
|
]]>
|
|
|
|
</programlisting>
|
|
|
|
</example>
|
|
|
|
<example>
|
|
|
|
<title>
|
2012-03-13 02:12:21 +00:00
|
|
|
Unregistering a variable with <varname>$_SESSION</varname>.
|
2007-12-21 18:42:54 +00:00
|
|
|
</title>
|
|
|
|
<programlisting role="php">
|
|
|
|
<![CDATA[
|
|
|
|
<?php
|
|
|
|
session_start();
|
|
|
|
unset($_SESSION['count']);
|
|
|
|
?>
|
|
|
|
]]>
|
|
|
|
</programlisting>
|
|
|
|
</example>
|
|
|
|
</para>
|
|
|
|
<para>
|
|
|
|
<caution>
|
|
|
|
<para>
|
|
|
|
Do NOT unset the whole <varname>$_SESSION</varname> with
|
|
|
|
<literal>unset($_SESSION)</literal> as this will disable the
|
|
|
|
registering of session variables through the
|
|
|
|
<varname>$_SESSION</varname> superglobal.
|
|
|
|
</para>
|
|
|
|
</caution>
|
|
|
|
</para>
|
|
|
|
<warning>
|
|
|
|
<para>
|
|
|
|
You can't use references in session variables as there is no feasible way
|
|
|
|
to restore a reference to another variable.
|
|
|
|
</para>
|
|
|
|
</warning>
|
|
|
|
</section>
|
2012-02-21 07:44:11 +00:00
|
|
|
|
2007-12-21 18:42:54 +00:00
|
|
|
<section xml:id="session.idpassing">
|
|
|
|
<title>Passing the Session ID</title>
|
|
|
|
<para>
|
|
|
|
There are two methods to propagate a session id:
|
|
|
|
<itemizedlist>
|
|
|
|
<listitem>
|
|
|
|
<simpara>
|
|
|
|
Cookies
|
|
|
|
</simpara>
|
|
|
|
</listitem>
|
|
|
|
<listitem>
|
|
|
|
<simpara>
|
|
|
|
URL parameter
|
|
|
|
</simpara>
|
|
|
|
</listitem>
|
|
|
|
</itemizedlist>
|
|
|
|
</para>
|
|
|
|
<para>
|
|
|
|
The session module supports both methods. Cookies are optimal, but
|
|
|
|
because they are not always available, we also provide an alternative
|
|
|
|
way. The second method embeds the session id directly into URLs.
|
|
|
|
</para>
|
|
|
|
<para>
|
|
|
|
PHP is capable of transforming links transparently. Unless you are using
|
|
|
|
PHP 4.2.0 or later, you need to enable it manually when building PHP.
|
|
|
|
Under Unix, pass <link linkend="ini.session.use-trans-sid">
|
|
|
|
--enable-trans-sid</link> to configure. If this build
|
|
|
|
option and the run-time option
|
|
|
|
<literal>session.use_trans_sid</literal> are enabled, relative
|
|
|
|
URIs will be changed to contain the session id automatically.
|
|
|
|
<note>
|
|
|
|
<para>
|
|
|
|
The <link linkend="ini.arg-separator.output">arg_separator.output</link>
|
|
|
|
&php.ini; directive allows to customize the argument seperator. For full
|
|
|
|
XHTML conformance, specify &amp; there.
|
|
|
|
</para>
|
2012-02-21 07:44:11 +00:00
|
|
|
</note>
|
2007-12-21 18:42:54 +00:00
|
|
|
</para>
|
|
|
|
<para>
|
2009-05-03 15:59:44 +00:00
|
|
|
Alternatively, you can use the constant <constant>SID</constant> which is
|
2007-12-21 18:42:54 +00:00
|
|
|
defined if the session started. If the client did not send an appropriate session
|
|
|
|
cookie, it has the form <literal>session_name=session_id</literal>.
|
|
|
|
Otherwise, it expands to an empty string. Thus, you can embed it
|
|
|
|
unconditionally into URLs.
|
|
|
|
</para>
|
|
|
|
<para>
|
|
|
|
The following example demonstrates how to register a variable, and
|
2009-05-03 15:59:44 +00:00
|
|
|
how to link correctly to another page using <constant>SID</constant>.
|
2007-12-21 18:42:54 +00:00
|
|
|
<example>
|
|
|
|
<title>Counting the number of hits of a single user</title>
|
|
|
|
<programlisting role="php">
|
|
|
|
<![CDATA[
|
|
|
|
<?php
|
|
|
|
|
|
|
|
session_start();
|
|
|
|
|
|
|
|
if (empty($_SESSION['count'])) {
|
2012-03-12 19:13:43 +00:00
|
|
|
$_SESSION['count'] = 1;
|
2007-12-21 18:42:54 +00:00
|
|
|
} else {
|
2012-03-12 19:13:43 +00:00
|
|
|
$_SESSION['count']++;
|
2007-12-21 18:42:54 +00:00
|
|
|
}
|
|
|
|
?>
|
|
|
|
|
|
|
|
<p>
|
|
|
|
Hello visitor, you have seen this page <?php echo $_SESSION['count']; ?> times.
|
|
|
|
</p>
|
|
|
|
|
|
|
|
<p>
|
|
|
|
To continue, <a href="nextpage.php?<?php echo htmlspecialchars(SID); ?>">click
|
|
|
|
here</a>.
|
|
|
|
</p>
|
|
|
|
]]>
|
|
|
|
</programlisting>
|
|
|
|
</example>
|
|
|
|
</para>
|
|
|
|
<para>
|
2009-05-03 15:59:44 +00:00
|
|
|
The <function>htmlspecialchars</function> may be used when printing the <constant>SID</constant>
|
2007-12-21 18:42:54 +00:00
|
|
|
in order to prevent XSS related attacks.
|
|
|
|
</para>
|
|
|
|
<para>
|
2009-05-03 15:59:44 +00:00
|
|
|
Printing the <constant>SID</constant>, like shown above, is not necessary if
|
2007-12-21 18:42:54 +00:00
|
|
|
<link linkend="ini.session.use-trans-sid">
|
|
|
|
--enable-trans-sid</link> was used to compile PHP.
|
|
|
|
</para>
|
|
|
|
<note>
|
|
|
|
<para>
|
|
|
|
Non-relative URLs are assumed to point to external sites and
|
2009-05-03 15:59:44 +00:00
|
|
|
hence don't append the <constant>SID</constant>, as it would be a security risk to
|
2009-05-22 12:06:15 +00:00
|
|
|
leak the <constant>SID</constant> to a different server.
|
2007-12-21 18:42:54 +00:00
|
|
|
</para>
|
|
|
|
</note>
|
|
|
|
</section>
|
2012-02-21 07:44:11 +00:00
|
|
|
|
2007-12-21 18:42:54 +00:00
|
|
|
<section xml:id="session.customhandler">
|
|
|
|
<title>Custom Session Handlers</title>
|
|
|
|
<para>
|
|
|
|
To implement database storage, or any other storage method, you
|
|
|
|
will need to use <function>session_set_save_handler</function> to
|
2012-02-29 06:04:30 +00:00
|
|
|
create a set of user-level storage functions. As of PHP 5.4.0 you may create session handlers
|
|
|
|
using the <classname>SessionHandlerInterface</classname> or extend internal PHP handlers by inheriting
|
|
|
|
from <classname>SessionHandler</classname>.
|
2007-12-21 18:42:54 +00:00
|
|
|
</para>
|
2012-02-20 15:22:27 +00:00
|
|
|
<para>
|
|
|
|
The callbacks specified in <function>session_set_save_handler</function> are methods
|
2012-02-29 06:04:30 +00:00
|
|
|
called by PHP during the life-cycle of a session: <parameter>open</parameter>, <parameter>read</parameter>,
|
2012-02-29 06:09:12 +00:00
|
|
|
<parameter>write</parameter> and <parameter>close</parameter> and for the housekeeping tasks:
|
|
|
|
<parameter>destroy</parameter> for deleting a session and <parameter>gc</parameter> for periodic garbage
|
|
|
|
collection.
|
2012-02-20 15:22:27 +00:00
|
|
|
</para>
|
|
|
|
<para>
|
|
|
|
Therefor, PHP always requires session save handlers. The default is usually the
|
2012-02-29 06:04:30 +00:00
|
|
|
internal 'files' save handler. A custom save handler can be set using
|
|
|
|
<function>session_set_save_handler</function>. Alternative internal save handlers are also be
|
|
|
|
provided by PHP extensions, such as <parameter>sqlite</parameter>,
|
|
|
|
<parameter>memcache</parameter> and <parameter>memcached</parameter> and can be set with
|
|
|
|
<link linkend="ini.session.save-handler">session.save_handler</link>.
|
2012-02-20 15:22:27 +00:00
|
|
|
</para>
|
|
|
|
<para>
|
2012-02-29 06:04:30 +00:00
|
|
|
When the session starts, PHP will internally call the <parameter>open</parameter> handler followed by the
|
|
|
|
<parameter>read</parameter> callback which should return an encoded string extactly as it was originally
|
|
|
|
passed for storage. Once the <parameter>read</parameter> callback returns the encoded string, PHP will
|
|
|
|
decode it and then populate the resulting array into the <varname>$_SESSION</varname> superglobal.
|
2012-02-20 15:22:27 +00:00
|
|
|
</para>
|
|
|
|
<para>
|
|
|
|
When PHP shuts down (or when <function>session_write_close</function> is called),
|
2012-02-24 08:50:39 +00:00
|
|
|
PHP will internally encode the <varname>$_SESSION</varname> superglobal and pass this
|
|
|
|
along with the session ID to the the <parameter>write</parameter> callback.
|
2012-02-29 06:04:30 +00:00
|
|
|
After the <parameter>write</parameter> callback has finished, PHP will internally invoke the
|
|
|
|
<parameter>close</parameter> callback handler.
|
2012-02-20 15:22:27 +00:00
|
|
|
</para>
|
|
|
|
<para>
|
2012-02-29 06:15:21 +00:00
|
|
|
When a session is specifically destroyed, PHP will call the <parameter>destroy</parameter> handler with
|
|
|
|
the session ID.
|
2012-02-20 15:22:27 +00:00
|
|
|
</para>
|
|
|
|
<para>
|
2012-02-24 08:50:39 +00:00
|
|
|
PHP will call the <parameter>gc</parameter> callback from time to time to
|
|
|
|
expire any session records according to the set max lifetime of a session.
|
|
|
|
This routine should delete all records from persistent storage which were
|
2012-02-24 23:45:13 +00:00
|
|
|
last accessed longer than the <parameter>$lifetime</parameter>.
|
2012-02-20 15:22:27 +00:00
|
|
|
</para>
|
2007-12-21 18:42:54 +00:00
|
|
|
</section>
|
|
|
|
</appendix>
|
|
|
|
|
|
|
|
<!-- 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
|
2009-09-25 07:04:39 +00:00
|
|
|
sgml-default-dtd-file:"~/.phpdoc/manual.ced"
|
2007-12-21 18:42:54 +00:00
|
|
|
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
|
|
|
|
-->
|
|
|
|
|