This article describes the basic HTTP authentication skills in PHP with examples. Share it with everyone for your reference. The specific analysis is as follows:
Used to prevent users from accessing directories on certain servers by combining .htaccess files and .htpasswd files. These files contain information about the users allowed to access a directory and their passwords. HTTP authentication can be done by sending special HTTP header information instead of using .htaccess file
Copy code The code is as follows:
If (!isset($_SERVER['PHP_AUTH_USER'])) {
header("WWW-Authenticate: Basic realm="My Private Area"");
header("HTTP/1.0 401 Unauthorized");
print "You need valid credentials to get access!n";
exit;
} else {
If (($_SERVER['PHP_AUTH_USER'] == 'mani') && ($_SERVER['PHP_AUTH_PW'] == 'w#m3nt0r')) {
print "Welcome to the private area!";
} else {
header("WWW-Authenticate: Basic realm="My Private Area"");
header("HTTP/1.0 401 Unauthorized");
print "You need valid credentials to get access!n";
exit;
}
}
?>
I hope this article will be helpful to everyone’s PHP programming design.