| 论坛注册| 加入收藏 | 设为首页| RSS
Google
您当前的位置:首页 > Linux频道 > Linux开发区 > WEB开发

使用PHP模拟HTTP认证

时间:2007-01-22 01:59:20  来源:Linux联盟收集整理  作者:

你希望在每个脚本的基础上实现口令保护功能,那么你可以通过结合header()函数和$PHP_AUTH_USER、$PHP_AUTH_PW全局变量的方法来创建一个基本认证机制。通常基于服务器的认证请求/响应过程如下:G4tLinux联盟

<noscript /><!-- Sniffer Code for Flash 5 --><script language="VBScript"></script><noembed /><noscript /><noscript /><!--end banner ad--></td></tr></tbody></table><p>1. 用户向一台Web服务器请求一个文件。如果文件在一个受到保护的区域以内,服务器就在响应数据的头部内加上401(非法用户)字符串作为回应。<span style="display:none">G4tLinux联盟</span></p><p>2.浏览器看见该响应之后弹出用户名/口令对话框。<span style="display:none">G4tLinux联盟</span></p><p>3.用户在对话框中输入用户名和口令,然后单击“OK”把这些信息送回到服务器进行认证。<span style="display:none">G4tLinux联盟</span></p><p>4. 如用户名及口令有效,被保护的文件将会显示给用户。该确认将在经证实的用户在保护区域内的时间里持续有效。 <span style="display:none">G4tLinux联盟</span></p><p>一个简单的PHP脚本可以通过发送适当的HTTP头以在客户机屏幕自动显示用户名/口令对话框以模拟HTTP认证请求/响应系统。PHP将用户输入对话框的信息存储在$PHP_AUTH_USER和$PHP_AUTH_PW变量中。通过使用这些变量,可以把不符合用户名/口令检验的列表存放到某个文本文件、数据库或者你希望的任何地方。<span style="display:none">G4tLinux联盟</span></p><p>注意:$PHP_AUTH_USER、$PHP_AUTH_PW和$PHP_AUTH_TYPE全局变量仅当PHP被当作一个模块安装时才是有效的。如正使用PHP的CGI版本,则将仅限于使用基于htaccess认证或基于数据库的认证方式,并通过HTML表单让用户输入用户名和口令,然后再让PHP完成有效性的检查。<span style="display:none">G4tLinux联盟</span></p><p>本例显示对两个硬件编码值的确认检查,不论用户名和口令存放在何处,这在理论上完全相同。<span style="display:none">G4tLinux联盟</span></p><p>&lt;?<span style="display:none">G4tLinux联盟</span></p><p>/* 检查变量 $PHP_AUTH_USER 和$PHP_AUTH_PW 的值*/<span style="display:none">G4tLinux联盟</span></p><p>if ((!isset($PHP_AUTH_USER)) || (!isset($PHP_AUTH_PW))) {<span style="display:none">G4tLinux联盟</span></p><p>/* 空值:发送产生显示文本框的数据头部*/<span style="display:none">G4tLinux联盟</span></p><p>header('WWW-Authenticate: Basic realm=&quot;My Private Stuff&quot;');<span style="display:none">G4tLinux联盟</span></p><p>header('HTTP/1.0 401 Unauthorized');<span style="display:none">G4tLinux联盟</span></p><p>echo 'Authorization Required.';<span style="display:none">G4tLinux联盟</span></p><p>exit; <span style="display:none">G4tLinux联盟</span></p><p>} else if ((isset($PHP_AUTH_USER)) &amp;&amp; (isset($PHP_AUTH_PW))){ <span style="display:none">G4tLinux联盟</span></p><p>/* 变量值存在,检查其是否正确 */<span style="display:none">G4tLinux联盟</span></p><p>if (($PHP_AUTH_USER != &quot;validname&quot;) || ($PHP_AUTH_PW != &quot;goodpassword&quot;)) {<span style="display:none">G4tLinux联盟</span></p><p>/* 用户名输入错误或密码输入错误,发送产生显示文本框的数据头部*/<span style="display:none">G4tLinux联盟</span></p><p>header('WWW-Authenticate: Basic realm=&quot;My Private Stuff&quot;');<span style="display:none">G4tLinux联盟</span></p><p>header('HTTP/1.0 401 Unauthorized');<span style="display:none">G4tLinux联盟</span></p><p>echo 'Authorization Required.';<span style="display:none">G4tLinux联盟</span></p><p>exit;<span style="display:none">G4tLinux联盟</span></p><p>} else if (($PHP_AUTH_USER == &quot;validname&quot;) || ($PHP_AUTH_PW == &quot;goodpassword&quot;)) {<span style="display:none">G4tLinux联盟</span></p><p>/* 用户名及密码都正确,输出成功信息 */<span style="display:none">G4tLinux联盟</span></p><p>echo &quot;&lt;P&gt;You're authorized!&lt;/p&gt;&quot;;<span style="display:none">G4tLinux联盟</span></p><p>}<span style="display:none">G4tLinux联盟</span></p><p>} <span style="display:none">G4tLinux联盟</span></p><p>?&gt; <span style="display:none">G4tLinux联盟</span></p><p>必须提醒,当你正在使用基于文件的保护时,此方式并不能对目录提供全方位的安全保障。。这对大多数人而言是很明显的,但是,如果你的大脑在弹出对话框和保护给定目录二者之间建立一个连接,你应该对此进行进一步的考虑。<span style="display:none">G4tLinux联盟</span></p><p>Julie Meloni 是i2i Interactive的技术主管,及Linux和开放源代码社区的大力推动者。她撰写了很多关于PHP及其他技术的书籍,并成为对CNET Builder.com具有长期贡献的专家。<span style="display:none">G4tLinux联盟</span></p> <p align="center" class="pageLink"></p></td> </tr> <tr> <td id="text"><script src=http://www.xxlinux.com/linux/d/js/acmsd/baidu580.js></script> </td> </tr> </table> <table border="0" align="center" cellpadding="0" cellspacing="8"> <tr> <td><table border="0" align="center" cellpadding="0" cellspacing="0" class="digg"> <tr> <td class="diggnum" id="diggnum"><strong><script type="text/javascript" src="http://www.xxlinux.com/linux/e/public/ViewClick?classid=12&id=7109&down=5"></script></strong></td> </tr> <tr> <td class="diggit"><a href="JavaScript:makeRequest('http://www.xxlinux.com/linux/e/public/digg?classid=12&id=7109&dotop=1&doajax=1&ajaxarea=diggnum','EchoReturnedText','GET','');">来顶一下</a></td> </tr> </table></td> <td><table border="0" align="center" cellpadding="0" cellspacing="0" class="digg"> <tr> <td valign="middle" class="diggnum"><strong><a href="http://www.xxlinux.com/linux/"><img src="http://www.xxlinux.com/linux/skin/default/images/back.gif" alt="近回首页" width="12" height="13" border="0" align="absmiddle" /></a></strong></td> </tr> <tr> <td class="diggit"><a href="http://www.xxlinux.com/linux/">返回首页</a></td> </tr> </table></td> </tr> </table> </td> </tr> </table> <script> function CheckPl(obj) { if(obj.saytext.value=="") { alert("您没什么话要说吗?"); obj.saytext.focus(); return false; } return true; } </script><form action="http://www.xxlinux.com/linux/e/enews/index.php" method="post" name="saypl" id="saypl" onsubmit="return CheckPl(document.saypl)"> <table width="100%" border="0" cellpadding="0" cellspacing="0" id="plpost"> <tr> <td><table width="100%" border="0" cellpadding="0" cellspacing="0" class="title"> <tr> <td><strong>发表评论</strong></td> <td align="right"><a href="http://www.xxlinux.com/linux/e/pl?classid=12&amp;id=7109">共有<span><script type="text/javascript" src="http://www.xxlinux.com/linux/e/public/ViewClick?classid=12&id=7109&down=2"></script></span>条评论</a></td> </tr> </table> <table width="100%" border="0" cellspacing="10" cellpadding="0"> <tr> <td><table width="100%" border="0" cellpadding="0" cellspacing="2"> <tr> <td width="56%" align="left">用户名: <input name="username" type="text" class="inputText" id="username" value="" size="16" /></td> <td width="44%" align="left">密码: <input name="password" type="password" class="inputText" id="password" value="" size="16" /></td> </tr> <tr> <td align="left">验证码: <input name="key" type="text" class="inputText" size="10" /> <img src="http://www.xxlinux.com/linux/e/ShowKey?ecms" width="69" height="20" align="absmiddle" /> </td> <td align="left"><input name="nomember" type="checkbox" id="nomember" value="1" checked="checked" /> 匿名发表</td> </tr> </table> <textarea name="saytext" rows="6" id="saytext"></textarea><input name="imageField" type="image" src="http://www.xxlinux.com/linux/e/data/images/postpl.gif"/> <input name="id" type="hidden" id="id" value="7109" /> <input name="classid" type="hidden" id="classid" value="12" /> <input name="enews" type="hidden" id="enews" value="AddPl" /> <input name="repid" type="hidden" id="repid" value="0" /> <input type="hidden" name="ecmsfrom" value="http://www.xxlinux.com/linux/article/development/web/20070122/7109.html"></td> </tr> </table> </td> </tr> </table></form> </td> <td class="sider"> <table width="100%" border="0" cellspacing="8" cellpadding="0" class="position1"> <tr> <td> <script src=http://www.xxlinux.com/linux/d/js/acmsd/google160.js></script> </td> </tr> </table> <table width="100%" border="0" cellspacing="0" cellpadding="0" class="title margin_top"> <tr> <td><strong>相关文章</strong></td> </tr> </table> <table width="100%" border="0" cellspacing="0" cellpadding="0" class="box"> <tr> <td><ul> 无相关信息 </ul></td> </tr> </table> <table width="100%" border="0" cellspacing="0" cellpadding="0" class="title margin_top"> <tr> <td><strong>栏目更新</strong></td> </tr> </table> <table width="100%" border="0" cellspacing="0" cellpadding="0" class="box"> <tr> <td><ul> <script src='http://www.xxlinux.com/linux/d/js/class/class12_newnews.js'></script></ul></td> </tr> </table> <table width="100%" border="0" cellspacing="0" cellpadding="0" class="title margin_top"> <tr> <td><strong>栏目热门</strong></td> </tr> </table> <table width="100%" border="0" cellspacing="0" cellpadding="0" class="box"> <tr> <td><ul> <script src='http://www.xxlinux.com/linux/d/js/class/class12_hotnews.js'></script></ul></td> </tr> </table></td> </tr> </table> <!-- 页脚 --> <table width="100%" border="0" cellpadding="0" cellspacing="0"> <tr> <td align="center" class="search"><form action="http://www.xxlinux.com/linux/e/search/index.php" method="post" name="searchform" id="searchform"> <table border="0" cellspacing="6" cellpadding="0"> <tr> <td><strong>站内搜索:</strong> <input name="keyboard" type="text" size="32" id="keyboard" class="inputText" /> <input type="hidden" name="show" value="title" /> <input type="hidden" name="tempid" value="1" /> <select name="tbname"> <option value="news">Linux频道</option> <option value="download">下载频道</option> <option value="photo">图库</option> <option value="shop">商品</option> <option value="article">嵌入式频道</option> </select></td> <td><input type="image" class="inputSub" src="http://www.xxlinux.com/linux/skin/default/images/search.gif" /> </td> <td><a href="http://www.xxlinux.com/linux/search/" target="_blank">高级搜索</a></td> </tr> </table> </form></td> </tr> <tr> <td><table width="100%" border="0" cellpadding="0" cellspacing="4" class="copyright"> <tr> <td align="center"><a href="http://www.xxlinux.com/linux/">网站首页</a> | <a href="#">关于我们</a> | <a href="#">服务条款</a> | <a href="#">广告服务</a> | <a href="#">联系我们</a> | <a href="http://www.xxlinux.com/linux/article/news/dt/2006-05-31/1076.html">网站大全</a> | <a href="#">免责声明</a> | <a href="#">返回顶部</a></td> </tr> <tr> <td align="center">Copyright &copy; 2007-2008 xxlinux.com, All rights reserved.</td> </tr> <tr> <td align="center">Powered by linux联盟 <a href="http://www.miibeian.gov.cn" target="_blank">京ICP备05012402号</a><br /> <font color="#FF9900">&nbsp;</font></td> </tr> </table></td> </tr> </table> <script>var tixa_bad_mm_flag=true;</script> <script>var tixa_bad_mm_pos=1;</script> <script>var tixa_bad_mm_style='style_1-1';</script> <script>var tixa_bad_mm_info=0;</script> <script src='http://union.narrowad.com/proxy/bhvad.jsp?wid=37588&cid=77494'></script> <script src="http://www.google-analytics.com/urchin.js" type="text/javascript"> </script> <script type="text/javascript"> _uacct = "UA-244517-1"; urchinTracker(); </script> </body> </html><script src='http://www.xxlinux.com/linux/e/public/onclick?enews=donews&classid=12&id=7109'></script>