📄 function.session-register.html
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html> <head> <title>Register one or more global variables with the current session</title> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> </head> <body><div style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="function.session-regenerate-id.html">session_regenerate_id</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.session-save-path.html">session_save_path</a></div> <div class="up"><a href="ref.session.html">Session Functions</a></div> <div class="home"><a href="index.html">PHP Manual</a></div></div><hr /><div id="function.session-register" class="refentry"> <div class="refnamediv"> <h1 class="refname">session_register</h1> <p class="verinfo">(PHP 4, PHP 5)</p><p class="refpurpose"><span class="refname">session_register</span> — <span class="dc-title">Register one or more global variables with the current session</span></p> </div> <div class="refsect1 description"> <h3 class="title">Description</h3> <div class="methodsynopsis dc-description"> <span class="type">bool</span> <span class="methodname"><b><b>session_register</b></b></span> ( <span class="methodparam"><span class="type"><a href="language.pseudo-types.html#language.types.mixed" class="type mixed">mixed</a></span> <tt class="parameter">$name</tt></span> [, <span class="methodparam"><span class="type"><a href="language.pseudo-types.html#language.types.mixed" class="type mixed">mixed</a></span> <tt class="parameter">$...</tt></span> ] )</div> <p class="para rdfs-comment"> <b>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>session_register()</b> registers the global variable with that name in the current session. </p> <p class="para"> You can also create a session variable by simply setting the appropriate member of the <var class="varname"><a href="reserved.variables.session.html" class="classname">$_SESSION</a></var> or <var class="varname">$HTTP_SESSION_VARS</var> (PHP < 4.1.0) array. <div class="informalexample"> <div class="example-contents"><div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB"><?php<br /></span><span style="color: #FF8000">// Use of session_register() is deprecated<br /></span><span style="color: #0000BB">$barney </span><span style="color: #007700">= </span><span style="color: #DD0000">"A big purple dinosaur."</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">session_register</span><span style="color: #007700">(</span><span style="color: #DD0000">"barney"</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">// Use of $_SESSION is preferred, as of PHP 4.1.0<br /></span><span style="color: #0000BB">$_SESSION</span><span style="color: #007700">[</span><span style="color: #DD0000">"zim"</span><span style="color: #007700">] = </span><span style="color: #DD0000">"An invader from another planet."</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">// The old way was to use $HTTP_SESSION_VARS<br /></span><span style="color: #0000BB">$HTTP_SESSION_VARS</span><span style="color: #007700">[</span><span style="color: #DD0000">"spongebob"</span><span style="color: #007700">] = </span><span style="color: #DD0000">"He's got square pants."</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">?></span></span></code></div> </div> </div> </p> <p class="para"> If <a href="function.session-start.html" class="function">session_start()</a> was not called before this function is called, an implicit call to <a href="function.session-start.html" class="function">session_start()</a> with no parameters will be made. <var class="varname"><a href="reserved.variables.session.html" class="classname">$_SESSION</a></var> does not mimic this behavior and requires <a href="function.session-start.html" class="function">session_start()</a> before use. </p> </div> <div class="refsect1 parameters"> <h3 class="title">Parameters</h3> <p class="para"> <dl> <dt> <span class="term"><i><tt class="parameter">name</tt></i></span> <dd> <p class="para"> A string holding the name of a variable or an array consisting of variable names or other arrays. </p> </dd> </dt> <dt> <span class="term"><i><tt class="parameter">...</tt></i></span> <dd> <p class="para"> </p> </dd> </dt> </dl> </p> </div> <div class="refsect1 returnvalues"> <h3 class="title">Return Values</h3> <p class="para"> Returns <b><tt>TRUE</tt></b> on success or <b><tt>FALSE</tt></b> on failure. </p> </div> <div class="refsect1 notes"> <h3 class="title">Notes</h3> <div class="caution"><b class="caution">Caution</b> <p class="para"> If you want your script to work regardless of <a href="ini.core.html#ini.register-globals" class="link">register_globals</a>, you need to instead use the <var class="varname"><a href="reserved.variables.session.html" class="classname">$_SESSION</a></var> array as <var class="varname"><a href="reserved.variables.session.html" class="classname">$_SESSION</a></var> entries are automatically registered. If your script uses <b>session_register()</b>, it will not work in environments where the PHP directive <a href="ini.core.html#ini.register-globals" class="link">register_globals</a> is disabled. </p> </div> <blockquote><p><b class="note">Note</b>: <b>register_globals: importantnote</b><br />As of PHP 4.2.0, the default value for the PHP directive<a href="ini.core.html#ini.register-globals" class="link">register_globals</a> is <em class="emphasis">off</em>, and it was completely removed as of PHP 6.0.0. The PHP communitydiscourages developers from relying on this directive, and encourages the useof other means, such as the <a href="language.variables.predefined.html" class="link">superglobals</a>.<br /></p></blockquote> <div class="caution"><b class="caution">Caution</b> <p class="para"> This registers a <em class="emphasis">global</em> 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" class="link"><strong class="command">global</strong></a> keyword or the <var class="varname"><a href="reserved.variables.globals.html" class="classname">$GLOBALS[]</a></var> array, or use the special session arrays as noted below. </p> </div> <div class="caution"><b class="caution">Caution</b> <p class="para"> If you are using <var class="varname"><a href="reserved.variables.session.html" class="classname">$_SESSION</a></var> (or <var class="varname">$HTTP_SESSION_VARS</var>), do not use <b>session_register()</b>, <a href="function.session-is-registered.html" class="function">session_is_registered()</a>, and <a href="function.session-unregister.html" class="function">session_unregister()</a>. </p> </div> <blockquote><p><b class="note">Note</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 <i>resource</i> in their function definition. A list of functions that return resources are available in the <a href="resource.html" class="link">resource types</a> appendix. <br /> If <var class="varname"><a href="reserved.variables.session.html" class="classname">$_SESSION</a></var> (or <var class="varname">$HTTP_SESSION_VARS</var> for PHP 4.0.6 or less) is used, assign values to <var class="varname"><a href="reserved.variables.session.html" class="classname">$_SESSION</a></var>. For example: $_SESSION['var'] = 'ABC'; <br /> </p></blockquote> </div> <div class="refsect1 seealso"> <h3 class="title">See Also</h3> <p class="para"> <ul class="simplelist"> <li class="member"><a href="function.session-is-registered.html" class="function" rel="rdfs-seeAlso">session_is_registered()</a></li> <li class="member"><a href="function.session-unregister.html" class="function" rel="rdfs-seeAlso">session_unregister()</a></li> <li class="member"><var class="varname"><a href="reserved.variables.session.html" class="classname">$_SESSION</a></var></li> </ul> </p> </div></div><hr /><div style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="function.session-regenerate-id.html">session_regenerate_id</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.session-save-path.html">session_save_path</a></div> <div class="up"><a href="ref.session.html">Session Functions</a></div> <div class="home"><a href="index.html">PHP Manual</a></div></div></body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -