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.
|
2012-12-03 15:53:03 +00:00
|
|
|
The absence of an ID or session cookie lets PHP know to create a new session, and generate a new
|
2012-03-12 19:13:43 +00:00
|
|
|
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>
|
2012-11-12 22:35:49 +00:00
|
|
|
Sessions can be started manually using the <function>session_start</function> function.
|
|
|
|
If the <link linkend="ini.session.auto-start">session.auto_start</link> directive is set
|
|
|
|
to <parameter>1</parameter>, a session will automatically start on request startup.
|
2012-03-12 19:13:43 +00:00
|
|
|
</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>
|
2013-09-25 21:00:54 +00:00
|
|
|
<note>
|
|
|
|
<para>
|
|
|
|
File based sessions (the default in PHP) lock the session file once a
|
|
|
|
session is opened via <function>session_start</function> or implicitly via
|
|
|
|
<link linkend="ini.session.auto-start">session.auto_start</link>. Once
|
|
|
|
locked, no other script can access the same session file until it has been
|
|
|
|
closed by the first script terminating or calling
|
|
|
|
<function>session_write_close</function>.
|
|
|
|
</para>
|
|
|
|
<para>
|
|
|
|
This is most likely to be an issue on Web sites that use AJAX heavily and
|
|
|
|
have multiple concurrent requests. The easiest way to deal with it is to
|
|
|
|
call <function>session_write_close</function> as soon as any required
|
|
|
|
changes to the session have been made, preferably early in the script.
|
|
|
|
Alternatively, a different session backend that does support concurrency
|
|
|
|
could be used.
|
|
|
|
</para>
|
|
|
|
</note>
|
2007-12-21 18:42:54 +00:00
|
|
|
</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>
|
2016-12-05 10:17:07 +00:00
|
|
|
PHP is capable of transforming links transparently. If the run-time
|
|
|
|
option <literal>session.use_trans_sid</literal> is enabled, relative
|
2007-12-21 18:42:54 +00:00
|
|
|
URIs will be changed to contain the session id automatically.
|
|
|
|
<note>
|
|
|
|
<para>
|
|
|
|
The <link linkend="ini.arg-separator.output">arg_separator.output</link>
|
2015-08-06 22:59:13 +00:00
|
|
|
&php.ini; directive allows to customize the argument separator. For full
|
2007-12-21 18:42:54 +00:00
|
|
|
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
|
2021-05-12 11:49:16 +00:00
|
|
|
create a set of user-level storage functions. A session handlers may be created
|
|
|
|
using the <classname>SessionHandlerInterface</classname> or extending PHP's internal handlers by inheriting
|
2012-02-29 06:04:30 +00:00
|
|
|
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>
|
2012-04-10 07:40:08 +00:00
|
|
|
Therefore, 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
|
2014-03-29 21:54:53 +00:00
|
|
|
<function>session_set_save_handler</function>. Alternative internal save handlers are also
|
2012-02-29 06:04:30 +00:00
|
|
|
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
|
2013-08-16 09:25:19 +00:00
|
|
|
<parameter>read</parameter> callback which should return an encoded string exactly as it was originally
|
2012-02-29 06:04:30 +00:00
|
|
|
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
|
2014-05-16 20:12:19 +00:00
|
|
|
along with the session ID to 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
|
|
|
|
-->
|
|
|
|
|