Basic identity authentication, you can also use the PHP Web page to process HTTP request header fields to match Digest authentication information. For example, the following code uses the header () function to require the client to use Digest authentication, which adds a www-authenticate field to the HTTP message header:
header (' www-authenticate:digest realm= ' Myrealm ', nonce= ' 47alf7cf25ce7 ', algorithm=md5,qop= ' auth ');
--------------------------------------------------------------------------------
The code below describes a Web page that uses Digest authentication (the Apache authentication configuration is first canceled).
Copy CodeThe code is as follows:
$realm = "Myrealm";
If no authentication information is available, the Send header requires the browser to use Digest authentication
if (!isset ($_server[' php_auth_digest ')) {
Header ("Www-authenticate:digest realm=/" $realm/", nonce=/" ". Uniqid ()." /", algorithm=md5,qop=/" auth/"");
Header ("http/1.0 401 unauthorization Required");
echo "Account/Password Error!" ";
Exit
}else{
Parsing validation information using function Http_digest_parse
$data =http_digest_parse ($_server["php_auth_digest"]);
if (! $data) {
Header ("http/1.0 401 unauthorization Required");
echo "Account/Password Error!" ";
Exit
}else{
Build a response value yourself based on the HTTP protocol
$A 1=md5 (' admin: ' $realm. ':p assword ');
$A 2=md5 ($_server[' Request_method '). ': ' $data [' URI ']);
$valid _response=
MD5 ($A 1. ': ' $data [' nonce ']. ': ' $data [' NC ']. ': '. $data [' cnonce ']. ': '. $data [' Qop ']. ': '. $A 2);}
Compare the response values you build with the response values sent by the browser and then prove that the user name and password input are correct
if ($data [' response ']== $valid _response) {
echo "Verification passed! ";
}else{
Header ("http/1.0 401 unauthorization Required");
Echo ("Account/Password Error! ");
Exit
}
}
function Http_digest_parse ($digest _str) {
$needed _parts=array (' nonce ' =>1, ' NC ' =>1, ' cnonce ' =>1, ' Qop ' =>1, ' username ' =>1, ' uri ' =>1, ' Response ' =>1);
Parsing the contents of the authorization header using regular expressions
Preg_match_all (' @ (/w+) = ([/' "]?) ([a-za-z0-9=.//_-]+)/2@ ', $digest _str, $result, Preg_set_order);
Populates the result with the $data array and returns
$data =array ();
foreach ($result as $m) {
$data [$m [1]]= $m [3];
unset ($needed _parts[$m [1]]);
}
Return $needed _parts?false: $data;
}
?>
http://www.bkjia.com/PHPjc/327488.html www.bkjia.com true http://www.bkjia.com/PHPjc/327488.html techarticle Basic identity authentication, you can also use the PHP Web page to process HTTP request header fields to match Digest authentication information. For example, the code below uses the header () function to require the client to use ...