SoFunction
Updated on 2025-04-08

Detailed explanation of how to create a basic identity authentication site by php

By default, most web servers are generally configured for anonymous access, that is, users are generally not required to prompt for identification information when accessing information on the server. Anonymous access means that users can access the website without logging in with their username and password. This is also the configuration used by most public websites.
In Apache's configuration file "", it is configured for anonymous access by default (as follows):
Copy the 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, which adds a WWW-Authenticate field to the HTTP message header:
header("WWW-Authenticate:BASIC Realm=My Realm");
--------------------------------------------------------------------------------
Write a use below
Copy the 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("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 "pass verification";
}else{
header("HTTP/1.0 401 Unauthorized");
echo "Incorrect account/password!";
exit;
}
}
?>