所以我目前正在学习
PHP编程课程,最后我进入了认证和授权的艺术.
原文链接:https://www.f2er.com/php/136949.html因此,在本书中,我们首先限制授予您可以更新和删除所有帐户的所有帐户的访问权限的页面.我们从定义常量开始:
define('VALID_USERNAME',''); define('VALID_PASSWORD','');
为了方便自己,我只是将它们作为空字符串.
一切顺利,弹出用户名和密码的对话框弹出.
但是,当我把空字符串放入时,对话框会一直弹出,好像密码错误,或者根本就没有输入密码.即使我用真实字符串定义用户名和密码,也会发生这种情况.
这是我的部分代码:
if (!isset($_SERVER['PHP_AUTH_USER']) || !isset($_SERVER['PHP_AUTH_PW'])) { header('http/1.1 401 Unauthorized'); header('WWW-Authenticate: Basic realm="Wonder Penguin"'); } else { if (($_SERVER['PHP_AUTH_USER'] != VALID_USERNAME) || ($_SERVER['PHP_AUTH_PW'] != VALID_PASSWORD)) { header('http/1.1 401 Unauthorized'); header('WWW-Authenticate: Basic realm="Wonder Penguin"'); } }
如果您想知道为什么我没有使用exit()函数来阻止人们点击取消来绕过身份验证并进入更新页面.我这样做了,所以我的老师可以评分我创建最后作业的页面.
我是多么厌倦排除故障:
我测试了是否通过使用echo函数正确定义了有效的用户名和密码.
echo VALID_USERNAME; echo VALID_PASSWORD;
它突然出现了我的定义.所以在理论上,我认为我正确地定义了它.
我试着用单引号,双引号和没有引号来编写define函数.
这本书要我写这样的函数:
define(VALID_USERNAME,"admin"); define(VALID_PASSWORD,"password");
但是,当我尝试在那里定义它时,这会导致我使用未定义常量的错误.
后续代码var_dump($_ SERVER);结果:
array(37) { ["REDIRECT_HANDLER"]=> string(23) "application/x-httpd-PHP" ["REDIRECT_STATUS"]=> string(3) "200" ["HTTP_HOST"]=> string(20) "patti-bee2.dcccd.edu" ["HTTP_CONNECTION"]=> string(10) "keep-alive" ["HTTP_ACCEPT"]=> string(74) "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8" ["HTTP_USER_AGENT"]=> string(108) "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML,like Gecko) Chrome/31.0.1650.57 Safari/537.36" ["HTTP_ACCEPT_ENCODING"]=> string(17) "gzip,deflate,sdch" ["HTTP_ACCEPT_LANGUAGE"]=> string(14) "en-US,en;q=0.8" ["HTTP_COOKIE"]=> string(217) "__qca=P0-630369357-1378011844686; __utma=198331962.264424896.1377179965.1382812794.1384740700.12; __utmc=198331962; __utmz=198331962.1381981575.8.4.utmcsr=google|utmccn=(organic)|utmcmd=organic|utmctr=(not%20provided)" ["PATH"]=> string(135) "C:\Program Files (x86)\PHP\;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;c:\PHP;" ["SystemRoot"]=> string(10) "C:\Windows" ["COMSPEC"]=> string(27) "C:\Windows\system32\cmd.exe" ["PATHEXT"]=> string(53) ".COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC" ["WINDIR"]=> string(10) "C:\Windows" ["SERVER_SIGNATURE"]=> string(0) "" ["SERVER_SOFTWARE"]=> string(21) "Apache/2.2.22 (Win32)" ["SERVER_NAME"]=> string(20) "patti-bee2.dcccd.edu" ["SERVER_ADDR"]=> string(14) "144.162.99.193" ["SERVER_PORT"]=> string(2) "80" ["REMOTE_ADDR"]=> string(11) "99.7.247.36" ["DOCUMENT_ROOT"]=> string(66) "C:/Program Files (x86)/Apache Software Foundation/Apache2.2/htdocs" ["SERVER_ADMIN"]=> string(16) "pburks@dcccd.edu" ["SCRIPT_FILENAME"]=> string(106) "C:\Program Files (x86)\Apache Software Foundation\Apache2.2\htdocs\Coleman\Wonder Penguin\PHP\show_all.PHP" ["REMOTE_PORT"]=> string(5) "54619" ["REDIRECT_URL"]=> string(40) "/coleman/wonder penguin/PHP/show_all.PHP" ["GATEWAY_INTERFACE"]=> string(7) "CGI/1.1" ["SERVER_PROTOCOL"]=> string(8) "HTTP/1.1" ["REQUEST_METHOD"]=> string(3) "GET" ["QUERY_STRING"]=> string(0) "" ["REQUEST_URI"]=> string(42) "/coleman/wonder%20penguin/PHP/show_all.PHP" ["SCRIPT_NAME"]=> string(40) "/coleman/wonder penguin/PHP/show_all.PHP" ["ORIG_SCRIPT_FILENAME"]=> string(18) "C:/PHP/php-cgi.exe" ["ORIG_PATH_INFO"]=> string(40) "/coleman/wonder penguin/PHP/show_all.PHP" ["ORIG_PATH_TRANSLATED"]=> string(106) "C:\Program Files (x86)\Apache Software Foundation\Apache2.2\htdocs\Coleman\Wonder Penguin\PHP\show_all.PHP" ["ORIG_SCRIPT_NAME"]=> string(16) "/PHP/php-cgi.exe" ["PHP_SELF"]=> string(40) "/coleman/wonder penguin/PHP/show_all.PHP" ["REQUEST_TIME"]=> int(1385432192) }
此时我已经将它直接从书中复制出来了.
我究竟做错了什么?
如果你想亲自尝试一下,这里是link to my site.