<HTML ><HEAD ><TITLE >Session handling functions</TITLE ><META NAME="GENERATOR" CONTENT="Modular DocBook HTML Stylesheet Version 1.7"><LINK REL="HOME" TITLE="PHP Manual" HREF="index.html"><LINK REL="UP" TITLE="Function Reference" HREF="funcref.html"><LINK REL="PREVIOUS" TITLE="sesam_settransaction" HREF="function.sesam-settransaction.html"><LINK REL="NEXT" TITLE="session_cache_expire" HREF="function.session-cache-expire.html"><META HTTP-EQUIV="Content-type" CONTENT="text/html; charset=ISO-8859-1"></HEAD ><BODY CLASS="reference" BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#0000FF" VLINK="#840084" ALINK="#0000FF" ><DIV CLASS="NAVHEADER" ><TABLE SUMMARY="Header navigation table" WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" ><TR ><TH COLSPAN="3" ALIGN="center" >PHP Manual</TH ></TR ><TR ><TD WIDTH="10%" ALIGN="left" VALIGN="bottom" ><A HREF="function.sesam-settransaction.html" ACCESSKEY="P" >Prev</A ></TD ><TD WIDTH="80%" ALIGN="center" VALIGN="bottom" ></TD ><TD WIDTH="10%" ALIGN="right" VALIGN="bottom" ><A HREF="function.session-cache-expire.html" ACCESSKEY="N" >Next</A ></TD ></TR ></TABLE ><HR ALIGN="LEFT" WIDTH="100%"></DIV ><DIV CLASS="reference" ><A NAME="ref.session" ></A ><DIV CLASS="TITLEPAGE" ><H1 CLASS="title" >XCIII. Session handling functions</H1 ><DIV CLASS="PARTINTRO" ><A NAME="AEN83869" ></A ><DIV CLASS="section" ><H1 CLASS="section" ><A NAME="session.intro" ></A >Introduction</H1 ><P > Session support in PHP consists of a way to preserve certain data across subsequent accesses. This enables you to build more customized applications and increase the appeal of your web site. </P ><P > If you are familiar with the session management of PHPLIB, you will notice that some concepts are similar to PHP's session support. </P ><P > A visitor accessing your web site is assigned an unique id, the so-called session id. This is either stored in a cookie on the user side or is propagated in the URL. </P ><P > The session support allows you to register arbitrary numbers of variables to be preserved across requests. When a visitor accesses your site, PHP will check automatically (if session.auto_start is set to 1) or on your request (explicitly through <A HREF="function.session-start.html" ><B CLASS="function" >session_start()</B ></A > or implicitly through <A HREF="function.session-register.html" ><B CLASS="function" >session_register()</B ></A >) whether a specific session id has been sent with the request. If this is the case, the prior saved environment is recreated. </P ><P > All registered variables are serialized after the request finishes. Registered variables which are undefined are marked as being not defined. On subsequent accesses, these are not defined by the session module unless the user defines them later. </P ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >Note: </B > Session handling was added in PHP 4.0. </P ></BLOCKQUOTE ></DIV ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >Note: </B > Please note when working with sessions that a record of a session is not created until a variable has been registered using the <A HREF="function.session-register.html" ><B CLASS="function" >session_register()</B ></A > function or by adding a new key to the <TT CLASS="varname" >$_SESSION</TT > superglobal array. This holds true regardless of if a session has been started using the <A HREF="function.session-start.html" ><B CLASS="function" >session_start()</B ></A > function. </P ></BLOCKQUOTE ></DIV ></DIV ><DIV CLASS="section" ><H1 CLASS="section" ><A NAME="session.security" ></A >Sessions and security</H1 ><P > External links: <A HREF="http://www.acros.si/papers/session_fixation.pdf" TARGET="_top" >Session fixation</A > </P ><P > The session module cannot guarantee that the information you store in a session is only viewed by the user who created the session. You need to take additional measures to actively protect the integrity of the session, depending on the value associated with it. </P ><P > Assess the importance of the data carried by your sessions and deploy addditional protections -- this usually comes at a price, reduced convenience for the user. For example, if you want to protect users from simple social engineering tactics, you need to enable session.use_only_cookies. In that case, cookies must be enabled unconditionally on the user side, or sessions will not work. </P ><P > There are several ways to leak an existing session id to third parties. A leaked session id enables the third party to access all resources which are associated with a specific id. First, URLs carrying session ids. If you link to an external site, the URL including the session id might be stored in the external site's referrer logs. Second, a more active attacker might listen to your network traffic. If it is not encrypted, session ids will flow in plain text over the network. The solution here is to implement SSL on your server and make it mandatory for users. </P ></DIV ><DIV CLASS="section" ><H1 CLASS="section" ><A NAME="session.requirements" ></A >Requirements</H1 ><P >No external libraries are needed to build this extension.</P ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >Note: </B > Optionally you can use shared memory allocation (mm), developed by Ralf S. Engelschall, for session storage. You have to download <A HREF="http://www.ossp.org/pkg/lib/mm/" TARGET="_top" >mm</A > and install it. This option is not available for Windows platforms. Note that the session storage module for mm does not guarantee that concurrent accesses to the same session are properly locked. It might be more appropiate to use a shared memory based filesystem (such as tmpfs on Solaris/Linux, or /dev/md on BSD) to store sessions in files, because they are properly locked. </P ></BLOCKQUOTE ></DIV ></DIV ><DIV CLASS="section" ><H1 CLASS="section" ><A NAME="session.installation" ></A >Installation</H1 ><P > Session support is enabled in PHP by default. If you would not like to build your PHP with session support, you should specify the <TT CLASS="option" >--disable-session</TT > option to configure. To use shared memory allocation (mm) for session storage configure PHP <TT CLASS="option" >--with-mm[=DIR] </TT >. </P ><P > The windows version of <TT CLASS="literal" >PHP</TT > has built in support for this extension. You do not need to load any additional extension in order to use these functions.</P ></DIV ><DIV CLASS="section" ><H1 CLASS="section" ><A NAME="session.configuration" ></A >Runtime Configuration</H1 ><P > The behaviour of these functions is affected by settings in <TT CLASS="filename" >php.ini</TT >. </P ><P > <DIV CLASS="table" ><A NAME="AEN83911" ></A ><P ><B >Table 1. Session configuration options</B ></P ><TABLE BORDER="1" CLASS="CALSTABLE" ><THEAD ><TR ><TH ALIGN="LEFT" VALIGN="MIDDLE" >Name</TH ><TH ALIGN="LEFT" VALIGN="MIDDLE" >Default</TH ><TH ALIGN="LEFT" VALIGN="MIDDLE" >Changeable</TH ></TR ></THEAD ><TBODY ><TR ><TD ALIGN="LEFT" VALIGN="MIDDLE" >session.save_path</TD ><TD ALIGN="LEFT" VALIGN="MIDDLE" >"/tmp"</TD ><TD ALIGN="LEFT" VALIGN="MIDDLE" >PHP_INI_ALL</TD ></TR ><TR ><TD ALIGN="LEFT" VALIGN="MIDDLE" >session.name</TD ><TD ALIGN="LEFT" VALIGN="MIDDLE" >"PHPSESSID"</TD ><TD ALIGN="LEFT" VALIGN="MIDDLE" >PHP_INI_ALL</TD ></TR ><TR ><TD ALIGN="LEFT" VALIGN="MIDDLE" >session.save_handler</TD ><TD ALIGN="LEFT" VALIGN="MIDDLE" >"files"</TD ><TD ALIGN="LEFT" VALIGN="MIDDLE" >PHP_INI_ALL</TD ></TR ><TR ><TD ALIGN="LEFT" VALIGN="MIDDLE" >session.auto_start</TD ><TD ALIGN="LEFT" VALIGN="MIDDLE" >"0"</TD ><TD ALIGN="LEFT" VALIGN="MIDDLE" >PHP_INI_ALL</TD ></TR ><TR ><TD ALIGN="LEFT" VALIGN="MIDDLE" >session.gc_probability</TD ><TD ALIGN="LEFT" VALIGN="MIDDLE" >"1"</TD ><TD ALIGN="LEFT" VALIGN="MIDDLE" >PHP_INI_ALL</TD ></TR ><TR ><TD ALIGN="LEFT" VALIGN="MIDDLE" >session.gc_maxlifetime</TD ><TD ALIGN="LEFT" VALIGN="MIDDLE" >"1440"</TD ><TD ALIGN="LEFT" VALIGN="MIDDLE" >PHP_INI_ALL</TD ></TR ><TR ><TD ALIGN="LEFT" VALIGN="MIDDLE" >session.serialize_handler</TD ><TD ALIGN="LEFT" VALIGN="MIDDLE" >"php"</TD ><TD ALIGN="LEFT" VALIGN="MIDDLE" >PHP_INI_ALL</TD ></TR ><TR ><TD ALIGN="LEFT" VALIGN="MIDDLE" >session.cookie_lifetime</TD ><TD ALIGN="LEFT" VALIGN="MIDDLE" >"0"</TD ><TD ALIGN="LEFT" VALIGN="MIDDLE" >PHP_INI_ALL</TD ></TR ><TR ><TD ALIGN="LEFT" VALIGN="MIDDLE" >session.cookie_path</TD ><TD ALIGN="LEFT" VALIGN="MIDDLE" >"/"</TD ><TD ALIGN="LEFT" VALIGN="MIDDLE" >PHP_INI_ALL</TD ></TR ><TR ><TD ALIGN="LEFT" VALIGN="MIDDLE" >session.cookie_domain</TD ><TD ALIGN="LEFT" VALIGN="MIDDLE" >""</TD ><TD ALIGN="LEFT" VALIGN="MIDDLE" >PHP_INI_ALL</TD ></TR ><TR ><TD ALIGN="LEFT" VALIGN="MIDDLE" >session.cookie_secure</TD ><TD ALIGN="LEFT" VALIGN="MIDDLE" >""</TD ><TD ALIGN="LEFT" VALIGN="MIDDLE" >PHP_INI_ALL</TD ></TR ><TR ><TD ALIGN="LEFT" VALIGN="MIDDLE" >session.use_cookies</TD ><TD ALIGN="LEFT" VALIGN="MIDDLE" >"1"</TD ><TD ALIGN="LEFT" VALIGN="MIDDLE" >PHP_INI_ALL</TD ></TR ><TR ><TD ALIGN="LEFT" VALIGN="MIDDLE" >session.use_only_cookies</TD ><TD ALIGN="LEFT" VALIGN="MIDDLE" >"0"</TD ><TD ALIGN="LEFT" VALIGN="MIDDLE" >PHP_INI_ALL</TD ></TR ><TR ><TD ALIGN="LEFT" VALIGN="MIDDLE" >session.referer_check</TD ><TD ALIGN="LEFT" VALIGN="MIDDLE" >""</TD ><TD ALIGN="LEFT" VALIGN="MIDDLE" >PHP_INI_ALL</TD ></TR ><TR ><TD ALIGN="LEFT" VALIGN="MIDDLE" >session.entropy_file</TD ><TD ALIGN="LEFT" VALIGN="MIDDLE" >""</TD ><TD ALIGN="LEFT" VALIGN="MIDDLE" >PHP_INI_ALL</TD ></TR ><TR ><TD ALIGN="LEFT" VALIGN="MIDDLE" >session.entropy_length</TD ><TD ALIGN="LEFT" VALIGN="MIDDLE" >"0"</TD ><TD ALIGN="LEFT" VALIGN="MIDDLE" >PHP_INI_ALL</TD ></TR ><TR ><TD ALIGN="LEFT" VALIGN="MIDDLE" >session.cache_limiter</TD ><TD ALIGN="LEFT" VALIGN="MIDDLE" >"nocache"</TD ><TD ALIGN="LEFT" VALIGN="MIDDLE" >PHP_INI_ALL</TD ></TR ><TR ><TD ALIGN="LEFT" VALIGN="MIDDLE" >session.cache_expire</TD ><TD ALIGN="LEFT" VALIGN="MIDDLE" >"180"</TD ><TD ALIGN="LEFT" VALIGN="MIDDLE" >PHP_INI_ALL</TD ></TR ><TR ><TD ALIGN="LEFT" VALIGN="MIDDLE" >session.use_trans_sid</TD ><TD ALIGN="LEFT" VALIGN="MIDDLE" >"0"</TD ><TD ALIGN="LEFT" VALIGN="MIDDLE" >PHP_INI_SYSTEM|PHP_INI_PERDIR</TD ></TR ><TR ><TD ALIGN="LEFT" VALIGN="MIDDLE" >url_rewriter.tags</TD ><TD ALIGN="LEFT" VALIGN="MIDDLE" >"a=href,area=href,frame=src,input=src,form=fakeentry"</TD ><TD ALIGN="LEFT" VALIGN="MIDDLE" >PHP_INI_ALL</TD ></TR ></TBODY ></TABLE ></DIV > For further details and definition of the PHP_INI_* constants see <A HREF="function.ini-set.html" ><B CLASS="function" >ini_set()</B ></A >. </P ><P > The session management system supports a number of configuration options which you can place in your <TT CLASS="filename" >php.ini</TT > file. We will give a short overview. <P ></P ><DIV CLASS="variablelist" ><DL ><DT ><A NAME="ini.session.save-handler" ></A ><TT CLASS="parameter" ><I >session.save_handler</I ></TT > <A HREF="language.types.string.html" ><B CLASS="type" >string</B ></A ></DT ><DD ><P > <TT CLASS="literal" >session.save_handler</TT > defines the name of the handler which is used for storing and retrieving data associated with a session. Defaults to <TT CLASS="literal" >files</TT >. See also <A HREF="function.session-set-save-handler.html" ><B CLASS="function" >session_set_save_handler()</B ></A >. </P ></DD ><DT ><A NAME="ini.session.save-path" ></A ><TT CLASS="parameter" ><I >session.save_path</I ></TT > <A HREF="language.types.string.html" ><B CLASS="type" >string</B ></A ></DT ><DD ><P > <TT CLASS="literal" >session.save_path</TT > defines the argument which is passed to the save handler. If you choose the default files handler, this is the path where the files are created. Defaults to <TT CLASS="literal" >/tmp</TT >. If <TT CLASS="literal" >session.save_path</TT >'s path depth is more than 2, garbage collection will not be performed. See also <A HREF="function.session-save-path.html" ><B CLASS="function" >session_save_path()</B ></A >. </P ><DIV CLASS="warning" ><P ></P ><TABLE CLASS="warning" BORDER="1" WIDTH="100%" ><TR ><TD ALIGN="CENTER" ><B >Warning</B ></TD ></TR ><TR ><TD ALIGN="LEFT" ><P > If you leave this set to a world-readable directory, such as <TT CLASS="filename" >/tmp</TT > (the default), other users on the server may be able to hijack sessions by getting the list of files in that directory. </P ></TD ></TR ></TABLE ></DIV ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >Note: </B > Windows users have to change this variable in order to use PHP's session functions. Make sure to specify a valid path, e.g.: <TT CLASS="filename" >c:/temp</TT >. </P ></BLOCKQUOTE ></DIV ></DD ><DT ><A NAME="ini.session.name" ></A ><TT CLASS="parameter" ><I >session.name</I ></TT > <A HREF="language.types.string.html" ><B CLASS="type" >string</B ></A ></DT ><DD ><P > <TT CLASS="literal" >session.name</TT > specifies the name of the session which is used as cookie name. It should only contain alphanumeric characters. Defaults to <TT CLASS="literal" >PHPSESSID</TT >. See also <A HREF="function.session-name.html" ><B CLASS="function" >session_name()</B ></A >. </P ></DD ><DT ><A NAME="ini.session.auto-start" ></A ><TT CLASS="parameter" ><I >session.auto_start</I ></TT > <A HREF="language.types.boolean.html" ><B CLASS="type" >boolean</B ></A ></DT ><DD ><P > <TT CLASS="literal" >session.auto_start</TT > specifies whether the session module starts a session automatically on request startup. Defaults to <TT CLASS="literal" >0</TT > (disabled). </P ></DD ><DT ><A NAME="ini.session.serialize-handler" ></A ><TT CLASS="parameter" ><I >session.serialize_handler</I ></TT > <A HREF="language.types.string.html" ><B CLASS="type" >string</B ></A ></DT ><DD ><P > <TT CLASS="literal" >session.serialize_handler</TT > defines the name of the handler which is used to serialize/deserialize data. Currently, a PHP internal format (name <TT CLASS="literal" >php</TT >) and WDDX is supported (name <TT CLASS="literal" >wddx</TT >). WDDX is only available, if PHP is compiled with <A HREF="ref.wddx.html" >WDDX support</A >. Defaults to <TT CLASS="literal" >php</TT >. </P ></DD ><DT ><A NAME="ini.session.gc-probability" ></A ><TT CLASS="parameter" ><I >session.gc_probability</I ></TT > <A HREF="language.types.integer.html" ><B CLASS="type" >integer</B ></A ></DT ><DD ><P > <TT CLASS="literal" >session.gc_probability</TT > specifies the probability that the gc (garbage collection) routine is started on each request in percent. Defaults to <TT CLASS="literal" >1</TT >. </P ></DD ><DT ><A NAME="ini.session.gc-maxlifetime" ></A ><TT CLASS="parameter" ><I >session.gc_maxlifetime</I ></TT > <A HREF="language.types.integer.html" ><B CLASS="type" >integer</B ></A ></DT ><DD ><P > <TT CLASS="literal" >session.gc_maxlifetime</TT > specifies the number of seconds after which data will be seen as 'garbage' and cleaned up. </P ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >Note: </B >If you are using the default file-based session handler, your filesystem must keep track of access times (atime). Windows FAT does not so you will have to come up with another way to handle garbage collecting your session if you are stuck with a FAT filesystem or any other fs where atime tracking is not available. </P ></BLOCKQUOTE ></DIV ></DD ><DT ><A NAME="ini.session.referer-check" ></A ><TT CLASS="parameter" ><I >session.referer_check</I ></TT > <A HREF="language.types.string.html" ><B CLASS="type" >string</B ></A ></DT ><DD ><P > <TT CLASS="literal" >session.referer_check</TT > contains the substring you want to check each HTTP Referer for. If the Referer was sent by the client and the substring was not found, the embedded session id will be marked as invalid. Defaults to the empty string. </P ></DD ><DT ><A NAME="ini.session.entropy-file" ></A ><TT CLASS="parameter" ><I >session.entropy_file</I ></TT > <A HREF="language.types.string.html" ><B CLASS="type" >string</B ></A ></DT ><DD ><P > <TT CLASS="literal" >session.entropy_file</TT > gives a path to an external resource (file) which will be used as an additional entropy source in the session id creation process. Examples are <TT CLASS="literal" >/dev/random</TT > or <TT CLASS="literal" >/dev/urandom</TT > which are available on many Unix systems. </P ></DD ><DT ><A NAME="ini.session.entropy-length" ></A ><TT CLASS="parameter" ><I >session.entropy_length</I ></TT > <A HREF="language.types.integer.html" ><B CLASS="type" >integer</B ></A ></DT ><DD ><P > <TT CLASS="literal" >session.entropy_length</TT > specifies the number of bytes which will be read from the file specified above. Defaults to <TT CLASS="literal" >0</TT > (disabled). </P ></DD ><DT ><A NAME="ini.session.use-cookies" ></A ><TT CLASS="parameter" ><I >session.use_cookies</I ></TT > <A HREF="language.types.boolean.html" ><B CLASS="type" >boolean</B ></A ></DT ><DD ><P > <TT CLASS="literal" >session.use_cookies</TT > specifies whether the module will use cookies to store the session id on the client side. Defaults to <TT CLASS="literal" >1</TT > (enabled). </P ></DD ><DT ><A NAME="ini.session.use-only_cookies" ></A ><TT CLASS="parameter" ><I >session.use_only_cookies</I ></TT > <A HREF="language.types.boolean.html" ><B CLASS="type" >boolean</B ></A ></DT ><DD ><P > <TT CLASS="literal" >session.use_only_cookies</TT > specifies whether the module will <SPAN CLASS="strong" ><B CLASS="emphasis" >only</B ></SPAN > use cookies to store the session id on the client side. Defaults to <TT CLASS="literal" >0</TT > (disabled, for backward compatibility). Enabling this setting prevents attacks involved passing session ids in URLs. This setting was added in <TT CLASS="literal" >PHP</TT > 4.3.0. </P ></DD ><DT ><A NAME="ini.session.cookie-lifetime" ></A ><TT CLASS="parameter" ><I >session.cookie_lifetime</I ></TT > <A HREF="language.types.integer.html" ><B CLASS="type" >integer</B ></A ></DT ><DD ><P > <TT CLASS="literal" >session.cookie_lifetime</TT > 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 <TT CLASS="literal" >0</TT >.See also <A HREF="function.session-get-cookie-params.html" ><B CLASS="function" >session_get_cookie_params()</B ></A > and <A HREF="function.session-set-cookie-params.html" ><B CLASS="function" >session_set_cookie_params()</B ></A >. </P ></DD ><DT ><A NAME="ini.session.cookie-path" ></A ><TT CLASS="parameter" ><I >session.cookie_path</I ></TT > <A HREF="language.types.string.html" ><B CLASS="type" >string</B ></A ></DT ><DD ><P > <TT CLASS="literal" >session.cookie_path</TT > specifies path to set in session_cookie. Defaults to <TT CLASS="literal" >/</TT >.See also <A HREF="function.session-get-cookie-params.html" ><B CLASS="function" >session_get_cookie_params()</B ></A > and <A HREF="function.session-set-cookie-params.html" ><B CLASS="function" >session_set_cookie_params()</B ></A >. </P ></DD ><DT ><A NAME="ini.session.cookie-domain" ></A ><TT CLASS="parameter" ><I >session.cookie_domain</I ></TT > <A HREF="language.types.string.html" ><B CLASS="type" >string</B ></A ></DT ><DD ><P > <TT CLASS="literal" >session.cookie_domain</TT > specifies the domain to set in session_cookie. Default is none at all. See also <A HREF="function.session-get-cookie-params.html" ><B CLASS="function" >session_get_cookie_params()</B ></A > and <A HREF="function.session-set-cookie-params.html" ><B CLASS="function" >session_set_cookie_params()</B ></A >. </P ></DD ><DT ><A NAME="ini.session.cookie-secure" ></A ><TT CLASS="parameter" ><I >session.cookie_secure</I ></TT > <A HREF="language.types.boolean.html" ><B CLASS="type" >boolean</B ></A ></DT ><DD ><P > <TT CLASS="literal" >session.cookie_secure</TT > specifies whether cookies should only be sent over secure connections. Defaults to <TT CLASS="literal" >off</TT >. This setting was added in <TT CLASS="literal" >PHP</TT > 4.0.4. See also <A HREF="function.session-get-cookie-params.html" ><B CLASS="function" >session_get_cookie_params()</B ></A > and <A HREF="function.session-set-cookie-params.html" ><B CLASS="function" >session_set_cookie_params()</B ></A >. </P ></DD ><DT ><A NAME="ini.session.cache-limiter" ></A ><TT CLASS="parameter" ><I >session.cache_limiter</I ></TT > <A HREF="language.types.string.html" ><B CLASS="type" >string</B ></A ></DT ><DD ><P > <TT CLASS="literal" >session.cache_limiter</TT > specifies cache control method to use for session pages (none/nocache/private/private_no_expire/public). Defaults to <TT CLASS="literal" >nocache</TT >. See also <A HREF="function.session-cache-limiter.html" ><B CLASS="function" >session_cache_limiter()</B ></A >. </P ></DD ><DT ><A NAME="ini.session.cache-expire" ></A ><TT CLASS="parameter" ><I >session.cache_expire</I ></TT > <A HREF="language.types.integer.html" ><B CLASS="type" >integer</B ></A ></DT ><DD ><P > <TT CLASS="literal" >session.cache_expire</TT > specifies time-to-live for cached session pages in minutes, this has no effect for nocache limiter. Defaults to <TT CLASS="literal" >180</TT >. See also <A HREF="function.session-cache-expire.html" ><B CLASS="function" >session_cache_expire()</B ></A >. </P ></DD ><DT ><A NAME="ini.session.use-trans_sid" ></A ><TT CLASS="parameter" ><I >session.use_trans_sid</I ></TT > <A HREF="language.types.boolean.html" ><B CLASS="type" >boolean</B ></A ></DT ><DD ><P > <TT CLASS="literal" >session.use_trans_sid</TT > whether transparent sid support is enabled or not. Defaults to <TT CLASS="literal" >0</TT > (disabled). </P ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >Note: </B > For PHP 4.1.2 or less, it is enabled by compiling with <A HREF="install.configure.html#install.configure.enable-trans-sid" > <TT CLASS="literal" >--enable-trans-sid</TT ></A >. From PHP 4.2.0, trans-sid feature is always compiled. </P ><P > URL based session management has additional security risks compared to cookie based session management. Users may send an URL that contains an active session ID to their friends by email or users may save an URL that contains a session ID to their bookmarks and access your site with the same session ID always, for example. </P ></BLOCKQUOTE ></DIV ></DD ><DT ><A NAME="ini.url-rewriter.tags" ></A ><TT CLASS="parameter" ><I >url_rewriter.tags</I ></TT > <A HREF="language.types.string.html" ><B CLASS="type" >string</B ></A ></DT ><DD ><P > <TT CLASS="literal" >url_rewriter.tags</TT > specifies which html tags are rewritten to include session id if transparent sid support is enabled. Defaults to <TT CLASS="literal" >a=href,area=href,frame=src,input=src,form=fakeentry</TT > </P ></DD ></DL ></DIV > </P ><P > The <A HREF="configuration.directives.html#ini.track-vars" ><TT CLASS="literal" >track_vars</TT ></A > and <A HREF="configuration.directives.html#ini.register-globals" ><TT CLASS="literal" >register_globals</TT ></A > configuration settings influence how the session variables get stored and restored. </P ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >Note: </B > As of PHP 4.0.3, <A HREF="configuration.directives.html#ini.track-vars" ><TT CLASS="literal" >track_vars</TT ></A > is always turned on. </P ></BLOCKQUOTE ></DIV ></DIV ><DIV CLASS="section" ><H1 CLASS="section" ><A NAME="session.resources" ></A >Resource Types</H1 ><P >This extension has no resource types defined.</P ></DIV ><DIV CLASS="section" ><H1 CLASS="section" ><A NAME="session.constants" ></A >Predefined Constants</H1 ><P > The constants below are defined by this extension, and will only be available when the extension has either been compiled into PHP or dynamically loaded at runtime. </P ><P ></P ><DIV CLASS="variablelist" ><DL ><DT ><TT CLASS="constant" ><B >SID</B ></TT > (<A HREF="language.types.string.html" >string</A >)</DT ><DD ><P > Constant containing the session name and session ID in the form of <TT CLASS="literal" >"name=ID"</TT >. </P ></DD ></DL ></DIV ></DIV ><DIV CLASS="section" ><H1 CLASS="section" ><A NAME="session.examples" ></A >Examples</H1 ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >Note: </B > As of PHP 4.1.0, <TT CLASS="varname" >$_SESSION</TT > is available as a global variable just like <TT CLASS="varname" >$_POST</TT >, <TT CLASS="varname" >$_GET</TT >, <TT CLASS="varname" >$_REQUEST</TT > and so on. Unlike <TT CLASS="varname" >$HTTP_SESSION_VARS</TT >, <TT CLASS="varname" >$_SESSION</TT > is always global. Therefore, you do not need to use the <A HREF="language.variables.scope.html" ><B CLASS="command" >global</B ></A > keyword for <TT CLASS="varname" >$_SESSION</TT >. Please note that this documentation has been changed to use <TT CLASS="varname" >$_SESSION</TT > everywhere. You can substitute <TT CLASS="varname" >$HTTP_SESSION_VARS</TT > for <TT CLASS="varname" >$_SESSION</TT >, if you prefer the former. Also note that you must start your session using <A HREF="function.session-start.html" ><B CLASS="function" >session_start()</B ></A > before use of <TT CLASS="varname" >$_SESSION</TT > becomes available. </P ><P > The keys in the <TT CLASS="varname" >$_SESSION</TT > associative array are subject to the same limitations as regular variable names in PHP, i.e. they cannot start with a number and must start with a letter or underscore. For more details see the section on <A HREF="language.variables.html" >variables</A > in this manual. </P ></BLOCKQUOTE ></DIV ><P > If <A HREF="configuration.directives.html#ini.register-globals" ><TT CLASS="literal" >register_globals</TT ></A > is disabled, only members of the global associative array <TT CLASS="varname" >$_SESSION</TT > can be registered as session variables. The restored session variables will only be available in the array <TT CLASS="varname" >$_SESSION</TT >. </P ><P > Use of <TT CLASS="varname" >$_SESSION</TT > (or <TT CLASS="varname" >$HTTP_SESSION_VARS</TT > with PHP 4.0.6 or less) is recommended for improved security and code readablity. With <TT CLASS="varname" >$_SESSION</TT >, there is no need to use the <A HREF="function.session-register.html" ><B CLASS="function" >session_register()</B ></A >, <A HREF="function.session-unregister.html" ><B CLASS="function" >session_unregister()</B ></A >, <A HREF="function.session-is-registered.html" ><B CLASS="function" >session_is_registered()</B ></A > functions. Session variables are accessible like any other variables. <TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN84251" ></A ><P ><B >Example 1. Registering a variable with $_SESSION. </B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php session_start(); // Use $HTTP_SESSION_VARS with PHP 4.0.6 or less if (!isset($_SESSION['count'])) { $_SESSION['count'] = 0; } else { $_SESSION['count']++; } ?></PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > <TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN84254" ></A ><P ><B >Example 2. Unregistering a variable with $_SESSION and register_globals disabled. </B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php session_start(); // Use $HTTP_SESSION_VARS with PHP 4.0.6 or less unset($_SESSION['count']); ?></PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > <TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN84257" ></A ><P ><B >Example 3. Unregistering a variable with register_globals enabled, after registering it using $_SESSION. </B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php session_start(); // With PHP 4.3 and later, you can also simply use the prior example. session_unregister('count'); ?></PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > </P ><P > If <A HREF="configuration.directives.html#ini.register-globals" ><TT CLASS="literal" >register_globals</TT ></A > is enabled, then each global variable can be registered as session variable. Upon a restart of a session, these variables will be restored to corresponding global variables. Since PHP must know which global variables are registered as session variables, users need to register variables with <A HREF="function.session-register.html" ><B CLASS="function" >session_register()</B ></A > function. You can avoid this by simply setting entries in <TT CLASS="varname" >$_SESSION</TT >. <DIV CLASS="caution" ><P ></P ><TABLE CLASS="caution" BORDER="1" WIDTH="100%" ><TR ><TD ALIGN="CENTER" ><B >Caution</B ></TD ></TR ><TR ><TD ALIGN="LEFT" ><P > If you are using <TT CLASS="varname" >$_SESSION</TT > and disable <A HREF="configuration.directives.html#ini.register-globals" ><TT CLASS="literal" >register_globals</TT ></A >, do not use <A HREF="function.session-register.html" ><B CLASS="function" >session_register()</B ></A >, <A HREF="function.session-is-registered.html" ><B CLASS="function" >session_is_registered()</B ></A > and <A HREF="function.session-unregister.html" ><B CLASS="function" >session_unregister()</B ></A >, if your scripts shall work in PHP 4.2 and earlier. You can use these functions in 4.3 and later. </P ><P > If you enable <A HREF="configuration.directives.html#ini.register-globals" ><TT CLASS="literal" >register_globals</TT ></A >, <A HREF="function.session-unregister.html" ><B CLASS="function" >session_unregister()</B ></A > should be used since session variables are registered as global variables when session data is deserialized. Disabling <A HREF="configuration.directives.html#ini.register-globals" ><TT CLASS="literal" >register_globals</TT ></A > is recommended for both security and performance reasons. </P ></TD ></TR ></TABLE ></DIV > <TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN84279" ></A ><P ><B >Example 4. Registering a variable with <A HREF="configuration.directives.html#ini.register-globals" ><TT CLASS="literal" >register_globals</TT ></A > enabled </B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php if (!session_is_registered('count')) { session_register("count"); $count = 0; } else { $count++; } ?></PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > </P ><P > If <A HREF="configuration.directives.html#ini.register-globals" ><TT CLASS="literal" >register_globals</TT ></A > is enabled, then the global variables and the <TT CLASS="varname" >$_SESSION</TT > entries will automatically reference the same values which were registered in the prior session instance. </P ><P > There is a defect in PHP 4.2.3 and earlier. If you register a new session variable by using <A HREF="function.session-register.html" ><B CLASS="function" >session_register()</B ></A >, the entry in the global scope and the <TT CLASS="varname" >$_SESSION</TT > entry will not reference the same value until the next <A HREF="function.session-start.html" ><B CLASS="function" >session_start()</B ></A >. I.e. a modification to the newly registered global variable will not be reflected by the <TT CLASS="varname" >$_SESSION</TT > entry. This has been corrected in PHP 4.3. </P ></DIV ><DIV CLASS="section" ><H1 CLASS="section" ><A NAME="session.idpassing" ></A >Passing the Session ID</H1 ><P > There are two methods to propagate a session id: <P ></P ><UL ><LI ><P > Cookies </P ></LI ><LI ><P > URL parameter </P ></LI ></UL > </P ><P > 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. </P ><P > PHP is capable of transforming links transparently. Unless you are using PHP 4.2 or later, you need to enable it manually when building PHP. Under UNIX, pass <A HREF="install.configure.html#install.configure.enable-trans-sid" > <TT CLASS="literal" >--enable-trans-sid</TT ></A > to configure. If this build option and the run-time option session.use_trans_sid are enabled, relative URIs will be changed to contain the session id automatically. <DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >Note: </B > The <A HREF="configuration.directives.html#ini.arg-separator.output" >arg_separator.output</A > <TT CLASS="filename" >php.ini</TT > directive allows to customize the argument seperator. For full XHTML conformance, specify &amp; there. </P ></BLOCKQUOTE ></DIV > </P ><P > Alternatively, you can use the constant <TT CLASS="literal" >SID</TT > which is always defined. If the client did not send an appropriate session cookie, it has the form <TT CLASS="literal" >session_name=session_id</TT >. Otherwise, it expands to an empty string. Thus, you can embed it unconditionally into URLs. </P ><P > The following example demonstrates how to register a variable, and how to link correctly to another page using SID. <TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN84313" ></A ><P ><B >Example 5. Counting the number of hits of a single user</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php if (!session_is_registered('count')) { session_register('count'); $count = 1; } else { $count++; } ?> Hello visitor, you have seen this page <?php echo $count; ?> times.<p> To continue, <A HREF="nextpage.php?<?php echo SID?>">click here</A></PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > </P ><P > The <TT CLASS="literal" ><?php echo SID?></TT > (<TT CLASS="literal" ><?=SID?></TT > can be used if <A HREF="configuration.directives.html#ini.short-open-tag" >short_open_tag</A > is enabled) is necessary to preserve the session id in the case that the user has disabled cookies. The <TT CLASS="literal" ><?=SID?></TT > is not necessary, if <A HREF="install.configure.html#install.configure.enable-trans-sid" > <TT CLASS="literal" >--enable-trans-sid</TT ></A > was used to compile PHP. </P ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >Note: </B > Non-relative URLs are assumed to point to external sites and hence don't append the SID, as it would be a security risk to leak the SID to a different server. </P ></BLOCKQUOTE ></DIV ></DIV ><DIV CLASS="section" ><H1 CLASS="section" ><A NAME="session.customhandler" ></A >Custom Session Handlers</H1 ><P > To implement database storage, or any other storage method, you will need to use <A HREF="function.session-set-save-handler.html" ><B CLASS="function" >session_set_save_handler()</B ></A > to create a set of user-level storage functions. </P ></DIV ></DIV ><DIV CLASS="TOC" ><DL ><DT ><B >Table of Contents</B ></DT ><DT ><A HREF="function.session-cache-expire.html" >session_cache_expire</A > -- Return current cache expire</DT ><DT ><A HREF="function.session-cache-limiter.html" >session_cache_limiter</A > -- Get and/or set the current cache limiter</DT ><DT ><A HREF="function.session-decode.html" >session_decode</A > -- Decodes session data from a string</DT ><DT ><A HREF="function.session-destroy.html" >session_destroy</A > -- Destroys all data registered to a session</DT ><DT ><A HREF="function.session-encode.html" >session_encode</A > -- Encodes the current session data as a string </DT ><DT ><A HREF="function.session-get-cookie-params.html" >session_get_cookie_params</A > -- Get the session cookie parameters </DT ><DT ><A HREF="function.session-id.html" >session_id</A > -- Get and/or set the current session id</DT ><DT ><A HREF="function.session-is-registered.html" >session_is_registered</A > -- Find out whether a global variable is registered in a session </DT ><DT ><A HREF="function.session-module-name.html" >session_module_name</A > -- Get and/or set the current session module</DT ><DT ><A HREF="function.session-name.html" >session_name</A > -- Get and/or set the current session name</DT ><DT ><A HREF="function.session-readonly.html" >session_readonly</A > -- Begin session - reinitializes frozen variables, but no writeback on request end</DT ><DT ><A HREF="function.session-register.html" >session_register</A > -- Register one or more global variables with the current session </DT ><DT ><A HREF="function.session-save-path.html" >session_save_path</A > -- Get and/or set the current session save path</DT ><DT ><A HREF="function.session-set-cookie-params.html" >session_set_cookie_params</A > -- Set the session cookie parameters </DT ><DT ><A HREF="function.session-set-save-handler.html" >session_set_save_handler</A > -- Sets user-level session storage functions </DT ><DT ><A HREF="function.session-start.html" >session_start</A > -- Initialize session data</DT ><DT ><A HREF="function.session-unregister.html" >session_unregister</A > -- Unregister a global variable from the current session </DT ><DT ><A HREF="function.session-unset.html" >session_unset</A > -- Free all session variables </DT ><DT ><A HREF="function.session-write-close.html" >session_write_close</A > -- Write session data and end session</DT ></DL ></DIV ></DIV ></DIV ><DIV CLASS="NAVFOOTER" ><HR ALIGN="LEFT" WIDTH="100%"><TABLE SUMMARY="Footer navigation table" WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" ><A HREF="function.sesam-settransaction.html" ACCESSKEY="P" >Prev</A ></TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="index.html" ACCESSKEY="H" >Home</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" ><A HREF="function.session-cache-expire.html" ACCESSKEY="N" >Next</A ></TD ></TR ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" >sesam_settransaction</TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="funcref.html" ACCESSKEY="U" >Up</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" >session_cache_expire</TD ></TR ></TABLE ></DIV ></BODY ></HTML >