How is basic HTTP authentication implemented in PHP? This article mainly introduces the basic HTTP authentication techniques in PHP, and analyzes the principles and implementation methods of HTTP authentication with examples. I hope to be helpful.
The examples in this article describe basic HTTP authentication techniques in PHP. Share it with everyone for your reference. The specific analysis is as follows:
is 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
<?php 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; } } ?>x
Related recommendations:
php http protocol post Summary of information related to request parameters
php HTTP request class, Support GET, POST, Multipart/form-data
The above is the detailed content of PHP implements HTTP authentication. For more information, please follow other related articles on the PHP Chinese website!