• This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn more.

[Solved] HTTP Authentication Basic Using PHP

Kepala Suku

Web & Mobile Developer
Staff member
Here is a function for creating HTTP Authentication Using PHP
function require_auth() {
    $AUTH_USER = 'your_user';
    $AUTH_PASS = 'your_password';
    header('Cache-Control: no-cache, must-revalidate, max-age=0');
    $has_supplied_credentials = !(empty($_SERVER['PHP_AUTH_USER']) && empty($_SERVER['PHP_AUTH_PW']));
    $is_not_authenticated = (
        !$has_supplied_credentials ||
        $_SERVER['PHP_AUTH_USER'] != $AUTH_USER ||
        $_SERVER['PHP_AUTH_PW']   != $AUTH_PASS
    if ($is_not_authenticated) {
        header('HTTP/1.1 401 Authorization Required');
        header('WWW-Authenticate: Basic realm="Access denied"');
You could use this function easilly. Just call require_auth() function at begining of your php block code.