features.http-auth.html
来自「php的帮助文档,涉及到PHP的案例和基本语法,以及实际应用内容」· HTML 代码 · 共 195 行 · 第 1/2 页
HTML
195 行
seem very picky about the order of the headers. Sending the <em class="emphasis">WWW-Authenticate</em> header before the <i>HTTP/1.0 401</i> header seems to do the trick for now. </p> <p class="simpara"> As of PHP 4.3.0, in order to prevent someone from writing a script which reveals the password for a page that was authenticated through a traditional external mechanism, the PHP_AUTH variables will not be set if external authentication is enabled for that particular page and <a href="features.safe-mode.html#ini.safe-mode" class="link">safe mode</a> is enabled. Regardless, <var class="varname">REMOTE_USER</var> can be used to identify the externally-authenticated user. So, you can use <var class="varname"><a href="reserved.variables.server.html" class="classname">$_SERVER['REMOTE_USER']</a></var>. </p> <blockquote><p><b class="note">Note</b>: <b>Configuration Note</b><br /> PHP uses the presence of an <i>AuthType</i> directive to determine whether external authentication is in effect. <br /> </p></blockquote> <p class="simpara"> Note, however, that the above does not prevent someone who controls a non-authenticated URL from stealing passwords from authenticated URLs on the same server. </p> <p class="simpara"> Both Netscape Navigator and Internet Explorer will clear the local browser window's authentication cache for the realm upon receiving a server response of 401. This can effectively "log out" a user, forcing them to re-enter their username and password. Some people use this to "time out" logins, or provide a "log-out" button. </p> <p class="para"> <div class="example"> <p><b>Example #3 HTTP Authentication example forcing a new name/password</b></p> <div class="example-contents"><div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB"><?php<br /></span><span style="color: #007700">function </span><span style="color: #0000BB">authenticate</span><span style="color: #007700">() {<br /> </span><span style="color: #0000BB">header</span><span style="color: #007700">(</span><span style="color: #DD0000">'WWW-Authenticate: Basic realm="Test Authentication System"'</span><span style="color: #007700">);<br /> </span><span style="color: #0000BB">header</span><span style="color: #007700">(</span><span style="color: #DD0000">'HTTP/1.0 401 Unauthorized'</span><span style="color: #007700">);<br /> echo </span><span style="color: #DD0000">"You must enter a valid login ID and password to access this resource\n"</span><span style="color: #007700">;<br /> exit;<br />}<br /> <br />if (!isset(</span><span style="color: #0000BB">$_SERVER</span><span style="color: #007700">[</span><span style="color: #DD0000">'PHP_AUTH_USER'</span><span style="color: #007700">]) ||<br /> (</span><span style="color: #0000BB">$_POST</span><span style="color: #007700">[</span><span style="color: #DD0000">'SeenBefore'</span><span style="color: #007700">] == </span><span style="color: #0000BB">1 </span><span style="color: #007700">&& </span><span style="color: #0000BB">$_POST</span><span style="color: #007700">[</span><span style="color: #DD0000">'OldAuth'</span><span style="color: #007700">] == </span><span style="color: #0000BB">$_SERVER</span><span style="color: #007700">[</span><span style="color: #DD0000">'PHP_AUTH_USER'</span><span style="color: #007700">])) {<br /> </span><span style="color: #0000BB">authenticate</span><span style="color: #007700">();<br />} else {<br /> echo </span><span style="color: #DD0000">"<p>Welcome: {$_SERVER['PHP_AUTH_USER']}<br />"</span><span style="color: #007700">;<br /> echo </span><span style="color: #DD0000">"Old: {$_REQUEST['OldAuth']}"</span><span style="color: #007700">;<br /> echo </span><span style="color: #DD0000">"<form action='{$_SERVER['PHP_SELF']}' METHOD='post'>\n"</span><span style="color: #007700">;<br /> echo </span><span style="color: #DD0000">"<input type='hidden' name='SeenBefore' value='1' />\n"</span><span style="color: #007700">;<br /> echo </span><span style="color: #DD0000">"<input type='hidden' name='OldAuth' value='{$_SERVER['PHP_AUTH_USER']}' />\n"</span><span style="color: #007700">;<br /> echo </span><span style="color: #DD0000">"<input type='submit' value='Re Authenticate' />\n"</span><span style="color: #007700">;<br /> echo </span><span style="color: #DD0000">"</form></p>\n"</span><span style="color: #007700">;<br />}<br /></span><span style="color: #0000BB">?></span></span></code></div> </div> </div> </p> <p class="simpara"> This behavior is not required by the HTTP Basic authentication standard, so you should never depend on this. Testing with Lynx has shown that Lynx does not clear the authentication credentials with a 401 server response, so pressing back and then forward again will open the resource as long as the credential requirements haven't changed. The user can press the '_' key to clear their authentication information, however. </p> <p class="simpara"> Also note that until PHP 4.3.3, HTTP Authentication did not work using Microsoft's IIS server with the CGI version of PHP due to a limitation of IIS. In order to get it to work in PHP 4.3.3+, you must edit your IIS configuration "Directory Security". Click on "Edit" and only check "Anonymous Access", all other fields should be left unchecked. </p> <p class="simpara"> Another limitation is if you're using the IIS module (ISAPI) and PHP 4, you may not use the <i>PHP_AUTH_*</i> variables but instead, the variable <i>HTTP_AUTHORIZATION</i> is available. For example, consider the following code: <i>list($user, $pw) = explode(':', base64_decode(substr($_SERVER['HTTP_AUTHORIZATION'], 6)));</i> </p> <blockquote><p><b class="note">Note</b>: <b>IIS Note:</b><br /> <span class="simpara"> For HTTP Authentication to work with IIS, the PHP directive <a href="ini.core.html#ini.cgi.rfc2616-headers" class="link">cgi.rfc2616_headers</a> must be set to <i>0</i> (the default value). </span> </p></blockquote> <blockquote><p><b class="note">Note</b>: If <a href="features.safe-mode.html#ini.safe-mode" class="link">safe mode</a> is enabled, the uid of the script is added to the <i>realm</i> part of the <i>WWW-Authenticate</i> header. <br /> </p></blockquote> </div><hr /><div style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="features.html">Features</a></div> <div class="next" style="text-align: right; float: right;"><a href="features.cookies.html">Cookies</a></div> <div class="up"><a href="features.html">Features</a></div> <div class="home"><a href="index.html">PHP Manual</a></div></div></body></html>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?