documentation-3.html
来自「PHPLOB注释详细版 使用模板技术的好帮手 PHP最有用的东东了」· HTML 代码 · 共 1,596 行 · 第 1/5 页
HTML
1,596 行
<BLOCKQUOTE><CODE><HR><PRE>class My_Ldap extends CT_Ldap { var $classname = "My_Ldap"; var $ldap_host = "localhost"; var $ldap_port = 389; var $basedn = "dc=your-domain, dc=com"; var $rootdn = "cn=root, dc=your-domain, dc=com"; var $rootpw = "secret"; var $objclass = "phplibdata";}</PRE><HR></CODE></BLOCKQUOTE><P>You can then use My_Ldap in class Session. Reference itby putting "My_Ldap" in the "that_class" variable.<P><P><H2><A NAME="ss3.8">3.8 Session</A></H2><P><P>The session class keeps a list of global variable names andprovides a set of functions to load and save these variablesfrom and to a data storage container (we will call it containerfor shortness). The named variables may be scalarvariables (strings, integers and floats) or arrays. Objects arehandled as well, provided they implement two instance variablesnaming their class and enumerating their (persistent) slots.<P><H3>Instance variables</H3><P><P><CENTER><TABLE BORDER><TR><TD><BR>classname</TD><TD>Serialization helper: The name of this class.</TD></TR><TR><TD>magic</TD><TD>A secret string used in ID creation. Change it!</TD></TR><TR><TD>mode </TD><TD>Mode of Session ID propagation. Either <CODE>cookie</CODE> or <CODE>get</CODE>.</TD></TR><TR><TD>fallback_mode</TD><TD>Mode of Session ID propagation should <CODE>$mode</CODE> not work. Set <CODE>$mode</CODE> to <CODE>cookie</CODE> and <CODE>$fallback_mode</CODE> to <CODE>get</CODE>.</TD></TR><TR><TD>lifetime </TD><TD>Lifetime of the session cookie in minutes or 0 to use session cookies.</TD></TR><TR><TD>gc_time</TD><TD>Garbage collection tuning parameter, see below.</TD></TR><TR><TD>gc_probability</TD><TD>Garbage collection tuning parameter, see below.</TD></TR><TR><TD>allowcache</TD><TD>Control caching of session pages. If set to <CODE>passive</CODE> (also the default), no cache-control headers are being sent. If set to <CODE>no</CODE>, the page is not cached under HTTP/1.1 or HTTP/1.0; if set to <CODE>public</CODE> , the page is publically cached under HTTP/1.1 and HTTP/1.0; if set to <CODE>private</CODE> , the page is privately cached under HTTP/1.1 and not cached under HTTP/1.0</TD></TR><TR><TD>allowcache_expire</TD><TD>When caching is allowed, the pages can be cached for this many minutes.</TD></TR><TR><TD>that_class</TD><TD>A classname. Session uses this class to store and retrieve data.</TD></TR><TR><TD>auto_init</TD><TD>The file to be loaded on session establishment.</TD></TR><TR><TD>secure_auto_init</TD><TD>Set to 0, if all pages always callpage_close() (This is never the case!).</TD></TR><TR><TD><CAPTION>Accessible instance variables.</CAPTION></TD></TR></TABLE></CENTER><P><CENTER><TABLE BORDER><TR><TD><BR>pt</TD><TD>Internal array of names of persistent variables.</TD></TR><TR><TD>in</TD><TD>Flag: If set, auto_init has been executed.</TD></TR><TR><TD>name</TD><TD>A tag (name) for the session type.</TD></TR><TR><TD>id</TD><TD>Id of the current session.</TD></TR><TR><TD>that</TD><TD>Container object instance.</TD></TR><TR><TD><CAPTION>Internal instance variables.</CAPTION></TD></TR></TABLE></CENTER><P><H3>Instance methods</H3><P><P><H3>Accessible instance methods</H3><P><P><DL><DT><B>register($varname)</B><DD><P>Registers a global variable name as a session variable. Thename may identify a scalar variable, an array or an object.If an object is to be made persistent, it must have twoinstance variables:<P><DL><DT><B>classname</B><DD><P>A string with the name of the objects class.<DT><B> persistent_slots</B><DD><P>An array with the names of all object slots to save.</DL><P><DT><B>unregister($varname)</B><DD><P>Unregisters a global variable name as a session variable.The variable is not deleted, but its value will be lostat the end of a page. It is no longer saved to the database.<P><DT><B>is_registered($varname)</B><DD><P>Returns true if the variable named $varname is registeredwith the session, false otherwise.<P><DT><B>delete()</B><DD><P>Destroy the current session and put_id() the current sessionid.<P>After <CODE>delete()</CODE> has been executed, all session data hasbeen removed from the database. Also, the session object isunusable on this page. Consequently, <CODE>page_close()</CODE> maynot be called for this session. Session variables are stillavailable on this page, even after the <CODE>delete()</CODE>, butwill be lost on the following pages.<P>In cookie mode, it is possible to <CODE>page_open()</CODE> a newsession after <CODE>delete()</CODE> has been called, if no HTML hasbeen output so far so that the new cookie can be set. If youdo this, you can also re-register some of the previoussession variables and can call <CODE>page_close()</CODE> for the newsession. This allows you to change the session on the flyand selectively carry over session data from the previoussession.<P><DT><B>url($url)</B><DD><P>Return an URL referencing the current session. If in <CODE>get</CODE>mode, the current session id is attached to this URL, else theURL is returned unmodified.<P><DT><B>purl($url)</B><DD><P>A shorthand for <CODE>print $this->url($url);</CODE><P><DT><B>self_url()</B><DD><P>Return an URL referencing the current page, including<CODE>PHP_SELF</CODE> and <CODE>QUERY_STRING</CODE> information.If in <CODE>get</CODE> mode, the session id is included.<P><DT><B>pself_url()</B><DD><P>A shorthand for <CODE>print $this->self_url()</CODE>.<P><DT><B>hidden_session()</B><DD><P>Adds a hidden form element containing the session name and id.<P><DT><B>add_query($qarray)</B><DD><P><P>Return string to be appended to the current URL for parametersin GET query format. Intended usage is like this:<P><BLOCKQUOTE><CODE><HR><PRE><a href="<<?$sess->pself_url().$sess->padd_query(array("again"=>"yes"))?>"> Reload</a> and log in?</PRE><HR></CODE></BLOCKQUOTE><P><DT><B>padd_query($qarray)</B><DD><P><P>A shorthand for <CODE>print $this-> add_query($qarray)</CODE>.<P><DT><B>reimport_get_vars()</B><DD><P><P>When a <CODE>FORM</CODE> variable is made persistent, that form variable isimported into PHP, then page_open() is being called andthe new variable value is overwritten from the database. The<CODE>FORM</CODE> value is lost.<P><P>If you had enabled <CODE>track_vars</CODE> and were accessing<CODE>HTTP_GET_VARS</CODE> directly, which is recommended,this were not a problem. Some legacy scripts rely on persistent<CODE>FORM</CODE> input variables, though.<P><P>These scripts may call the appropriate<CODE>reimport</CODE>_x_<CODE>vars()</CODE> functions. Thesefunctions will re-read the tracked variable arrays andreinitialize the appropriate global variables after sessionvariables have been restored.<P><P>Use of this function is discouraged.<P><DT><B>reimport_post_vars()</B><DD><P>See <CODE>reimport_get_vars()</CODE>.<P><DT><B>reimport_cookie_vars()</B><DD><P>See <CODE>reimport_get_vars()</CODE>.<P><DT><B>set_container()</B><DD><P>You shall not call this function directly. It is called back by the<CODE>start()</CODE> function of <CODE>Session()</CODE> during initializiation.It is documented so that you can override its implementation inyour subclass of <CODE>Session</CODE> if you know what you are doing.<P>This function creates and starts the container class used bythis instance of session.<P><DT><B>set_tokenname()</B><DD><P>You shall not call this function directly. It is called back by the<CODE>start()</CODE> function of <CODE>Session()</CODE> during initializiation.It is documented so that you can override its implementation inyour subclass of <CODE>Session</CODE> if you know what you are doing.<P>This function determines and sets the internal session name.<P><DT><B>release_token()</B><DD><P>You shall not call this function directly. It is called back by the<CODE>start()</CODE> function of <CODE>Session()</CODE> during initializiation.It is documented so that you can override its implementation inyour subclass of <CODE>Session</CODE> if you know what you are doing.<P>This function determines the current method of sessionpropagation and determines if a new session token has to begenerated.<P><DT><B>put_headers()</B><DD><P>You shall not call this function directly. It is called back by the<CODE>start()</CODE> function of <CODE>Session()</CODE> during initializiation.It is documented so that you can override its implementation inyour subclass of <CODE>Session</CODE> if you know what you are doing.<P>This function determines which header lines are to be generatedby the session, including cache control headers.</DL><P><H3>Internal instance methods</H3><P><P><DL><DT><B>get_id()</B><DD><P>See <CODE>get_id()</CODE>.<P><DT><B>get_id($id_to_use)</B><DD><P>get_id() is used internally to determine a sessionidentifier. Currently, a session identifier is a hex numberof 32 characters (128 bits) and it is generated bymd5(uniqid($this->magic)) to make it hard to guess.<P>get_id() may be called with an optional session id to use asa parameter. This is useful if you want to change a sessionid without breaking the session (taking over an old, leftover session).<P>get_id() can be overwritten by a subclass, if you want adifferent system to create session ids. For example, someapplications want to use a constant session id that is notpropagated to the client to use a shared pool of persistentvariables (a guestbook for example). These applications needlocking (to be implemented soon).<P><DT><B>put_id()</B><DD><P>put_id() is used internally to "unuse" a session it. At themoment it deletes the client side cookie and deletes$HTTP_COOKIE_VAR[$this->name] for that cookie. The variable${$this->name} is <EM>not</EM> deleted.<P><DT><B>serialize($prefix, &$str)</B><DD><P>serialize() is used internally to append to str all PHPcode needed to reconstruct the variable named in prefix.<P><DT><B>freeze()</B><DD><P>freeze() serializes all register()ed variables and writesthe resulting code into the database, tagged with thecurrent session id and the current session name.<P><DT><B>thaw()</B><DD><P>thaw() loads a set of freeze()ed variables for the currentsession id and session name out of the database andrecreates them.<P><DT><B>gc()</B><DD><P>The <CODE>active_sessions</CODE> table contains one row foreach session. That row is uniquely identified by the <CODE>sid</CODE>and <CODE>name</CODE> values (<CODE>name</CODE> is the name of the sessionclass that has written the row). Each time that row is written,the column <CODE>changed</CODE> is updated with the current time.<P>The gc() function deletes all rows that are older than<CODE>gc_time</CODE> minutes and have a matching <CODE>name</CODE> field. For speed reasons, gc() is not not called every timean update to <CODE>active_sessions</CODE> is being made.Instead it is called randomly with a probability of<CODE>gc_probability</CODE>.<P><DT><B>reimport_any_vars($arrayname)</B><DD><P>Used to implement the three official reimport functions.<P><DT><B>start()</B><DD><P>Initialization function, to be called after objectinstantiation. Calls get_id() to get the current session id,creates a database connection, then calls thaw() to load allsession variables. Randomly activates gc(). Checks <CODE>allowcache</CODE>to send proper headers to control browser caching.<P></DL><P><P><H3>Example</H3><P>Use a subclass to provide the appropriate parameters to yoursession. Usually your subclass looks like this:<P><BLOCKQUOTE><CODE><HR><PRE>class My_Session extends Session { var $classname = "My_Session"; ## Persistence support var $mode = "cookie"; var $lifetime = 0; ## use session cookies ## which container to use var $that_class = "Session_sql";}</PRE><HR></CODE></BLOCKQUOTE><P>Remember that you have to provide a <CODE>DB_Sql</CODE> subclasswith the parameters needed to access your database.<P>Use the page management functions (see above) to use yoursession subclass. The feature name for session management is<CODE>sess</CODE>; provide the name of your session subclass as aparameter to the sess feature:<P><BLOCKQUOTE><CODE><HR><PRE> page_open(array("sess" => "My_Session"));</PRE><HR></CODE></BLOCKQUOTE><P>Use the <CODE>register()</CODE> instance method to register variables aspersistent. If <CODE>$sess</CODE> is your session object, use<P><BLOCKQUOTE><CODE><HR><PRE>$sess->register("s");</PRE><HR></CODE></BLOCKQUOTE><P>to make the global variable <CODE>$s</CODE> persistent. <CODE>$s</CODE> may be ascalar value, an array or an object with persistence supportslots.<P>Do not use the instance methods <CODE>freeze()</CODE> and <CODE>thaw()</CODE>directly, but use the page management functions instead.<P>To have some pages cached and others not cached, use multipleinstances of the session object. For example, for those pagesthat should be cached, use a session object instance like<P><BLOCKQUOTE><CODE><HR><PRE>class My_Cached_Session extends My_Session { ## pages that use this session instance are cached. var $allowcache = "private";}</PRE><HR></CODE></BLOCKQUOTE><P>Be careful when using the <CODE>public</CODE> cache option. Publically cached pagesmay be accessible to unauthenticated users. The <CODE>private</CODE> cache optionprevents unauthenticated access, but is only functional in HTTP/1.1 browsers.<P><H3>Using "auto_init"</H3>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?