Compile a php application to implement Digest Authentication

Source: Internet
Author: User
This article provides a detailed analysis of how to compile a php application to implement Digest Authentication. For more information, see 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 for verification. It adds the WWW-Authenticate field to the HTTP message header:
Header ('www-Authenticate: Digest Realm = "MyRealm", nonce = "47alf7cf25ce7", algorithm = MD5, qop = "auth "');
--------------------------------------------------------------------------------
The following code describes a web page that uses Digest Authentication (first cancel the Apache authentication configuration ).
The code is as follows:
$ Realm = "MyRealm ";
// If no authentication information is provided, the sending 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 "incorrect account/password! ";
Exit;
} Else {
// Use the http_digest_parse function to parse the verification information
$ Data = http_digest_parse ($ _ SERVER ["PHP_AUTH_DIGEST"]);
If (! $ Data ){
Header ("HTTP/1.0 401 Unauthorization Required ");
Echo "incorrect account/password! ";
Exit;
} Else {
// Construct a response value based on the HTTP protocol
$ A1 = md5 ('admin: '. $ realm.': password ');
$ A2 = md5 ($ _ SERVER ['request _ method']. ':'. $ data ['uris ']);
$ Valid_response =
Md5 ($ A1 .':'. $ data ['nonce ']. ':'. $ data ['NC ']. ':'. $ data ['cnonce ']. ':'. $ data ['qop ']. ':'. $ A2 );}
// Compare the self-built response value with the response value constructed and sent by the browser. if the value is the same, the user name and password are entered correctly.
If ($ data ['response'] = $ valid_response ){
Echo "verified! ";
} Else {
Header ("HTTP/1.0 401 Unauthorization Required ");
Echo ("incorrect account/password! ");
Exit;
}
}
Function http_digest_parse ($ digest_str ){
$ Needed_parts = array ('nonce '=> 1, 'NC' => 1, 'cnonce '=> 1, 'qop' => 1, 'username' => 1, 'URL' => 1, 'response' => 1 );
// Use a regular expression to parse the content of the Authorization header
Preg_match_all ('@ (/w +) = ([/' "]?) ([A-zA-Z0-9 =. // _-] +)/2 @ ', $ digest_str, $ result, PREG_SET_ORDER );
// Fill the result in the $ data array and return
$ 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.