2 <!DOCTYPE modulesynopsis SYSTEM "../style/modulesynopsis.dtd">
3 <?xml-stylesheet type="text/xsl" href="../style/manual.en.xsl"?>
4 <!-- $LastChangedRevision$ -->
7 Licensed to the Apache Software Foundation (ASF) under one or more
8 contributor license agreements. See the NOTICE file distributed with
9 this work for additional information regarding copyright ownership.
10 The ASF licenses this file to You under the Apache License, Version 2.0
11 (the "License"); you may not use this file except in compliance with
12 the License. You may obtain a copy of the License at
14 http://www.apache.org/licenses/LICENSE-2.0
16 Unless required by applicable law or agreed to in writing, software
17 distributed under the License is distributed on an "AS IS" BASIS,
18 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19 See the License for the specific language governing permissions and
20 limitations under the License.
23 <modulesynopsis metafile="mod_session_dbd.xml.meta">
25 <name>mod_session_dbd</name>
26 <description>DBD/SQL based session support</description>
27 <status>Extension</status>
28 <sourcefile>mod_session_dbd.c</sourcefile>
29 <identifier>session_dbd_module</identifier>
30 <compatibility>Available in Apache 2.3 and later</compatibility>
33 <note type="warning"><title>Warning</title>
34 <p>The session modules make use of HTTP cookies, and as such can fall
35 victim to Cross Site Scripting attacks, or expose potentially private
36 information to clients. Please ensure that the relevant risks have
37 been taken into account before enabling the session functionality on
41 <p>This submodule of <module>mod_session</module> provides support for the
42 storage of user sessions within a SQL database using the
43 <module>mod_dbd</module> module.</p>
45 <p>Sessions can either be <strong>anonymous</strong>, where the session is
46 keyed by a unique UUID string stored on the browser in a cookie, or
47 <strong>per user</strong>, where the session is keyed against the userid of
48 the logged in user.</p>
50 <p>SQL based sessions are hidden from the browser, and so offer a measure of
51 privacy without the need for encryption.</p>
53 <p>Different webservers within a server farm may choose to share a database,
54 and so share sessions with one another.</p>
56 <p>For more details on the session interface, see the documentation for
57 the <module>mod_session</module> module.</p>
60 <seealso><module>mod_session</module></seealso>
61 <seealso><module>mod_session_crypto</module></seealso>
62 <seealso><module>mod_session_cookie</module></seealso>
63 <seealso><module>mod_dbd</module></seealso>
65 <section id="dbdconfig"><title>DBD Configuration</title>
67 <p>Before the <module>mod_session_dbd</module> module can be configured to maintain a
68 session, the <module>mod_dbd</module> module must be configured to make the various database queries
69 available to the server.</p>
71 <p>There are four queries required to keep a session maintained, to select an existing session,
72 to update an existing session, to insert a new session, and to delete an expired or empty
73 session. These queries are configured as per the example below.</p>
75 <example><title>Sample DBD configuration</title>
76 <highlight language="config">
78 DBDParams "dbname=apachesession user=apache password=xxxxx host=localhost"
79 DBDPrepareSQL "delete from session where key = %s" deletesession
80 DBDPrepareSQL "update session set value = %s, expiry = %lld where key = %s" updatesession
81 DBDPrepareSQL "insert into session (value, expiry, key) values (%s, %lld, %s)" insertsession
82 DBDPrepareSQL "select value from session where key = %s and (expiry = 0 or expiry > %lld)" selectsession
83 DBDPrepareSQL "delete from session where expiry != 0 and expiry < %lld" cleansession
89 <section id="anonymous"><title>Anonymous Sessions</title>
91 <p>Anonymous sessions are keyed against a unique UUID, and stored on the
92 browser within an HTTP cookie. This method is similar to that used by most
93 application servers to store session information.</p>
95 <p>To create a simple anonymous session and store it in a postgres database
96 table called <var>apachesession</var>, and save the session ID in a cookie
97 called <var>session</var>, configure the session as follows:</p>
99 <example><title>SQL based anonymous session</title>
100 <highlight language="config">
102 SessionDBDCookieName session path=/
106 <p>For more examples on how the session can be configured to be read
107 from and written to by a CGI application, see the
108 <module>mod_session</module> examples section.</p>
110 <p>For documentation on how the session can be used to store username
111 and password details, see the <module>mod_auth_form</module> module.</p>
115 <section id="peruser"><title>Per User Sessions</title>
117 <p>Per user sessions are keyed against the username of a successfully
118 authenticated user. It offers the most privacy, as no external handle
119 to the session exists outside of the authenticated realm.</p>
121 <p>Per user sessions work within a correctly configured authenticated
122 environment, be that using basic authentication, digest authentication
123 or SSL client certificates. Due to the limitations of who came first,
124 the chicken or the egg, per user sessions cannot be used to store
125 authentication credentials from a module like
126 <module>mod_auth_form</module>.</p>
128 <p>To create a simple per user session and store it in a postgres database
129 table called <var>apachesession</var>, and with the session keyed to the
130 userid, configure the session as follows:</p>
132 <example><title>SQL based per user session</title>
133 <highlight language="config">
141 <section id="housekeeping"><title>Database Housekeeping</title>
142 <p>Over the course of time, the database can be expected to start accumulating
143 expired sessions. At this point, the <module>mod_session_dbd</module> module
144 is not yet able to handle session expiry automatically.</p>
146 <note type="warning"><title>Warning</title>
147 <p>The administrator will need to set up an external process via cron to clean
148 out expired sessions.</p>
154 <name>SessionDBDCookieName</name>
155 <description>Name and attributes for the RFC2109 cookie storing the session ID</description>
156 <syntax>SessionDBDCookieName <var>name</var> <var>attributes</var></syntax>
157 <default>none</default>
158 <contextlist><context>server config</context>
159 <context>virtual host</context>
160 <context>directory</context>
161 <context>.htaccess</context>
165 <p>The <directive>SessionDBDCookieName</directive> directive specifies the name and
166 optional attributes of an RFC2109 compliant cookie inside which the session ID will
167 be stored. RFC2109 cookies are set using the <code>Set-Cookie</code> HTTP header.
170 <p>An optional list of cookie attributes can be specified, as per the example below.
171 These attributes are inserted into the cookie as is, and are not interpreted by
172 Apache. Ensure that your attributes are defined correctly as per the cookie specification.
175 <example><title>Cookie with attributes</title>
176 <highlight language="config">
178 SessionDBDCookieName session path=/private;domain=example.com;httponly;secure;version=1;
186 <name>SessionDBDCookieName2</name>
187 <description>Name and attributes for the RFC2965 cookie storing the session ID</description>
188 <syntax>SessionDBDCookieName2 <var>name</var> <var>attributes</var></syntax>
189 <default>none</default>
190 <contextlist><context>server config</context>
191 <context>virtual host</context>
192 <context>directory</context>
193 <context>.htaccess</context>
197 <p>The <directive>SessionDBDCookieName2</directive> directive specifies the name and
198 optional attributes of an RFC2965 compliant cookie inside which the session ID will
199 be stored. RFC2965 cookies are set using the <code>Set-Cookie2</code> HTTP header.
202 <p>An optional list of cookie attributes can be specified, as per the example below.
203 These attributes are inserted into the cookie as is, and are not interpreted by
204 Apache. Ensure that your attributes are defined correctly as per the cookie specification.
207 <example><title>Cookie2 with attributes</title>
208 <highlight language="config">
210 SessionDBDCookieName2 session path=/private;domain=example.com;httponly;secure;version=1;
218 <name>SessionDBDCookieRemove</name>
219 <description>Control for whether session ID cookies should be removed from incoming HTTP headers</description>
220 <syntax>SessionDBDCookieRemove On|Off</syntax>
221 <default>SessionDBDCookieRemove On</default>
222 <contextlist><context>server config</context>
223 <context>virtual host</context>
224 <context>directory</context>
225 <context>.htaccess</context>
229 <p>The <directive>SessionDBDCookieRemove</directive> flag controls whether the cookies
230 containing the session ID will be removed from the headers during request processing.</p>
232 <p>In a reverse proxy situation where the Apache server acts as a server frontend for
233 a backend origin server, revealing the contents of the session ID cookie to the backend
234 could be a potential privacy violation. When set to on, the session ID cookie will be
235 removed from the incoming HTTP headers.</p>
241 <name>SessionDBDPerUser</name>
242 <description>Enable a per user session</description>
243 <syntax>SessionDBDPerUser On|Off</syntax>
244 <default>SessionDBDPerUser Off</default>
245 <contextlist><context>server config</context>
246 <context>virtual host</context>
247 <context>directory</context>
248 <context>.htaccess</context>
252 <p>The <directive>SessionDBDPerUser</directive> flag enables a per user session keyed
253 against the user's login name. If the user is not logged in, this directive will be
260 <name>SessionDBDSelectLabel</name>
261 <description>The SQL query to use to select sessions from the database</description>
262 <syntax>SessionDBDSelectLabel <var>label</var></syntax>
263 <default>SessionDBDSelectLabel selectsession</default>
264 <contextlist><context>server config</context>
265 <context>virtual host</context>
266 <context>directory</context>
267 <context>.htaccess</context>
271 <p>The <directive>SessionDBDSelectLabel</directive> directive sets the default select
272 query label to be used to load in a session. This label must have been previously defined using the
273 <directive module="mod_dbd">DBDPrepareSQL</directive> directive.</p>
279 <name>SessionDBDInsertLabel</name>
280 <description>The SQL query to use to insert sessions into the database</description>
281 <syntax>SessionDBDInsertLabel <var>label</var></syntax>
282 <default>SessionDBDInsertLabel insertsession</default>
283 <contextlist><context>server config</context>
284 <context>virtual host</context>
285 <context>directory</context>
286 <context>.htaccess</context>
290 <p>The <directive>SessionDBDInsertLabel</directive> directive sets the default insert
291 query label to be used to load in a session. This label must have been previously defined using the
292 <directive module="mod_dbd">DBDPrepareSQL</directive> directive.</p>
294 <p>If an attempt to update the session affects no rows, this query will be called to insert the
295 session into the database.</p>
301 <name>SessionDBDUpdateLabel</name>
302 <description>The SQL query to use to update existing sessions in the database</description>
303 <syntax>SessionDBDUpdateLabel <var>label</var></syntax>
304 <default>SessionDBDUpdateLabel updatesession</default>
305 <contextlist><context>server config</context>
306 <context>virtual host</context>
307 <context>directory</context>
308 <context>.htaccess</context>
312 <p>The <directive>SessionDBDUpdateLabel</directive> directive sets the default update
313 query label to be used to load in a session. This label must have been previously defined using the
314 <directive module="mod_dbd">DBDPrepareSQL</directive> directive.</p>
316 <p>If an attempt to update the session affects no rows, the insert query will be
317 called to insert the session into the database. If the database supports InsertOrUpdate,
318 override this query to perform the update in one query instead of two.</p>
324 <name>SessionDBDDeleteLabel</name>
325 <description>The SQL query to use to remove sessions from the database</description>
326 <syntax>SessionDBDDeleteLabel <var>label</var></syntax>
327 <default>SessionDBDDeleteLabel deletesession</default>
328 <contextlist><context>server config</context>
329 <context>virtual host</context>
330 <context>directory</context>
331 <context>.htaccess</context>
335 <p>The <directive>SessionDBDDeleteLabel</directive> directive sets the default delete
336 query label to be used to delete an expired or empty session. This label must have been previously
337 defined using the <directive module="mod_dbd">DBDPrepareSQL</directive> directive.</p>