Home >Backend Development >PHP Tutorial >PHP implements HTTP authentication

PHP implements HTTP authentication

*文
*文Original
2017-12-26 10:03:142095browse

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[&#39;PHP_AUTH_USER&#39;])) {
        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[&#39;PHP_AUTH_USER&#39;] == &#39;mani&#39;) && ($_SERVER[&#39;PHP_AUTH_PW&#39;] == &#39;w#m3nt0r&#39;)) {
            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 problem

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn