<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <HTML ><HEAD ><TITLE >session_register</TITLE ><META NAME="GENERATOR" CONTENT="Modular DocBook HTML Stylesheet Version 1.79"><LINK REL="HOME" TITLE="PHP 手册" HREF="index.html"><LINK REL="UP" TITLE="Session 会话处理函数" HREF="ref.session.html"><LINK REL="PREVIOUS" TITLE="session_regenerate_id" HREF="function.session-regenerate-id.html"><LINK REL="NEXT" TITLE="session_save_path" HREF="function.session-save-path.html"><META HTTP-EQUIV="Content-type" CONTENT="text/html; charset=UTF-8"></HEAD ><BODY CLASS="refentry" 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 手册</TH ></TR ><TR ><TD WIDTH="10%" ALIGN="left" VALIGN="bottom" ><A HREF="function.session-regenerate-id.html" ACCESSKEY="P" >上一页</A ></TD ><TD WIDTH="80%" ALIGN="center" VALIGN="bottom" ></TD ><TD WIDTH="10%" ALIGN="right" VALIGN="bottom" ><A HREF="function.session-save-path.html" ACCESSKEY="N" >下一页</A ></TD ></TR ></TABLE ><HR ALIGN="LEFT" WIDTH="100%"></DIV ><H1 ><A NAME="function.session-register" ></A >session_register</H1 ><DIV CLASS="refnamediv" ><A NAME="AEN208579" ></A ><P > (PHP 4, PHP 5)</P >session_register -- Register one or more global variables with the current session </DIV ><DIV CLASS="refsect1" ><A NAME="AEN208582" ></A ><H2 >Description</H2 >bool <B CLASS="methodname" >session_register</B > ( mixed name [, mixed ...] )<BR ></BR ><P > <B CLASS="function" >session_register()</B > accepts a variable number of arguments, any of which can be either a string holding the name of a variable or an array consisting of variable names or other arrays. For each name, <B CLASS="function" >session_register()</B > registers the global variable with that name in the current session. </P ><DIV CLASS="caution" ><P ></P ><TABLE CLASS="caution" BORDER="1" WIDTH="100%" ><TR ><TD ALIGN="CENTER" ><B >小心</B ></TD ></TR ><TR ><TD ALIGN="LEFT" ><P > If you want your script to work regardless of <A HREF="ini.core.html#ini.register-globals" >register_globals</A >, you need to instead use the <A HREF="reserved.variables.html#reserved.variables.session" >$_SESSION</A > array as <CODE CLASS="varname" >$_SESSION</CODE > entries are automatically registered. If your script uses <B CLASS="function" >session_register()</B >, it will not work in environments where the PHP directive <A HREF="ini.core.html#ini.register-globals" >register_globals</A > is disabled. </P ></TD ></TR ></TABLE ></DIV ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >register_globals 重要说明: </B >自 PHP 4.2.0 起,PHP 中的选项 <A HREF="ini.core.html#ini.register-globals" >register_globals</A > 的默认值被设为 <SPAN CLASS="emphasis" ><I CLASS="emphasis" >off</I ></SPAN >,并且自 PHP 6.0.0 起会被彻底去除。PHP 社区鼓励大家不要依赖于这个选项,用其它方法替代,例如<A HREF="language.variables.predefined.html" >superglobals</A >。</P ></BLOCKQUOTE ></DIV ><DIV CLASS="caution" ><P ></P ><TABLE CLASS="caution" BORDER="1" WIDTH="100%" ><TR ><TD ALIGN="CENTER" ><B >小心</B ></TD ></TR ><TR ><TD ALIGN="LEFT" ><P > This registers a <SPAN CLASS="emphasis" ><I CLASS="emphasis" >global</I ></SPAN > variable. If you want to register a session variable from within a function, you need to make sure to make it global using the <A HREF="language.variables.scope.html" ><B CLASS="command" >global</B ></A > keyword or the <CODE CLASS="varname" >$GLOBALS[]</CODE > array, or use the special session arrays as noted below. </P ></TD ></TR ></TABLE ></DIV ><DIV CLASS="caution" ><P ></P ><TABLE CLASS="caution" BORDER="1" WIDTH="100%" ><TR ><TD ALIGN="CENTER" ><B >小心</B ></TD ></TR ><TR ><TD ALIGN="LEFT" ><P > If you are using <CODE CLASS="varname" >$_SESSION</CODE > (or <CODE CLASS="varname" >$HTTP_SESSION_VARS</CODE >), do not use <B CLASS="function" >session_register()</B >, <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 >. </P ></TD ></TR ></TABLE ></DIV ><P > This function returns <TT CLASS="constant" ><B >TRUE</B ></TT > when all of the variables are successfully registered with the session. </P ><P > If <A HREF="function.session-start.html" ><B CLASS="function" >session_start()</B ></A > was not called before this function is called, an implicit call to <A HREF="function.session-start.html" ><B CLASS="function" >session_start()</B ></A > with no parameters will be made. <CODE CLASS="varname" >$_SESSION</CODE > does not mimic this behavior and requires <A HREF="function.session-start.html" ><B CLASS="function" >session_start()</B ></A > before use. </P ><P > You can also create a session variable by simply setting the appropriate member of the <CODE CLASS="varname" >$_SESSION</CODE > or <CODE CLASS="varname" >$HTTP_SESSION_VARS</CODE > (PHP < 4.1.0) array. <DIV CLASS="informalexample" ><P ></P ><A NAME="AEN208632" ></A ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><code><font color="#000000"> <font color="#0000BB"><?php<br /></font><font color="#FF8000">// Use of session_register() is deprecated<br /></font><font color="#0000BB">$barney </font><font color="#007700">= </font><font color="#DD0000">"A big purple dinosaur."</font><font color="#007700">;<br /></font><font color="#0000BB">session_register</font><font color="#007700">(</font><font color="#DD0000">"barney"</font><font color="#007700">);<br /><br /></font><font color="#FF8000">// Use of $_SESSION is preferred, as of PHP 4.1.0<br /></font><font color="#0000BB">$_SESSION</font><font color="#007700">[</font><font color="#DD0000">"zim"</font><font color="#007700">] = </font><font color="#DD0000">"An invader from another planet."</font><font color="#007700">;<br /><br /></font><font color="#FF8000">// The old way was to use $HTTP_SESSION_VARS<br /></font><font color="#0000BB">$HTTP_SESSION_VARS</font><font color="#007700">[</font><font color="#DD0000">"spongebob"</font><font color="#007700">] = </font><font color="#DD0000">"He's got square pants."</font><font color="#007700">;<br /></font><font color="#0000BB">?></font> </font> </code></TD ></TR ></TABLE ><P ></P ></DIV > </P ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >注意: </B > It is currently impossible to register resource variables in a session. For example, you cannot create a connection to a database and store the connection id as a session variable and expect the connection to still be valid the next time the session is restored. PHP functions that return a resource are identified by having a return type of <TT CLASS="literal" >resource</TT > in their function definition. A list of functions that return resources are available in the <A HREF="resource.html" >resource types</A > appendix. </P ><P > If <CODE CLASS="varname" >$_SESSION</CODE > (or <CODE CLASS="varname" >$HTTP_SESSION_VARS</CODE > for PHP 4.0.6 or less) is used, assign values to <CODE CLASS="varname" >$_SESSION</CODE >. For example: $_SESSION['var'] = 'ABC'; </P ></BLOCKQUOTE ></DIV ><P > See also <A HREF="function.session-is-registered.html" ><B CLASS="function" >session_is_registered()</B ></A >, <A HREF="function.session-unregister.html" ><B CLASS="function" >session_unregister()</B ></A >, and <A HREF="reserved.variables.html#reserved.variables.session" >$_SESSION</A >. </P ></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.session-regenerate-id.html" ACCESSKEY="P" >上一页</A ></TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="index.html" ACCESSKEY="H" >起始页</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" ><A HREF="function.session-save-path.html" ACCESSKEY="N" >下一页</A ></TD ></TR ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" >session_regenerate_id</TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="ref.session.html" ACCESSKEY="U" >上一级</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" >session_save_path</TD ></TR ></TABLE ></DIV ></BODY ></HTML >