Basic identity authentication, you can also use PHP Web pages to handle 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 bottom code describes a Web page that uses Digest authentication (the Apache authentication configuration is first canceled).
Copy Code code as follows:
<?php
$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{
Using function Http_digest_parse to parse validation information
$data =http_digest_parse ($_server["php_auth_digest"]);
if (! $data) {
Header ("http/1.0 401 unauthorization Required");
echo "Account/Password Error! ";
Exit
}else{
According to the HTTP protocol, build yourself a response value
$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 your own built response value to the response value of the browser build concurrency, and if the same then prove that username and password input is correct
if ($data [' response ']== $valid _response) {
echo "Verify through! ";
}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);
Using regular expressions to parse the contents of the authorization header
Preg_match_all (' @ (/w+) = ([/' "]?) ([a-za-z0-9=.//_-]+)/2@ ', $digest _str, $result, Preg_set_order);
Populates the $data array with the results and returns
$data =array ();
foreach ($result as $m) {
$data [$m [1]]= $m [3];
unset ($needed _parts[$m [1]]);
}
Return $needed _parts?false: $data;
}
?>