Write a method for implementing Digest Authentication in PHP application _php tips

Source: Internet
Author: User
Tags auth md5
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;
}
?>

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.