Php methods for creating basic identity authentication sites

Source: Internet
Author: User

By default, most web servers are configured with anonymous access, that is, user information on the server is not prompted to identify the information. Anonymous access means that users can access the website without using their usernames and passwords. This is also the configuration used by most public websites.
In the Apache configuration file "httpd. conf", anonymous access is configured by default (as follows ): Copy codeThe Code is as follows: <directory "C:/program files/Apache software foundation/apache2.2/htdocs">
Options Indexes FollowSymLinks Includes
AllowOverride None
Order allow, deny
Allow from all
</Directory>

--------------------------------------------------------------------------------
To force the browser to use BASIC identity authentication, a WWW-Authenticate field must be passed. For example, the following code uses the header () function to require the client to use BASIC authentication, it adds a WWW-Authenticate field to the HTTP message header:
Header ("WWW-Authenticate: BASIC Realm = My Realm ");
--------------------------------------------------------------------------------
Write a usage example belowCopy codeThe Code is as follows: <? Php
If (! Isset ($ _ SERVER ['php _ AUTH_USER ']) {
Header ("WWW-Authenticate: BASIC Realm = My Realm ");
Header ("HTTP/1.0 401 Unauthorized ");
Echo ("incorrect account/password! ");
Exit;
} Else {
/* Obtain the user name and password for verification */
$ User = $ _ SERVER ['php _ AUTH_USER '];
$ Pwd = $ _ SERVER ['php _ AUTH_PW '];
If ($ user = "admin" & $ pwd = "password "){
Echo "verified ";
} Else {
Header ("HTTP/1.0 401 Unauthorized ");
Echo "incorrect account/password! ";
Exit;
}
}
?>

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.