documentation-3.html
来自「PHPLOB注释详细版 使用模板技术的好帮手 PHP最有用的东东了」· HTML 代码 · 共 1,596 行 · 第 1/5 页
HTML
1,596 行
<CODE>logout()</CODE> will call <CODE>unauth()</CODE> (passing <CODE>$nobody</CODE>),so the behaviour is identical (except <CODE>logout()</CODE> will alwaysclear <CODE>$this->auth["uname"]</CODE> and unregister the auth class).<P>Since V7.2: Passing $nobody to this method is deprecated.<P><DT><B>is_authenticated()</B><DD><P>Will return false, if the current authentication isinvalid or expired. Will return the authenticated uidotherwise.<P><DT><B>auth_preauth()</B><DD><P>This function can be overridden in a subclass to Auth. Itis being called as the very first step in the authenticationprocess and has the opportunity to authenticate the userwithout a loginform being displayed (by deriving all necessaryinformation telepathically, or by using cookies, or diviningthe user identities from the incestines of a dead squirrel).<P>If it returns a UID value, the user is authenticated and neitherauth_loginform() nor auth_validatelogin() arecalled. If it returns false, all goes on as usual.<P><DT><B>auth_loginform()</B><DD><P> This function must be overridden by a subclass to Auth. Itshould output HTML that creates a login screen for the user.We recommend that you use an <CODE>include()</CODE> statement to includeyour HTML file.<P><DT><B>auth_validatelogin()</B><DD><P>This function is called when the user submits the login formcreated by <CODE>auth_loginform()</CODE>. It must validate the user input.<P>If the user authenticated successfully, it must set upseveral fields within the <CODE>$auth[]</CODE> instance variable:<P><DL><DT><B>"uid"</B><DD><P>must contain the user id associated with that user.<DT><B>"uname"</B><DD><P>must contain the user name as entered by the user.<DT><B>"exp"</B><DD><P>must not be tampered with (field is maintained by<CODE>start()</CODE>, contains the time when the login expires).<DT><B>"perm"</B><DD><P>if you want to use the permission feature, youmust store the permissions of the validated user here.(Hint: due to a name conflict with sybase, "perm" is called "perms"in all the databases tables. Look for this small difference!)</DL><P>See the example below for more information.<P><DT><B>auth_refreshlogin()</B><DD><P>This function is called every <CODE>refresh</CODE> minutes. It must refreshthe authentication informations stored in <CODE>auth</CODE> array by<CODE>auth_validatelogin()</CODE> method. It is not called if theuser is logged in as nobody.<P>It must return true on success, false otherwise (i.e.: the useridis no longer valid).<P><DT><B>auth_registerform()</B><DD><P>See auth_doregister().<P><DT><B>auth_doregister()</B><DD><P>These functions mirror <CODE>auth_loginform()</CODE> and<CODE>auth_validatelogin()</CODE> in registration mode.</DL><P><H3>Internal instance methods</H3><P><P><DL><DT><B>start()</B><DD><P><P>Initialization function, does the authentication. If we arein <CODE>log</CODE> (login) mode, <CODE>auth_loginform()</CODE> iscalled to draw a login screen. When the login screen issubmitted back, <CODE>auth_validatelogin()</CODE> is called tovalidate the login. If the validation was successful, theactual page content is shown, otherwise we're back at<CODE>auth_loginform()</CODE>.<P>In <CODE>reg</CODE> mode, <CODE>auth_registerform()</CODE> is called to draw aregistration form. When the registration form is submittedback, <CODE>auth_doregister()</CODE> is called to register the user andto validate the session. If registration was successful, theactual page content is shown, otherwise we're back at<CODE>auth_registerform()</CODE>.</DL><P><H3>Example</H3><P>Use a subclass of <CODE>Auth</CODE> to provide parameters for yourauthentication class and to implement your own <CODE>auth_*</CODE> functions.<P><BLOCKQUOTE><CODE><HR><PRE>class My_Auth extends Auth { var $classname = "My_Auth"; # Object serialization support var $lifetime = 15; ## DB_Sql subclass and database table to use var $database_class = "DB_Session"; var $database_table = "auth_user"; ## Some magic value to make our uids harder to guess. var $magic = "Abracadabra"; ## Use an own login form function auth_loginform() { global $sess; include("loginform.ihtml"); } function auth_validatelogin() { global $username, $password; ## form variables from loginform.ihtml ## If authentication fails, loginform.html will ## find $this->auth["uname"] set and use it. $this->auth["uname"]=$username; ## Value to return in case auth fails. $uid = false; ## Check the database for this user and password pair. $query = sprintf( "select * from %s where username = '%s' and password = '%s'", $this->database_table, addslashes($username), addslashes($password) ); $this->db->query($query); ## If we found a matching user, grab the uid and permissions... while($this->db->next_record()) { ## Required. $uid = $this->db->f("uid"); ## Optional, for the perm feature. $this->auth["perm"] = $this->db->f("perms"); ## if you use perm feature be aware, that the db-field in our ## example table is called "perms" due to a name conflict with sybase } return $uid; }}</PRE><HR></CODE></BLOCKQUOTE><P>Your <CODE>loginform.ihtml</CODE> contains HTML and PHP code to draw a loginform. <CODE>$this->auth["uname"]</CODE> will be empty on the first loginattempt and set on all further login attempts. You can use thisto detect repeated login attempts and display an appropriateerror message. You must print the result of <CODE>$this->url()</CODE> tocreate your forms action attribute.<P>See the provided <CODE>loginform.ihtml</CODE> for an example.<P>Use the page management functions (see above) to use yourauthentication subclass. The feature name for authenticationmanagement is <CODE>auth</CODE>; provide the name of your <CODE>Auth</CODE> subclass asa parameter to the <CODE>auth</CODE> feature. The <CODE>auth</CODE> feature requires the<CODE>sess</CODE> feature:<P><BLOCKQUOTE><CODE><HR><PRE> page_open(array("sess" => "My_Session", "auth" => "My_Auth"));</PRE><HR></CODE></BLOCKQUOTE><P><H3>Using default authentication</H3><P>Many applications want to use <CODE>$auth</CODE> and <CODE>$perm</CODE>objects to protect functionality on a page, but do want tomake the unprotected part of this page available to userswith no account. This presents a kind of dilemma, because youneed <CODE>$auth</CODE> and <CODE>$perm</CODE> objects to protectfunctionality on a page, but you don't want a login screen toappear by default.<P>Default authentication solves this dilemma by providing aspecial uid and uname "nobody", which is guaranteed to failevery permission check. If you set the <CODE>nobody</CODE> flag,<CODE>$auth</CODE> will not create a login screen to force a user toauthenticate, but will authenticate the user silently as<CODE>nobody</CODE>. The application must offer a login button orother facility for users with accounts to change from thatid to their real user id.<P>To use default authentication, create a subclass of <CODE>My_Auth</CODE>as shown above with the <CODE>nobody</CODE> flag set (<EM>Note:</EM> No needto extend in two steps. The only important thing here is thatthe <CODE>nobody</CODE> flag is set.)<P><BLOCKQUOTE><CODE><HR><PRE>class My_Default_Auth extends My_Auth { var $classname = "My_Default_Auth"; var $nobody = true;}</PRE><HR></CODE></BLOCKQUOTE><P>To create a page that uses default authentication, use the pagemanagement functions. Check for relogin requests with the<CODE>login_if()</CODE> function. Create a relogin link on your page.<P><BLOCKQUOTE><CODE><HR><PRE><?php // using Default Authentication page_open(array("sess" => "My_Session", "auth" => "My_Default_Auth")); $auth->login_if($again); if ($auth->auth["uid"] == "nobody"):?> <A HREF="<?php $sess->purl("$PHP_SELF?again=yes") ?>">Relogin</A> to this page.<?php endif ?></PRE><HR></CODE></BLOCKQUOTE><P><H3>Using Challenge-Response Authentication</H3><P>As distributed, <CODE>local.inc</CODE> contains an example classnamed <CODE>Example_Challenge_Auth</CODE>, which uses aChallenge-Response authentication scheme. If the clientbrowser supports Javascript, this login screen does nottransmit passwords in clear over the network. If the clientdoes not support Javascript, login is still possible, butpasswords are transmitted in clear, as regular <CODE>Example_Auth</CODE>always does.<P><CODE>Example_Challenge_Auth</CODE> is there to demonstrate advancedusage of PHP and Javascript and to show off the flexibilityof the library base classes: The Challenge-Responseauthentication scheme has been implemented completely andnaturally in local.inc by subclassing <CODE>Auth</CODE> with noalteration of library code.<P><CODE>Example_Challenge_Auth</CODE> includes <CODE>crloginform.ihtml</CODE>. Italso requires that the file <CODE>md5.js</CODE> is present in thedocument root directory of your web server. That file containsan implementation of the MD5 message digest algorithm done byHenri Torgemane. The basic idea behind this authenticationscheme is simple: <CODE>$auth->auth_loginform()</CODE> creates achallenge value which is incorporated into this form. Whenthe user tries to submit the form,MD5("username:password:challenge") is calculated and filledinto the reply field. The password field is erased. Theserver can calculate the expected reply from the usernamereceived, the password in the database and the challenge,which it knows. It can compare the expected reply to theactual reply value. If they match, the user is authenticated.<P>If the reply field is empty and password is set, the serverknows that the client cannot do Javascript. The user can still beauthenticated, but the password is visible on the network. <P>The class is a dropin-replacement for <CODE>Example_Auth</CODE>.<P><H3>The complete guide to authentication and user variables</H3><P><P>This feature has originally been written for the PHPLIB mailinglist by Kristian K鰄ntopp and was included into thedocumentation later.<P><H3>How is the <CODE>Auth</CODE> class used usually?</H3><P><P>Usually, you write code like this into the top of the page youwant to protect:<P><BLOCKQUOTE><CODE><HR><PRE><?phppage_open(array( "sess" => "My_Session", "auth" => "My_Auth"));?><!-- your code here --><?phppage_close()?></PRE><HR></CODE></BLOCKQUOTE><P><H3>How does <CODE>$auth</CODE> work internally?</H3><P><P>When you access this page, the call <CODE>to page_open()</CODE> is beingmade as the first thing on that page. <CODE>page_open()</CODE> createsan instance of <CODE>My_Auth</CODE> named <CODE>$auth</CODE> and starts it.<CODE>$auth</CODE> then detects that you are not authenticated (how itdoes, I will explain below) and displays <CODE>loginform.ihtml</CODE>.$auth then exits the interpreter, so that <!-- your code here--> is never being executed or displayed.<P>The user now sits in front of a <CODE>loginform.ihtml</CODE> screen,which is shown under the URL of the page the user originallytried to access. The loginform has an action URL, which justpoints back to itself.<P>When the user filled out the loginform and submits it, the verysame URL is requested and the above <CODE>page_open()</CODE> isreexecuted, but this time a username and a password aresubmitted. When the <CODE>$auth</CODE> object is created and started, itdetects these parameters and validates them, resulting in eithera NULL value or a valid user id. If the validation failed,creating an empty user id, the loginform is displayed again andthe interpreter exits. Again <!-- your code here --> is notexecuted.<P>If a UID is returned, that UID and a timestamp are being madepersistent in that session and <CODE>$auth</CODE> returns control to<CODE>page_open()</CODE>. When <CODE>page_open()</CODE> finishes, which it mayor may not do, depending on the presence and result of anoptional <CODE>$perm</CODE> check, <!-- your code here --> is beingexecuted or shown.<P>Later calls to other pages or the same page check for thepresence of the UID and the timestamp in the sessions data. Ifthe UID is present and the timestamp is still valid, the UID isretained and the timestamp is refreshed. On <CODE>page_close()</CODE>both are written back to the user database (Note: Authenticatedpages REQUIRE that you <CODE>page_close()</CODE> them, even when youaccess them read-only or the timestamp will not be refreshed).<P>If
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?