php's method for creating basic authentication sites is explained in detail

  • 2020-06-07 04:07:26
  • OfStack

By default, most web servers 1 are configured for anonymous access, that is, users will not be asked for identification information when accessing information on the server 1. Anonymous access means that users can access the site without logging in with a username and password. This is also the configuration used by most public websites.
In Apache's configuration file "httpd.conf", by default it is configured for anonymous access (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 authentication, you must pass an ES10en-ES11en field. For example, the following code USES the header() function to require the client to use BASIC authentication by adding an ES15en-ES16en field to the HTTP header:
header("WWW-Authenticate:BASIC Realm=My Realm");
--------------------------------------------------------------------------------
Let me write down 1 to use

<?php
if(!isset($_SERVER['PHP_AUTH_USER'])){
header("WWW-Authenticate:BASIC Realm=My Realm");
header("HTTP/1.0 401 Unauthorized");
echo(" account / Password error! ");
exit;
}else{
/* Get username and password for verification */
$user=$_SERVER['PHP_AUTH_USER'];
$pwd=$_SERVER['PHP_AUTH_PW'];
if($user=="admin"&&$pwd="password"){
echo " Through the verification ";
}else{
header("HTTP/1.0 401 Unauthorized");
echo " account / Password error! ";
exit;
}
}
?>


Related articles: