How to write a PHP application to implement Digest authentication _php tutorial

Source: Internet
Author: User
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 ...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.